473,795 Members | 3,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CType Interfering in Reference assignment?

Hello all,

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

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

If Not CheckFormOpen(o bjFormToOpen) Then
Return
End If

objFormToOpen =
CType(Activator .CreateInstance (Type.GetType(T ypeToCreate)), Form)
objFormToOpen.M diParent = Me
objFormToOpen.S how()

End Sub

Private Function CheckFormOpen(B yVal objFrm As Form) As Boolean

If Array.IndexOf(M e.MdiChildren, objFrm) <> -1 Then
MessageBox.Show ("Table already open.", "", MessageBoxButto ns.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.Accoun ting.PayrollTra nslator.frmAcco unts")
the opened form reference is passed back to m_objfrmAccount s.

But if I call it like this:
OpenChildForm(C Type(m_objfrmAc counts, frmAccounts),
"CoairBR.Accoun ting.PayrollTra nslator.frmAcco unts")
m_objfrmAccount s is nothing, even after the form opens. No reference is
passed back to m_objfrmAccount s, 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.Accoun ting.PayrollTra nslator.frmAcco unts")
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 2053
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_objfrm Accounts, frmAccounts), ...), the use of
CType() makes it into an expression and so, within OpenChildForm() ,
objFormToOpen doesn't refer to m_objfrmAccount s 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_objfrmAccoun ts, frmAccounts),
"CoairBR.Accoun ting.PayrollTra nslator.frmAcco unts") 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******** ******@TK2MSFTN GP10.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_objfrm Accounts, frmAccounts), ...), the use of CType() makes it into an expression and so, within OpenChildForm() ,
objFormToOpen doesn't refer to m_objfrmAccount s 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_objfrmAccoun ts, frmAccounts),
"CoairBR.Accoun ting.PayrollTra nslator.frmAcco unts") 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.G etType Is oFormType Then
'Bring it to the front.
frmChild.Activa te
Return frmChild
End If
Next

'Not found. Let's create it.
frmChild= DirectCast (Activator.Crea teInstance (oFormType), Form)
frmChild.MdiPar ent = 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
28126
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
6574
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 inherits from clsA
12
5410
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 problems. For e.g.: navigator.stuff = 5;
11
1648
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: " System.InvalidCastException: Specified cast is not valid. " Why is that?
3
21424
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
3840
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 absolutely no added functionality to it.
6
3552
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 base class that is common to many other classes. public class Base .... end class I have 2 seperate classes that inherit from base
4
1679
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
12413
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
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10215
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.