| | |
Using set and get in C Sharp
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
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?
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?
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
OK..
Not really - firstly it would only be static if the variable was static - eg it was going to be on a per class basis, not a perinstance, that and you dont *HAVE* to use get and set properties to access it, you could just make it public so you would either have a
In this instance you could make 5 copies of "SomeClass" and 1 counter is shared between them all (ACounter) and 1 counter is per instance (OtherCounter)
so, you dont *need* to but its good practice on the simple grounds that it saves changing it later, and protects your variables if you wish from change.. and you can add validation etc..
In short your initial code with a static property for a instance based private variable fails, because the private variable isnt instanced so the static class cant use it.
C# Syntax (Toggle Plain Text)
public uint m_sumenum; public static uint Somenum { set { m_somenum = value; } get { return m_somenum; } }
Not really - firstly it would only be static if the variable was static - eg it was going to be on a per class basis, not a perinstance, that and you dont *HAVE* to use get and set properties to access it, you could just make it public so you would either have a
C# Syntax (Toggle Plain Text)
public class SomeClass { public static int ACounter; public int OtherCounter; }
In this instance you could make 5 copies of "SomeClass" and 1 counter is shared between them all (ACounter) and 1 counter is per instance (OtherCounter)
so, you dont *need* to but its good practice on the simple grounds that it saves changing it later, and protects your variables if you wish from change.. and you can add validation etc..
In short your initial code with a static property for a instance based private variable fails, because the private variable isnt instanced so the static class cant use it.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
•
•
Join Date: Nov 2005
Posts: 33
Reputation:
Solved Threads: 5
public class SomeClass
{
static uint m_somenum;
public static uint SomeNum
{
get { return m_somenum; }
set { m_somenum=value; }
}
}
with this you can write something like this :
SomeClass.SomeNum=otherNumber;
but be warned that this is a static property ! so for each instance of he SomeClass the SomeNum will have the same value.
by the way i really did not understand your question but i think this is what you had in mind.
{
static uint m_somenum;
public static uint SomeNum
{
get { return m_somenum; }
set { m_somenum=value; }
}
}
with this you can write something like this :
SomeClass.SomeNum=otherNumber;
but be warned that this is a static property ! so for each instance of he SomeClass the SomeNum will have the same value.
by the way i really did not understand your question but i think this is what you had in mind.
Phew! You are mixing a few things here.
You can have :
In your main method you can now say :
SomeClass MyClass = new SomeClass(); //instantiation
Now use(example) MyClass.MyInt = 42;
You can also have :
No instantion needed this time, you can directly use
SomeClass2.MyInt2=43;
You can have :
C# Syntax (Toggle Plain Text)
public class SomeClass { public int MyInt; }
In your main method you can now say :
SomeClass MyClass = new SomeClass(); //instantiation
Now use(example) MyClass.MyInt = 42;
You can also have :
C# Syntax (Toggle Plain Text)
public static class SomeClass2 { public static int MyInt2; }
No instantion needed this time, you can directly use
SomeClass2.MyInt2=43;
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
![]() |
Similar Threads
- Dani's Cookbook (Geeks' Lounge)
- Can't get Sharp AR-275 printer to print in small network (Networking Hardware Configuration)
- please take a look at my site... (Website Reviews)
Other Threads in the C# Forum
- Previous Thread: How to see errors in a C# application in a website?
- Next Thread: Parallel Object Oriented
| Thread Tools | Search this Thread |
.net 2007 access algorithm appportability array barchart bitmap box broadcast c# camera check checkbox client combobox control conversion cs4 csharp custom database datagrid datagridview dataset date datetime degrees development draganddrop drawing encryption enum event eventcloseformc# excel file form format forms function gdi+ handler httpwebrequest image index input install java keypress label list listbox listener listview load mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting resolved. richtextbox search security server sleep socket sql statistics stream string table text textbox thread time timer update usercontrol validation view visual visualstudio webbrowser windows winforms wordautomation wpf xml






