I have a class like following:
public class Trainee
{
private static int numberOfTrainee = 30;
private string traineeName;
private string tarineeId;
}
Now I want to access the static data “numberOfTrainee” in the following class without creating a object of “Trainee” class and I don’t want to write getter for “numberOfTrainee”. Because, static member can be used only using “.” operator.
public class TraineeUI : Form
{
private void showButton_Click(object sender, EventArgs e)
{
// I want to access "numberOfTrainee" here. Something like following:
// MessageBox.Show("Total number of trainee is: " );
}
}
I don’t want to recycle what others have said, but….
You need to look at access modifiers. You say you don’t want to make numberOfTrainee public, but you can make it internal. That way, if
TraineeandTraineeUIare in the same assembly, thanTraineeUIcan access the field ofTraineewithout the field being exposed to types outside the assembly.I would make it a property instead of a field though.