Re: "Global" objects by Kevin
Kevin
Fri Jun 09 15:17:51 CDT 2006
Hi BobRoyAce,
>I thought it was clear what the code was doing...at least clear enough.
Well, yes, as much as you posted.
> The Main form is declaring an object (doesn't really matter what it
> does for purposes of this discussion) and a public method to return it.
I followed that part.
> In the code in the UserControl I showed, it is merely trying to set a
> reference to that Main form's object by calling the method which
> returns it. So, my question is what am I doing wrong that is resulting
> in New method of the Main form being called again when I do this?
That is the part I could not answer. However, after going over your previous
message, I think I may have found a clue, but I'm not sure:
> In MainForm.vb (for the form I named frmMain in the designer) I have
This remark was somewhat ambiguous, as you mentioned the name of the file
was "MainForm.vb" and added that "...I named frmMain in the designer"). The
reference to "the designer" was the ambiguous part, as your project probably
has many Designers in it, and you could have been talking about the Designer
for your UserControl class. After looking at that line a couple of times, I
suspect that you named the Form *class* "frmMain." Would that be correct?
If so, this could well be the cause of your app's malfunction. I can't tell
you why it misbehaved in the *way* that you described, but I *can* tell you
why it would misbehave.
> In the New method of the User Control, let's say, frmEdit, I have code
> as follows:
> m_oStreetAddressCleaner = frmMain.GetStreetAddressCleaner
The short explanation is that you referred to the class name, not the
instance of the class. There is a huge distinction between a class and an
instance of a class. A class is a definition of what a class is. There can
be many instances of the class. An instance of a class is just a sort of
"copy" of it (it's a bit more complicated than that, but let's get the basic
stuff straightened out first). As an example, consider the following (VB)
code:
Public Sub MySub()
Dim btnA As New Button()
Dim btnB As New Button()
Dim btnC As New Button()
btnA.Text = "Button A"
btnB.Text = "Button B"
btnC.Text = "Button C"
End Sub
Note that each Button is a separate instance of the Button Class, and each
one has all the characteristics of Button, but is different, in that it has
a different Text property. Also note that you do not reference any of them
by the class name "Button."
If you did this with your Form, that would certainly cause something bad to
happen. What, I cannot say, but apparently, it caused the behavior you
observed.
Now, the rest of what I wrote was along the lines of some general good
advice which would prevent such things from happening, since I didn't see in
your message exactly what was going wrong, but did see some problematic
perceptions of things. Let me reiterate one important point all by itself:
"My point here is that there is no difference whether Visual Studio writes
the code or you do.
The rules for the code are still the same."
Now, your UserControl (I hope you didn't name it "frmEdit" because a
UserControl is *not* a Form, but the name would seem to imply that it is,
which can make things confusing later on) is just like any other Control in
the Form, just like any Control that is added to the Form in the
InitializeComponent method. The only difference is *when* it is added. Other
than that, it should be added to the Controls Collection of the Container
you want to place it in, just exactly in the same way that other Controls
are added to other Container Controls in the InitializeComponent method. My
point was (and I have done this myself before as a form of self-education),
if you look at the InitializeComponent method, you will see all of the steps
necessary to add it correctly, without causing any unforseen problems (such
as memory leaks from improper disposal), as well as seeing all of the
Microsoft Best Practices in action. It's an opportunity to learn from the
masters, and write more solid code in the process.
One final tip. Every Control has a FindForm method, which returns the
top-level Form (in your case, your instance of "frmMain") in which that
Control resides, regardless of how deeply nested it is inside any hierarchy
of Container Controls. You simply cast the return value as the type of the
Form that it is, and you can reference any public members of the form.
Here's a (VB) example:
Dim parentForm As frmMain = CType(FindForm(), frmMain)
m_oStreetAddressCleaner = parentForm.GetStreetAddressCleaner
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist
A lifetime is made up of
Lots of short moments.
"BobRoyAce" <broy@omegasoftwareinc.com> wrote in message
news:1149877666.085263.289310@y43g2000cwc.googlegroups.com...
>I thought it was clear what the code was doing...at least clear enough.
> The Main form is declaring an object (doesn't really matter what it
> does for purposes of this discussion) and a public method to return it.
> In the code in the UserControl I showed, it is merely trying to set a
> reference to that Main form's object by calling the method which
> returns it. So, my question is what am I doing wrong that is resulting
> in New method of the Main form being called again when I do this?
>
> I understand about how all the controls on the Main form are created at
> run time in the InitializeComponent method. However, the user controls
> I am talking about are NOT created there. They are later created, in
> response to a user clicking on a NavBar item, at which time they are
> shown with their Parent being set to a panel on the main form. In other
> words, these user controls are NOT placed onto the panel on the Main
> form at design time. If they were, I'm thinking the process of doing
> what I'm trying to do would be slightly different and easier.
>
> Do you understand?
>