static function question

  • Thread starter Thread starter juli jul
  • Start date Start date
J

juli jul

Hello!
If I want to pass some class variable from one class to another : how
can I do it with the static function?
I tried but I can't see the class variables in the static function I
made-are there some other methods to do it?
Thank you!
 
Hi,

Can you post the code, remember that you should call the object using the
class name and not the instance to access the static ones:

public class MyStaticClass
{
public static int StaticVar;
public int InstanceVar;
}

// To access the static one
MyStaticClass.StaticVar;

//To access the instance one
MyStaticClass MyInstance = new MyStaticClass();
MyInstance.InstanceVar;

Cheers
Salva
 
Hi,

Are you refering to how to pass INSTANCE variables from one instance to
other?

I think that you need to post some code to make it clear what you want.
If you want to have access to instance variables from inside a static method
you have to pass those variables as parameters.


cheers,
 
If I want to pass some class variable from one class to another : how
can I do it with the static function?
I tried but I can't see the class variables in the static function I
made-are there some other methods to do it?
Can you explain more in detail what you want?

Static functions are normally used to perform some operation without need of
the class to be instantiated because the static function does not use the
this pointer or any variable defined in the class. Basically you could see
these as the old style C functions.

Static variables are use to be used like global variables. This way you can
create multiple instances of this class, but they all point to that one
static variable.

So technically you rarely use static methods or variables.
Basically if you wish to pass on variables form one class to another class
then just assign them.

CClassA A = new CClassA()
CClassB B = new CClassA()

B.MyVariable=A.MyVariable.

If A.MyVariable is a basic type like int, float, then a copy is made.
If A.MyVariable is another instantiated class then the reference pointer is
copied, so both MyVariable wil point to the same instatiated object.

If A.MyVariable is another instantiated class but you do not want to have a
copy of that object and not the reference to that object then you do this:

B.MyVariable=new CMyVariable(A.MyVariable).

I hope this make sense?
 
Back
Top