• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Using set and get in C Sharp

complete

Weaksauce
Joined
Aug 30, 2005
Messages
92
What is the point of using set and get in C Sharp?

It seems variables are used differently in this language than in C++.

For some reason, you have to have a static variable defined like this:

public static uint Somenum
{
set { m_somenum = value; }
get { return m_somenum; }
}

and prior to this declaration, you need to have this:

public uint m_sumenum;

This seems to be the only way to expose a member of a class to other classes in C#.

The problem is that I seem to be doing this improperly because I get a compile error:

An object reference is required for the non-static field, metod, or property '.......m_somenum"

I think I see the problem. The problem is that I cannot use a static varable like this.

So you have to instantiate the class in order to set these members of the class.

So how would you do the equivalent of a global class in C Sharp?

Would I do it something like this:

public clase SomeClass
{
SomeClass someclass = new SomeClass();

public static uint Somenum
{
set { m_somenum = value; }
get { return m_somenum; }
}
}

Or perhaps this "new" needs to be outside of the class in order to work. So my next question is this. How and where would that command be such that it the internal set methods could be accessed by the other classes in the code?
 
What you are describing is a Property.

Properties:
http://msdn.microsoft.com/en-us/library/x9fsa0sw(VS.80).aspx
Code:
class TimePeriod
{
    private double seconds;

    public double Hours
    {
        get { return seconds / 3600; }
        set { seconds = value * 3600; }
    }
}

class Program
{
    static void Main()
    {
        TimePeriod t = new TimePeriod();

        // Assigning the Hours property causes the 'set' accessor to be called.
        t.Hours = 24;

        // Evaluating the Hours property causes the 'get' accessor to be called.
        System.Console.WriteLine("Time in hours: " + t.Hours);
    }
}


For information on the static modifier:
http://msdn.microsoft.com/en-us/library/98f28cdx(VS.80).aspx
 
This seems to be the only way to expose a member of a class to other classes in C#.

You don't need to use properties to expose members of a class.

Any public member can be accessed through an instance of an object. And public static member can be accessed through the class.

Your compile error is happening because your property is declared static and your member is not. You cannot access instance members from static members (unless you have a static instance of the class floating around, and that's another matter entirely.

What you want is:

Code:
private static uint m_somenum;

public static uint Somenum
{
set { m_somenum = value; }
get { return m_somenum; }
}
 
Excellent responses to the question. OP, it seems like you have the idea, but do you understand the difference between static and non-static declarations?
 
Back
Top