G
Gustaf Liljegren
Here's a small "problem" I often run into. The argument names in the
constructor are usually the same as the those I want to store away as
class variables. This example shows the problem and how I use to solve it:
public class Person
{
private string _name;
private string _age;
public Person(string name, string age)
{
_name = name;
_age = age;
}
}
It's ugly, but the underscore tells me later on that this variable is
global within the class. Another way I used is single-letter argument
names in constructors (not in other methods), like in:
public class Person
{
private string name;
private string age;
public Person(string n, string a)
{
name = n;
age = a;
}
}
This looks cleaner, but it makes it harder to call the constructor (when
you can't remember the acronyms), and the names don't indicate where the
variables are declared. I hope there are other tidy and useful ways I
have overlooked. Please share your's.
Gustaf
constructor are usually the same as the those I want to store away as
class variables. This example shows the problem and how I use to solve it:
public class Person
{
private string _name;
private string _age;
public Person(string name, string age)
{
_name = name;
_age = age;
}
}
It's ugly, but the underscore tells me later on that this variable is
global within the class. Another way I used is single-letter argument
names in constructors (not in other methods), like in:
public class Person
{
private string name;
private string age;
public Person(string n, string a)
{
name = n;
age = a;
}
}
This looks cleaner, but it makes it harder to call the constructor (when
you can't remember the acronyms), and the names don't indicate where the
variables are declared. I hope there are other tidy and useful ways I
have overlooked. Please share your's.
Gustaf