using members of a C Sharp class

Complete

OSNN Addict
Joined
25 Aug 2005
Messages
94
In C Sharp, just like in C++ you can have classes with "methods" and "members" but the syntax is different.

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.
 

X-Istence

*
Political Access
Joined
5 Dec 2001
Messages
6,498
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.
 

Complete

OSNN Addict
Joined
25 Aug 2005
Messages
94
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.

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"
 

Complete

OSNN Addict
Joined
25 Aug 2005
Messages
94
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);
        }
    }
}
suppose I wanted to make use of the "myClass" defined in the Main routine
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();    
    }
}
but this leads me to this question. How would the code, access the "program" variable?
 

X-Istence

*
Political Access
Joined
5 Dec 2001
Messages
6,498
Your question does not make any sense.

but this leads me to this question. How would the code, access the "program" variable?

The program variable is instantiated. You can use it wherever you want as long as it is in scope.
 

Members online

No members online now.

Latest profile posts

Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.
Terrahertz wrote on Electronic Punk's profile.
Yo fellas!
Electronic Punk wrote on Sazar's profile.
Where are you buddy?
Perris Calderon wrote on Electronic Punk's profile.
Hey EP! All good with me, applying for Microsoft MVP right now, should have done this a while ago.

Notifications don't work, I only found your response by coming back to hunt up some threads, if you want, give me your email address so we can keep in touch easier!
Perris Calderon wrote on Electronic Punk's profile.
EP, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there!

Forum statistics

Threads
61,997
Messages
673,411
Members
5,590
Latest member
AntonioPR