Global objects

B

BobRoyAce

I have an application which is comprised of, among many other things, a
Main form which shows various user controls depending on which NavBar
link they click on. In addition, there are many Entity classes that I
created to support the application, one of which happens to be called
User. In my main form I currently instantiate a User which corresponds
with the user who logged in. The User object that is created
(g_oCurrentUser) is declared as Private in my Main form's code, but
there is a Public function (GetCurrentUser) that returns a pointer to
g_oCurrentUser.

My question is "Is there a way for the entity classes to "get at" the
g_oCurrentUser object from the main form? The reason I need to do this
is because I want to pull the current user's ID to use for populating
the fkLastUpdatedByUserID field of the entities when they have been
changed by the current user.

Is there a way to do this? I guess my question, more generally, is how
can I have Object variables that are accessible both to the Main form
and to other parts of the system (e.g. user controls that will be
created later and shown on the form, other Objects, modules, etc.)?
 
J

Joanna Carter [TeamB]

"BobRoyAce" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Is there a way to do this? I guess my question, more generally, is how
| can I have Object variables that are accessible both to the Main form
| and to other parts of the system (e.g. user controls that will be
| created later and shown on the form, other Objects, modules, etc.)?

The answer is similar to that for your other question about a login form.

Don't rely on your main form as a place to do anyhting apart from manage
your main form. Create "global" objects in your program code module; you
will then need to, either pass the object to any forms that you create or
add static properties/methods to a non-instantiable class that is visible
throughout your application.

Joanna
 
B

BobRoyAce

Thanks Joanna...

Which program code module do I create these "global" objects in? How do
I declare them? When and how does the code that instantiates them get
executed? How do I "pass the object" to any forms, etc.? Could you post
a very simple code sample?
 
J

Joanna Carter [TeamB]

"BobRoyAce" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Which program code module do I create these "global" objects in? How do
| I declare them? When and how does the code that instantiates them get
| executed? How do I "pass the object" to any forms, etc.? Could you post
| a very simple code sample?

Create yourself a new code module and give it the same namespace as the rest
of your application.

// Globals.cs

namespace MyCompany.MyProject
{
public static class Globals
{
private MyClass myObject = new MyClass();

public static MyClass MyObject
{
get { return myObject; }
set { myObject = value; }
}
}
}

This will be accessible from any other module in your project.

{
MyClass test = Globals.MyObject;
...
// or
...
Globals.MyObject.SomeProperty = someValue;
Globals.MyObject.SomeMethod();
... // etc
}

Joanna
 
B

BobRoyAce

Thanks Joanna...

I don't seem to be able to use the Static keyword in VB. Is there an
equivalent way to declare the class as Static in VB?

Also, by the way, would this be the same place that I would put the
Main procedure you mentioned in your answer to my Login post? What
happens if I have a Main procedure in my Main form as well?
 
J

Joanna Carter [TeamB]

"BobRoyAce" <[email protected]> a écrit dans le message de (e-mail address removed)...

| I don't seem to be able to use the Static keyword in VB. Is there an
| equivalent way to declare the class as Static in VB?

I believe the VB equivalent of static is shared.

As to whether you can have a static class in VB, I am sorry, I don't know
how to use VB at all well. If you cannot have a shared class, then you need
to have a Sigleton class with a hidden constructor and an instance method.
If you need to know how to do this, then you would be better asking in a VB
language group.

| Also, by the way, would this be the same place that I would put the
| Main procedure you mentioned in your answer to my Login post? What
| happens if I have a Main procedure in my Main form as well?

You really shouldn't have a Main procedure in a form class, it should be in
a separate module. I would create a separate module for the Main and another
for the globals.

Joanna
 

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