static keyword

  • Thread starter Thread starter josh
  • Start date Start date
J

josh

Hi, if I want to use these following code I got a compiler error:

public Log()
{
Form1 f = new Form1();

static printLogTo()
{
f.textEdit1.Text = "some error...";
}
}

I understand that I cannot use f as when I call Log.printLog() f has
not been created so f has not an instance of
its class.

So how can I instantiate f for using in my static method?

P.S.
In Java I can do:

static
{
// some instructions will be executed here...
}
 
Hi, if I want to use these following code I got a compiler error:

public Log()
{
Form1 f = new Form1();

static printLogTo()
{
f.textEdit1.Text = "some error...";
}

}

I understand that I cannot use f as when I call Log.printLog() f has
not been created so f has not an instance of
its class.

Well, it's not so much that - it's that f belongs to an instance of
Log, and from within printLogTo there's no instance involved.
So how can I instantiate f for using in my static method?

Creating a form when and only when you first log seems very odd to me.
P.S.
In Java I can do:

static
{
// some instructions will be executed here...

}

The equivalent is a static constructor in C# - but I'm not sure that's
really the best approach here.

Jon
 
Josh,

You just declare f as static as well, or you pass f to the static
method. Either way, f is an instance field. If you change it to a static
field, then remember, all instances of the Log class will have the same
instance of f.
 
Hi,


You declare f as static too.

A word of advice though, unless Form1 has a REALLY compeling need to be
static I suggest you not doing it. You can get a wierd behavior with
parenting/etc.
 
sorry I don't understand. When I call Log.methods() there is no
instance of the Log class...
so what << all instances of the Log class will have the same instance
of f >> ?

please, make me an example.

Thanks
 
josh said:
sorry I don't understand. When I call Log.methods() there is no
instance of the Log class...
so what << all instances of the Log class will have the same instance
of f >> ?

It's not that each instance of the Log class will have the same
instance of f - it's that the variable f is independent of *any*
instances of Log. It's associated with the Log type rather than any
instance of Log.
please, make me an example.

Unfortunately this kind of thing isn't best discussed on a newsgroup -
it's best understood by reading a book. I will say that it's exactly
the same situation you'd have in Java though, so if you're familiar
with Java there shouldn't be much of a leap.
 
josh said:
In Java I can do:

static
{
// some instructions will be executed here...
}

In C# it is:

static MyClass()
{
// some instructions will be executed here...
}

Arne
 

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

Back
Top