4 Replies - 1043 Views - Last Post: 25 August 2012 - 02:41 AM Rate Topic: -----

#1 rfresh  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 141
  • Joined: 23-August 12

Name 'ChangePassword' does not exist in the current context ?

Posted 24 August 2012 - 08:52 PM

I'm trying add a class to my app to change the current Win7 user password. I don't under the error I'm getting:

The name 'ChangePassword' does not exist in the current context

I found this User class on the internet and am trying to use it in my app. The ChangePassword() function is declared as a public so I don't know why I can't call it.


namespace HelloWorld
{
    public class User
    {
        [DllImport("Netapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int NetUserChangePassword(
            string domainname,
            string username,
            string oldpassword,
            string newpassword
            );
        public static void ChangePassword(string username, string domain, string oldpassword, string newpassword)
        {
            int result = NetUserChangePassword(
                domain,
                username,
                oldpassword,
                newpassword
                );
            if (result != 0)
                throw new Win32Exception();
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) // void means this function returns nothing
        {

        }

        private void cmd_Set_Password_Click(object sender, EventArgs e)
        {
            ChangePassword(txt_Username.Text, txt_Domain.Text, txt_Old_Password.Text, txt_New_Password.Text);
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Name 'ChangePassword' does not exist in the current context ?

#2 KieranQuinn  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 6
  • Joined: 15-August 12

Re: Name 'ChangePassword' does not exist in the current context ?

Posted 24 August 2012 - 09:20 PM

You need to create an instance of the class.
Was This Post Helpful? -2
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3574
  • View blog
  • Posts: 11,114
  • Joined: 05-May 12

Re: Name 'ChangePassword' does not exist in the current context ?

Posted 24 August 2012 - 09:58 PM

He doesn't need to create an instance to access a static method of a class. It's like the way you don't need to create an instance of Console to access Console.WriteLine().
Was This Post Helpful? 1
  • +
  • -

#4 KieranQuinn  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 6
  • Joined: 15-August 12

Re: Name 'ChangePassword' does not exist in the current context ?

Posted 24 August 2012 - 10:10 PM

Didn't notice the method was static. It's 6:10am, with no sleep -_-
Was This Post Helpful? 0
  • +
  • -

#5 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Name 'ChangePassword' does not exist in the current context ?

Posted 25 August 2012 - 02:41 AM

*
POPULAR

As the ChangePassword method is in the User class, to call it from the form class you need to use User.ChangePassword().
Was This Post Helpful? 5
  • +
  • -

Page 1 of 1