Hello All,
I am really stuck here and I could use some help in figuring out why this code does not work in .Net. I am converting/rewriting a VB6 app. The VB6 code does what it is supposed to do. The .Net code is syntatically correct as far as I can see but does not function as expected. What is supposed to be happening is that clsApp.Mainform is supposed to be getting set to frmMain which is the central form of the application, thus allowing me to manipulate frmMain from within my application by accessing clsApp.Mainform. What happens instead is that clsApp.Mainform is set to a generic form object and does not have any reference to frmMain at all. I cannot access any of frmMain's controls or procedures. This object assignment is simple enough and certainly appears as though it should work but it does not. Would someone please be kind enough to explain why this code does not produce the expected result, it is driving me insane. Thanks In Advance.
V. Shane Curtis
This code is from a code module in VB6.
Sub Main()
ChDir App.Path
clsApp.GetProgramSettings
Set clsApp.MainForm = frmMain
clsApp.MainForm.Caption = App.ProductName
clsApp.MainForm.Show
Exit Sub
End Sub
This code is from a class module in VB6.
Dim frmMainForm As MDIForm
Public Property Set MainForm(ByVal vData As MDIForm)
If Not vData Is Nothing Then
Set frmMainForm = vData
End If
End Property
Public Property Get MainForm() As MDIForm
If frmMainForm Is Nothing Then
Set MainForm = New frmMainForm
End If
Set MainForm = frmMainForm
End Property
This code is from a code module in .Net 2005
Sub Main()ChDir(
My.Application.Info.DirectoryPath)clsApp.GetSettings()
clsApp.MainForm = frmMain
clsApp.MainForm.Text =
My.Application.Info.ProductNameclsApp.MainForm.Show()
End SubThis code is from a class module in .Net 2005
Private frmMainForm As System.Windows.Forms.Form
Public Property MainForm() As System.Windows.Forms.Form Get If frmMainForm Is Nothing ThenMainForm = frmMainForm
End IfMainForm = frmMainForm
End Get Set(ByVal Value As System.Windows.Forms.Form) If Not Value Is Nothing ThenfrmMainForm = Value
End If End Set End Property
Object Reference Problem, Why doesn't this work?
soumya30680
Project one contains a reference to project two. Project one contains the forms, project two contains the class modules that comprise the object model. You cannot have project two reference project one this creates a circular reference and is not allowed.
f a c
Sure - send it to christian dot graus at dytech dot com dot au. If you can send it in the next few minutes, I'll get it before I go home, and look at it tonight. Otherwise, I'll look at it in the morning. I'm in Australia, and it's quitting time !!!
KP201
Having done some experimenting, I've found that if you upcast an object that's been downcast, it's properties are restored. So, that being the case, your only problem is that you're returning a Form from your property, so a Form is what you get. Casting it to the type of your form should give you the properties you're missing. You're missing them because you are throwing them away from the interface that is returned, but they are still part of the object, if invisible. If you can't create a reference between the projects, I'd recommend putting the form class in a new, third class library and then importing that into both of your existing projects.
JuanManuel
If MDIForm is a base class, and not the name of your form class, this is the problem, your class instance is being downcast.
adamw2
You don't have access to the source code for both projects If one project contains a reference to the other, there's no reason you can't create instances of types from the other project.
Babbage
Sure, you are most welcome to leave the thread open. Probably you might get few more responses which might resolve your problem.
Thank you,
Bhanu.
CraigInGeorgia
Hi Curtis,
From both [Christian and You] of your replies, we feel that you guys are interacting directly in mails and taking this issue offline.If so, Shane , can you please mark it as answered by clicking on "Mark as Answer" If you still couldnot resolve this with Christian then you can re-open this thread by clicking on "Unmark as Answer" for the same reply. This will avoid confusion for other members on whether your post is already answered or not.
Thank you,
Bhanu.
Shabbar Husain
I really appreciate you taking the time to help me sort this mess out firstly. From my point of view the property procedure isn't returning anything. The property is being set not returned and it is being set to reference frmMain. I see the point about the casting issue. I had the thing partially working at one point, the app knew that clsApp.MainForm was referencing frmMain and allowed me to access it corectly. But it produced other problems. I have been trying to remember what I did to make it work that far, but I can't remember. Is there any chance that I could send you the solution file so you can understand the problem as a whole and offer some ideas
Thanks
Team Build
The issue is not solved, and I am still seeking advice from anyone who may have any ideas. Yes I am conversing with Christian via e-mail. My e-mails are not causing unnecessary traffic in this forum. I perfer to leave the thread open, if you please.
Best Regards,
V. Shane Curtis
DrNorm
Thank you for your response to my post. In the VB6 app and in the .Net app frmMain is a MDIParent form. VB6 made the distinction .Net does not appear to. This really should not matter from what I can see. All the code is doing is setting clsApp.Mainform to a reference of frmMain. If this is done correctly then I should be able to access frmMain's controls and functions. Instead what I get is a reference to a generic form object. The code functions correctly in VB6 so it should work in .Net. I am very puzzled as to why something so simple does not work. What do I need to do to the code to make it function as it should
The application is a multi-project solution. Project one is a Win Forms EXE and project two is a class library. Project two is a comprised of several class modlues organized to form an object model with Application being it's root. Project one accesses this object model by declaring a single variable of type Application. From there the object model is navigated using standard dot notation as in clsApp.Database.InitDB. As part of the process an object reference is created within the object model to frmMain allowing for frmMain's properties and controls to be accessed from within the object model.
Hopefully that explanation clarifies what I am trying to accomplish. Thanks again for your help.
Regards,
V. Shane Curtis
wgp
// The code functions correctly in VB6 so it should work in .Net.
No, that is so wrong. VB6 and VB.NET are worlds apart.
// What do I need to do to the code to make it function as it should
Change the property and private variable to be an instance of the actual form class that you're passing around, not a base class.
IMO, your whole model is a disaster - global variables are nearly always a sign of bad design. Given that your form is going to be running all the time, any communication between the form and other classes should really be changed to occur via delegates, so that what is exposed is rigidly defined, and decoupled from the class structure. But, I doubt you have time to worry about that... :P
James Huang
Thanks Christian for your time and patience. I appreciate it very much. I just send it to your e-mail in a zip file.
Thanks Again.
V. Shane Curtis
Zhouweidi
Thanks for your response. Firstly, I understand that .Net and VB6 are totally different beasts. But the base concept of what I am doing is simple, setting clsApp.MainForm to reference frmMain. If you read the above post you will see that what you propose that I do is not possible because frmMain is not in the same project as the class module Application. I should be able to set clsApp.Mainform to the object reference of frmMain from within Sub Main which is located in the same project as frmMain. That is all I am really trying to do.
Thanks for your help.
Regards,
V. Shane Curtis