tags:

views:

59

answers:

4

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: " );

    }
}
A: 

Well, either you could make it public and then just calll it with:

Trainee.numberOfTrainee

Or you could create a static readonly property.

public static int NumberOfTrainee
{
    get
    {
        return numberOfTrainee;
    }
}
ho1
I don't want to make "numberOfTrainee" public. Then what should I do?(May be I'm asking stupid question!!)
But your need/desire to access the value outside the class means you do want to make it public.If you can give us some indication as to why you don't want to make it public that would perhaps allow us to answer your question more clearly.
Daz Lewis
@ho1 :: Thanks ho1.@Daz Lewis :: Actually I was just curious whether it is possible or impossible to access a private static data from other class without writing a property in the same assembly. Thanks.
+3  A: 

You should make a property, like this:

public class Trainee
{    
    private static int numberOfTrainee = 30;

    public int TraineeCount { get { return numberOfTrainee; } }

    private string traineeName;
    private string tarineeId;
}

Note that you may want to consider thread safety... it's possible that this simple implementation will give stale results in a multi-threaded environment. Making numberOfTrainee volatile would solve this.

Further, note that by making this a property rather than giving direct access to the field, you:

  • Can add extra logic should you ever wish to
  • Can make the property readonly, but modify the field from within the class
  • Keep encapsulation intact: a property is part of the API of a class, whereas a field is an implementation detail
Jon Skeet
Thanks Mr Skeet.
+2  A: 

If you don't want a getter for it, the only way to use it from elsewhere will be to increase the visibility, like so:

public class Trainee
{
    public static int NumberOfTrainee = 30;
    private string traineeName;
    private string tarineeId;
}

// Other code.
MessageBox.Show("Total number of trainee is: " + Trainee.NumberOfTrainee);

However, I would recommend against doing this with a field that can change. Unless it's a constant, you should define a property to control access to this field, static or not.

Rich
Thanks Rich for your security related suggestion.
A: 

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 Trainee and TraineeUI are in the same assembly, than TraineeUI can access the field of Trainee without the field being exposed to types outside the assembly.

I would make it a property instead of a field though.

Matthew Abbott
Thanks Mr. Abboutt for your nice suggestion.