954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

static variable and static member programs

static variable and static function programs.

Alokkumar11
Newbie Poster
8 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
 

So, what do you wanna know?
A field declared with the static modifier is called a static variable.
Static variables - if they are marked as public they are accessed without a class reference.
For intance:

class class1
{
    public static string myVar = "abc";
}

class class2
{
     public class2
     {
         string myVar2 = class1.myVar;
     }
}


What else would you like to know?

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

what is static variable and static function

Alokkumar11
Newbie Poster
8 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
 

A static variable is one that doesn't require an instance of the class to access.
A static method is one that doesn't require an instance of the class to access.

public class MyClass {
    static public int myStaticInt = 3;
    public int myNonstaticInt = 5;

    static public void MyStaticMethod() {
        Console.WriteLine(myStaticInt);
        Console.WriteLine(myNonstaticInt);  // Error! Need an instance of the class!
    }

    public void MyNonstaticMethod() {
        Console.WriteLine(myStaticInt);
        Console.WriteLine(myNonstaticInt);
    }
}
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

I do not the understand the Code . And what is use of this code "Console.WriteLine(StaticExample.ivar.ToString();

class Program
    {
        static void Main(string[] args)
        {
            StaticExample.ivar = 1;
            Console.WriteLine(StaticExample.ivar.ToString());
            Console.ReadLine();

        }
    }
    public class StaticExample
    {
        public static int ivar;
    }
Alokkumar11
Newbie Poster
8 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
 

If you want to access some non-static member inside some class FROM a static method, you need an instance of that class:

class Program
{
    private int myFiled = 1;

    static void Main(string[] args)
    {
       //You can NOT do:
       int a = myField; //myField is not accessible!!
 
       //what you have to do, is to create a new reference of the class the variable is in:
       //so:
       Program p = new Program();
       int b = p.myField; //this is OK!!
    }

     //for example, if you have a non-static class:
     private void MyMethod()
     {
          //you can access to class`s members:
          int c = myField; //this is OK!!
     }
}


The same you do if the field (or some other member) needs to be accessed in some other class.

Hope this is clear enough how to access static members. The bottom line is, that you canNOT access to class members outside of the static class WITHOUT an instance of a class (in our case, an instance isp).

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

I can't believe someone replied to the OP. That's probably the worst post I've ever seen on Daniweb.

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: