473,395 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

CType Interfering in Reference assignment?

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(TypeTo Create)), 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

Nov 20 '05 #1
3 2029
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
Nov 20 '05 #2
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

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:ug**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #3
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>
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Riccardo Rossi | last post by:
Hi all! How does Python pass arguments to a function? By value or by reference? Thanks, Riccardo Rossi.
4
by: Joeri | last post by:
Hi, I'm struggling with this issue, maybe someone has already solved ik before. Here's the problem I Have : clsA witch is a base class clsB inherits from clsA clsC inherits from clsA clsD...
12
by: marcadonis | last post by:
Hi! Does anybody know of a way that I can keep a reference to an object that I can then reuse? I tried various approaches using navigator, but these all fail in an iframe due to premission...
11
by: tshad | last post by:
I have the following: sub submitQuestion_click(Sender as Object, e as EventArgs) Dim submitButton as Button = CType(sender,Button) This gives me an error that says: "...
3
by: Mark Kamoski | last post by:
Hi-- What is the difference between Convert.ToString(obj) and CType(obj, String)? (Assume obj is a variable of type Object.) Please advise. Thank you.
4
by: Mike Cooper | last post by:
There is something about inherited classes I evidently don't know... I wrote the following class: Class Class1 inherits System.Windows.Forms.DataGridTextBoxColumn End Class There is...
6
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a...
4
by: siddhu | last post by:
If there is reference member variable in the class, why doesn't default assignment operator work? class A { int& i; public: A( int& ii):i(ii){} //A& operator=(const A& a){i = a.i;} };
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.