Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

This should hopefully be an easy question to answer. Basically what I want to know how to do, is how to create an object that can be accessed in any class in the project without having to actually pass it into every single class.

 

At the moment I'm creating the object in the main form and then passing that object into a class' constructer. When I have one thing that keeps getting passed into a bunch of classes it starts to get confusing. Is there something like an include file in c#.net so that I could just define it in there and use it throughout the project? Is there an easier way to do this?

Posted

But how could I access that object from anywhere in the project once it's created? Would making the object static allow me to do this?

 

Say I create a string in the main form and I have about 5 classes in the project. How could I make it so that that string could be accessed from anywhere in the project without having to pass it or it's parent into all of the classes? Is there a way to sort of do something like this:

 

Form 1:

global string gHello;
gHello = "test";

 

Class 1:

MessageBox.Show(gHello); //outputs test

 

I'm trying to ask this as simply as possible using a string. I guess I'm sort of going away from object-oriented programming. I could live without the luxury of being able to do this if it's not possible. It's just it would make things a lot easier if it is.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...