i'm learning about encapsulation and have these examples of ways to control access of the data, both codes work and do the same thing, but through further researching i haven't been able to find any reference to the method access, so i was hoping someone could explain which one is better and reasons why? cause i'm a little confused
(when researching method access on msdn website it basically just gave me the properties example this is why i'm confused)
Method Access
public class LogInToken
{
private string Name;
private string Password;
public string GetName()
{
return Name;
}
public string GetPassword()
{
return Password;
}
public void SetName(string NewName)
{
Name = NewName;
}
public void SetPassword(string newPassword)
{
if (newPassword != null)
{
Password = newPassword;
}
else
{
throw new Exception("no password");
}
}
}
Property Access
public class LogInToken
{
private string name;
private string password;
public string Name
{
get
{
return name;
}
set
{
name = value; // C# uses the implicit parameter "value"
}
}
public string Password
{
get
{
return password;
}
set
{
if (value != null)
{
password = value;
}
else
{
throw new Exception("no password");
}
}
}
}