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

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
 
T

Tom Porterfield

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.
 
L

Larry

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top