Hi Folks,

A couple of questions...

If in a class I have a main which is obviously static, do I have to make every method in the same class static as well? What does static actually mean and how does it affect the program?

Also..

When given a certain method return type, say, int or string - What does the compiler expect to see within the method? Something like:

string name = "Usmaan";
return name; // COMMENT -> You can correct me if i'm wrong.

--------------------------------------------------------------

Any help is really appreciated - Thank you in advance.

Usmaan~

ddanbe commented: Good question and well posed. :) +7

Recommended Answers

All 2 Replies

regarding your question of Static, visit below link..
http://bytes.com/topic/c-sharp/answers/462233-what-use-static-method


Hi Folks,

A couple of questions...

If in a class I have a main which is obviously static, do I have to make every method in the same class static as well? What does static actually mean and how does it affect the program?

Also..

When given a certain method return type, say, int or string - What does the compiler expect to see within the method? Something like:

string name = "Usmaan";
return name; // COMMENT -> You can correct me if i'm wrong.

--------------------------------------------------------------

Any help is really appreciated - Thank you in advance.

Usmaan~

A static member exists even when the class has not been instantiated.
For instance:

class myClass
    {

        public myClass()
        {
            myString = "Not Static";
        }

        public static string getStaticString()
        {
            return "Static";
        }

        private string myString;

        public string getString()
        {
            return myString;
        }
    }

    public partial class Form1 : Form
    {
        private void button1_Click(object sender, EventArgs e)
        {
            string staticString = myClass.getStaticString();

            myClass instanceA = new myClass();
            myClass instanceB = new myClass();

            string notStaticA = instanceA.getString();
            string notStaticB = instanceB.getString();
        }

Lets examine the class first:
It has a static method called getStaticString. This method is accessible directly through the class without it being instantiated. This can be seen when we call string staticString = myClass.getStaticString(); . We haven't created an instance of the class, we are calling the static method of the class itself.
The class also has a non static method. In order to access this method we need an instance of the class. So we call myClass instanceA = new myClass(); which creates an instance of myClass in memory and allows us to access all of the members of that instance. In the classes constructor i have set the value of the non static string to be returned.
In my example i have two instances; instanceA and instanceB. In my example they are identical. For a second, imagine that the value of instanceB.myString was changed. We now have two instances of the same class which each contain different values for myString. So instanceA.myString and instanceB.myString are different.

Now lets look at why static methods cannot interact with nonstatic directly:

public static string getStaticString()
        {
            return myString;
        }

It wont compile, but imagine for a second it did and we ran the altered section above. When you call myClass.getStaticString() what should it return? If you haven't created an instance of the class yet then there is no such thing as myString in memory. If you have created 2 or 3 or more instances of myClass, which version do you return? This is what is meant by the compiler error "An Object reference is required for the non-static field, method or property"; the call needs to know which object you want the value to come from.

So in summary, you don't need to make every method static, but you DO need to instantiate an object that contains the methods you wish to run if they are non-static.

Lastly, and very quickly, i used both of the return styles in my code to show you how they can be used. If the method has a return type then every path of execution must result in a value of the correct type being returned. You can either create a variable to store a value and return the variable or you can return a value directly. It generally depends on preference and circumstance which you use :)

Sorry for looong post, hope it has helped

commented: You explained it well :) +7
commented: Good one! +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.