Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

static help... 1

Status
Not open for further replies.

ghesse

Programmer
Jun 16, 2003
51
US
Hello,

I have a problem, how do I access static variables outside of a class? Here's some sample code:

public class Resource
{
public static string var1;
public static string var2;

public Resource()
{
Resource.var1 = "Hello";
Resource.var2 = "World";
}
}

Now when I create an instance of it in another file:

Resource r = new Resource();

I can't do this:

Label1.Text = Resource.var1;

so...smart people out there, let me know how I access these static variables please!

Thanks,
ghesse
 
You are on the right track, but you don't need a constructor for a class containing only static members... just initialize them in the declarations or create a static init method.

Code:
using System;

namespace Generic
{
  public class Resource_1
  {
    // initialized in declaration
    public static string Var1 = "Hello";
    public static string Var2 = "World";
  }
	
  public class Resource_2
  {
    // initialized through static method
    public static string Var1;
    public static string Var2;
  
    public static void init()
    {
      Var1 = "Hello";
      Var2 = "World";
    }
  }
	
  public class Driver
  {
    public static void Main( string[] args )
    {
      Console.WriteLine( "Using static methods from Resource_1:" );
      Console.WriteLine( "{0} {1}\n", Resource_1.Var1, Resource_1.Var2 );
	    
      Console.WriteLine( "Using static methods from Resource_2:" );
  
      // must initialize first
      Resource_2.init();
      Console.WriteLine( "{0} {1}\n", Resource_2.Var1, Resource_2.Var2 );
    }
  }

}

Both have the same output:
Using static methods from Resource_1:
Hello World

Using static methods from Resource_2:
Hello World
 
You no need an instance of the Resource class in order to access a static member.
The following is valid in any another class that knows the Resource class without have an instance of the Resource class:
Label1.Text = Resource.var1;
But the next is not valid (this pointer cannot be passed)
Label1.Text = r.var1; // it is not valid

-obislavu-

 
ghesse,
I don't see anything wrong with your code. As far as my knowledge goes, the only reason why this might not work is if your Resource class lives in a different namespace where the code that wants to use it is and you don't have the appropriate references. In this case, you wouldn't be able to instanciate the class at all, and to fix the problem, you would have to add a reference to the assembly where your Resource class is. According to your sample code, however, this is not your case as it looks like you're able to instanciate the class, and it appears that the problem is just using its static fields. I tried your code and it works without problems.

Now, are you getting any error messages? Can you instanciate the class? Are the Resource class and the class using it in the same namespace? If not, do you have a reference to the namespace where the Resource class is? Are you using the appropriate "using" directives?

Jose
 
thanks guys, got it up and running now!

-ghesse
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top