开发者

Edited this question with another related question. Please help: Can I use the same variable names for several methods of a class?

开发者 https://www.devze.com 2023-03-12 07:35 出处:网络
Thanks everyone for your answers. The one issue I am having though is when I print the value of each of the variables in the methods Helpdesk and IT, it does not print \"help\", \"hellp123\", and \"We

Thanks everyone for your answers. The one issue I am having though is when I print the value of each of the variables in the methods Helpdesk and IT, it does not print "help", "hellp123", and "Welcome Helpdesk!" rather prints "admin", "admin" and "Welcome". THe case is same for the method IT.

public class Login
{
    public string[] array = new string[3];
    public string UserName;
    public string Password;
    public string Message;
}

public Login
{
   UserName = array[0] = "admin";
   Password = array[1] = "admin"; 
   Message = array[2] = "Welcome";

}

public void Helpdesk
{
    UserName = "help";
    Password = "help123";
    Message = "开发者_如何学JAVAWelcome Helpdesk!";
    Console.WriteLine(UserName); //still prints admin
    Console.WriteLine(Password); // still prints admin
    Console.WriteLine(Message); // still prints Welcome
}
public void IT
{
    UserName = "it";
    Password = "Pa$$w0rd";
    Message = "Welcome IT!";

}

Can I use the same variable names: UserName, Password and Message for methods: Helpdesk and IT for the same class Login like below?

public class Login { public string[] array = new string[3]; public string UserName; public string Password; public string Message; }

public Login
{
   UserName = array[0] = "admin";
   Password = array[1] = "admin"; 
   Message = array[2] = "Welcome";  
}

public void Helpdesk
{
    UserName = "help";
    Password = "help123";
    Message = "Welcome Helpdesk!";
}
public void IT
{
    UserName = "it";
    Password = "Pa$$w0rd";
    Message = "Welcome IT!";

}


The variables you talk of are fields in the class. The are confined to that class. If you create new variables with the same name, but not inside the same class (like you do above), that is allowed. If you do it inside your class, it will shadow the field, which is considered a bad practice.

Note: if you only assign to the field from inside the class (like you may imply in your question, but the code is not complete), you simply change its value.

public class Login
{
    public string Message;
    public string Name;


    public void Test()
    {
       Message =  "Welcome";  // changes field, same as this.Message = ...
       var Name = "Me";       // does not change field, hides field Name
    }

}


You can, but you must have in mind that those variables are in a class-level scope. When a method changes any of them, the other methods are gonna use the new value as well.


If you're intending on changing the value of the class variables, yes.


Yes, you could definitely do this. The variables would be in scope being declared at the class level, they are usable in methods of that class. Further, if you declare variables in a method with the same name as variables declared in the class the method variables would take precedence until the method has completed.


Yes you can, since you declared the variables as class member variables, you can access it in any method in the class. You can even access it outside the class since they are public but a class's instance is required if its not public and static.

In response to your edited question, the code you provided doesn't even compile, however assuming that you want to construct a class

public class Login
{
    public string[] array = new string[3];
    public string UserName;
    public string Password;
    public string Message;


public Login()
{
   UserName = array[0] = "admin";
   Password = array[1] = "admin"; 
   Message = array[2] = "Welcome";

}

public void Helpdesk()
{
    UserName = "help";
    Password = "help123";
    Message = "Welcome Helpdesk!";
    Console.WriteLine(UserName); //still prints admin
    Console.WriteLine(Password); // still prints admin
    Console.WriteLine(Message); // still prints Welcome
}
public void IT()
{
    UserName = "it";
    Password = "Pa$$w0rd";
    Message = "Welcome IT!";

}
}

And in the main method if you do

 Login login = new Login();
    login.Helpdesk();
    Console.ReadKey();

It will output

help
help 123
Welcome Helpdisk!

And not

admin
admin
Welcome
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号