|
|
![]() |
|
|
Top | #1 |
|
OSNN Addict
Joined: August 2005
Posts: 94
Reputation: 10
Power: 79 |
Does anyone know how to declare a varable in a C# class so that it is accessable to other classes and code? After I have declared a class, I have accessed the methods of the class as it appears in intellisense type as I type the class name and then a dot. But the declared members of the class does not appear. |
|
|
|
|
|
Top | #2 |
|
*
Joined: December 2001
Location: USA
Posts: 6,490
Reputation: 2808
Power: 217 |
You need to learn how to use get and set within C#, to access public data variables. Google for it, you will get awesome answers. Or even better, http://stackoverflow.com/ and look for your programming questions there.
|
|
|
|
|
|
Top | #3 |
|
OSNN Addict
Joined: August 2005
Posts: 94
Reputation: 10
Power: 79 |
Originally Posted by X-Istence
I will take a look.
In the meantime: 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" |
|
|
|
|
|
Top | #4 |
|
OSNN Addict
Joined: August 2005
Posts: 94
Reputation: 10
Power: 79 |
Taking this article on classes and structs as an example:
http://msdn.microsoft.com/en-us/library/ms173109.aspx Code:
namespace ProgrammingGuide
{
// Class definition.
public class MyCustomClass
{
// Class members:
// Property.
public int Number { get; set; }
// Method.
public int Multiply(int num)
{
return num * Number;
}
// Instance Constructor.
public MyCustomClass()
{
Number = 0;
}
}
// Another class definition. This one contains
// the Main method, the entry point for the program.
class Program
{
static void Main(string[] args)
{
// Create an object of type MyCustomClass.
MyCustomClass myClass = new MyCustomClass();
// Set the value of a public property.
myClass.Number = 27;
// Call a public method.
int result = myClass.Multiply(4);
}
}
}
elsewhere in the program as if it were a global class. How would I do that? I could do something like this: Code:
class Program
{
public MyCustomClass myClass;
public Program()
{
// Create an object of type MyCustomClass.
myClass = new MyCustomClass();
// Set the value of a public property.
myClass.Number = 27;
// Call a public method.
int result = myClass.Multiply(4);
}
static void Main(string[] args)
{
Program program = new Program();
}
}
|
|
|
|
|
|
Top | #5 |
|
*
Joined: December 2001
Location: USA
Posts: 6,490
Reputation: 2808
Power: 217 |
Your question does not make any sense.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| running a batch file from a command window launched from a c sharp program | Complete | Web Design & Coding | 1 | March 28th, 2008 12:13am |
| Utility Class | Complete | Web Design & Coding | 1 | September 11th, 2007 11:59am |
| Textures inexplicably became less sharp | Petros | Graphics Cards | 0 | March 25th, 2006 4:57am |
| CSS class? | Nismo83 | Web Design & Coding | 2 | October 22nd, 2002 3:40am |