"Global" objects

B

BobRoyAce

I posted the post below in another newsgroup and it has been suggested
that this may be a better place to post it. See below:

---ORIGINAL POST BEGINS---
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 object which
corresponds
to 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 other 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.)?
 
C

Cor Ligthert [MVP]

Bob,

You can use a shared class or a module (which is a shared class with less
documentive value).

It this is not what you want or if you don't know how, than please reply.

Cor
 
B

BobRoyAce

I don't know if that's what I need, but sounds like I would want one of
them. I don't know how to do either, though, so would appreciate
guidance including steps and code samples if at all possible (brief, of
course). I am brand new to VB.NET and am using Visual Studio 2005.
 
B

BobRoyAce

I don't know if that's what I need, but sounds like I would want one of
them. I don't know how to do either, though, so would appreciate
guidance including steps and code samples if at all possible (brief, of
course). I am brand new to VB.NET and am using Visual Studio 2005.
 
C

Cor Ligthert [MVP]

Bob,

You would not place values or objects that is for your complete project in
one UI (form)class as all classes could use it.

Therefore is the Shared Class (in fact it is placed on the mainstack of the
resulting endprogram, and therfore consumes constantly memory). In C
languages the name for this was Static, this word however does not so good
reflect the meaning, because the contents of everything is variable.
Therefore in VBNet it is Shared.

You can make this class as by instance below. (Just typed in so watch typos)

Public Shared Class TheClass 'MyClass is a special word
Private mTheField as String
Public Property TheField as String
Get
Retunr mField
End Get
Set ............. I forever forget this text it is autotyped
mField = value
End Set
End Class

This you can use in everywhere in your project by instance the mainfrm

me.Textbox1.text = TheClass.TheField

I hope this helps,

Cor
 

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