CType Interfering in Reference assignment?

G

Giovanni Bassi

Hello all,

I have prepared a simple procedure to open forms only once. Here it is:


-//-

Private Sub OpenChildForm(ByRef objFormToOpen As
System.Windows.Forms.Form, ByVal TypeToCreate As String)

If Not CheckFormOpen(objFormToOpen) Then
Return
End If

objFormToOpen =
CType(Activator.CreateInstance(Type.GetType(TypeToCreate)), Form)
objFormToOpen.MdiParent = Me
objFormToOpen.Show()

End Sub

Private Function CheckFormOpen(ByVal objFrm As Form) As Boolean

If Array.IndexOf(Me.MdiChildren, objFrm) <> -1 Then
MessageBox.Show("Table already open.", "", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Return False
Else
Return True
End If

End Function



-//-



The problem is:

When I call it like this:
OpenChildForm(m_objfrmAccounts,
"CoairBR.Accounting.PayrollTranslator.frmAccounts")
the opened form reference is passed back to m_objfrmAccounts.

But if I call it like this:
OpenChildForm(CType(m_objfrmAccounts, frmAccounts),
"CoairBR.Accounting.PayrollTranslator.frmAccounts")
m_objfrmAccounts is nothing, even after the form opens. No reference is
passed back to m_objfrmAccounts, even though I am calling OpenChildForm with
the objFormToOpen argument passed by reference.

I am using Option Strict On, but I can't use it On anymore, otherwise:
OpenChildForm(m_objfrmAccounts,
"CoairBR.Accounting.PayrollTranslator.frmAccounts")
won't compile, as I need an explicit conversion from m_frmAccounts to a
System.Windows.Forms.Form.
By the way, I don't know if it matters, but frmAccounts is derived from
another form using visual inheritance and not directly from Form.

What do I do? Why is CType keeping the reference value from being set?

Thanks!

Giovanni Bassi
 
F

Fergus Cooney

Hi Giovanni,

ByRef creates a reference to the variable being passed into a method so
that if you change it within the method, the variable that it was called with
will change too. You know this, which is why you used it.

However, when you use an expression instead of a variable, the reference
passed into the method is a reference, not to the variable, but to a temporary
location where the result of the expression is stored.

In OpenChildForm (CType(m_objfrmAccounts, frmAccounts), ...), the use of
CType() makes it into an expression and so, within OpenChildForm(),
objFormToOpen doesn't refer to m_objfrmAccounts but to the expression.

What you'll have to do is change OpenChildForm so that it returns the
child Form rather than setting a passed-in variable. Make the return type of
OpenChildForm be the highest class that is common to all the possible child
Forms. Then cast it to the correct type when assigning the result.

|| By the way, I don't know if it matters, but frmAccounts is
|| derived from another form using visual inheritance and not
|| directly from Form.

As a general point, that's fine - as long as there's a Form somewhere down
the chain of inheritance (which there will be).

I'm curious. Was OpenChildForm (CType (m_objfrmAccounts, frmAccounts),
"CoairBR.Accounting.PayrollTranslator.frmAccounts") just an example or do you
have these calls hard-coded in this way? Are they from a list or something?

Regards,
Fergus
 
G

Giovanni Bassi

Hey Fergus,

Thanks for the explanation! I was not aware of this temporary location
behavior.
These calls are being made from inside click menu calls. As all of them will
need to check if the form is open and perform some other things too (as seen
on the first post) I decided to create a little procedure to handle all the
work. Why are you curious? Is that strange in any way?

Regards,

Giovanni Bassi
 
F

Fergus Cooney

Hi Giovanni,

Last night I answered your query with most of my attention on explaining
ByRef. I've been wracking my brains all day trying to remember what I was
thinking last night about OpenChildForm and why I asked you about how you were
using it. Now I've remembered. :)

I noticed a couple of things about your routine.

objFormToOpen will be Nothing when you call OpenChildForm, so when
CheckFormOpen tries to find it in the list, it won't be there and the result
will always be False. In order to check that the Form is in the list you need
the form.

The other thing I noticed was that OpenChildForm takes an object of the
required type as well as a string giving the name of the type. In other words
objFormToOpen and TypeToCreate share something in common which suggests that
one of them can go.

Finally, and this one was really bugging me, I remembered a previous
thread where this same issue had been explored - but I couldn't find it. Well
I have, just now, and in that thread we developed a version of Sub
OpenChildForm. So below is that Sub converted to a Function for you. :)

You'll notice that it uses the Type of the Form for checking and creating
rather than the name or an instance. Also, if the child Form is already open,
there is no message 'telling the User off' ** but just the activation of that
Form.

All the best,
Fergus

** One of my programming principles is to avoid telling a User off for having
done something wrong. Rather they should be prevented from doing it in the
first place. So if a menu item shouldn't be applicable, it gets disabled
instead of being left for the User to click with a resulting "Hands off!"
message.

<code>
Function OpenChildForm (oFormType As Type) As Form
Dim frmChild As Form

'The form may already exist.
For Each frmChild In Me.MdiChildren
If frmChild.Name.GetType Is oFormType Then
'Bring it to the front.
frmChild.Activate
Return frmChild
End If
Next

'Not found. Let's create it.
frmChild= DirectCast (Activator.CreateInstance (oFormType), Form)
frmChild.MdiParent = Me
frmChild.Show()

Return frmChild
End Function
</code>
 

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