reuse object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

this is probably a newbie question par excellence. Would appreciate help
nonetheless. I have just started with C# and ran into a problem.

I have created a class with several properties and methods. In one of the
methods (main actually) I create an instance of a special log file class.
This object has a lot of parameters I would like to set only once.

Now I want to use this log-file object in the other methods, too. But VS
complains that the name were not known in the current context. So apparently
the scope of this object is limited to the one method, not to the class.

Is there a way to create an object that can be used throughout a whole class?

I read something about singletons, but cannot say it really helped me a lot.

Thanks

Henrik
 
Henrick,

If you want to use it throughout an ^instance^ of a class, then you
would store the reference to the object in a field of the class.

If you want to share the reference among ^all^ instances (and possibly
outside of the class, depending on the visibility of the field) and to
static members as well (which is what your Main method is), then you should
create a static field to hold the reference to the object.

Hope this helps.
 

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