C# / ASP.NET Question: how to use a class variable without initiate

  • Thread starter Thread starter Larry
  • Start date Start date
L

Larry

how to use a class variable without initiate



for example:



ClassA { int var1; }



ClassB{

ClassA aa = new ClassA(); // omit this

aa.var1 = 1; // call this variable directly

}



I am thinking I may try something like this:

ClassA::var1 = 1;



hope you can make me clear.



thanks

larry
 
how to use a class variable without initiate

for example:

ClassA { int var1; }

ClassB{

ClassA aa = new ClassA(); // omit this

aa.var1 = 1; // call this variable directly

}

I am thinking I may try something like this:

ClassA::var1 = 1;

hope you can make me clear.

thanks

larry

Make var1 static and you can access as you have described. Ex:

class ClassA
{
public static int var1;
}

class ClassB
{
public void DoSomethingWithClassA()
{
ClassA.var1 = 1;
}
}

Read up on the static keyword to understand the implications of this.
 
thanks a lot for all of you.

Tom Porterfield said:
Make var1 static and you can access as you have described. Ex:

class ClassA
{
public static int var1;
}

class ClassB
{
public void DoSomethingWithClassA()
{
ClassA.var1 = 1;
}
}

Read up on the static keyword to understand the implications of this.
 
Back
Top