|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Type.GetType with two projectsall my base user controls, classes, etc. that are used over multiple applications. The second is my application project (OCFU) which holds the forms and code that the users run. I need to instantiate a new form only given the form's name in a string. In the calling form in the OCFU project I used the following code: Dim objNewForm As Object = Activator.CreateInstance(Type.GetType(myApplicationName & "." & FormToOpen)) Dim frm As FrontierForm = DirectCast(objNewForm, FrontierForm) The "myApplicationName & "." & FormToOpen" code fully qualifies the form. The myApplicationName is a global variable of the application name, in this case OCFU. It works when the code is in a form from within the OCFU project. I then copied this code to a class in the Frontier project so that it is reusable by multiple applications. Then I commented out the code in the OCFU form. Now it doesn't work. The Type.GetType procedure returns Nothing. It's as if it cannot find the OCFU project and the form within it. Am I missing additional qualification? Did you reference the other project?
T Paul wrote: Show quoteHide quote >I have two projects in one solution. One is called Frontier and holds >all my base user controls, classes, etc. that are used over multiple >applications. The second is my application project (OCFU) which holds >the forms and code that the users run. I need to instantiate a new >form only given the form's name in a string. In the calling form in >the OCFU project I used the following code: > >Dim objNewForm As Object = >Activator.CreateInstance(Type.GetType(myApplicationName & "." & >FormToOpen)) >Dim frm As FrontierForm = DirectCast(objNewForm, FrontierForm) > >The "myApplicationName & "." & FormToOpen" code fully qualifies the >form. The myApplicationName is a global variable of the application >name, in this case OCFU. It works when the code is in a form from >within the OCFU project. > >I then copied this code to a class in the Frontier project so that it >is reusable by multiple applications. Then I commented out the code in >the OCFU form. Now it doesn't work. The Type.GetType procedure >returns Nothing. It's as if it cannot find the OCFU project and the >form within it. Am I missing additional qualification? > > > I think the error is here
GetType(myApplicationName & "." & FormToOpen)) cuz you try to pass a String value instead of you passing an Object . Paul,
I see this the last weeks more and more in this newsgroup and I really don't understand it. What is your code better than Dim myform as new MySecondProject.Form1 (the name from the form as you use now in a string) Can you enlighten me? A second question is what do you mean by FormToOpen is that a term from old Basic? An existing Form object can be showed or an Form object can be instanced from a Class. I have the idea that you have this all from VBA and create something terrible difficult for a simple problem. However, just my idea. Cor Thanks for the responses. Let me try to explain further...
tomb: Did you reference the other project? In the code "Type.GetType(myApplicationName & "." & FormToOpen)" the variable myApplicationName is a global variable. The value of the variable is the name of the project (assembly, they are the same) that the form I want to open is housed in. TSI: I think the error is here No, that is not correct. If you just code "GetType(???)" the parameter for the GetType procedure is an object. That is what you are referring to. However, the Type.GetType function accepts a string as a parameter. Cor: I really don't understand it. I'm converting an old application. The old application dynamically generates a menuing system on the form through a table. So when the user presses a button the form they want to open will vary. The form name is read from a table and then needs to be opened. That is why I am doing this. I have multiple applications that do this and so need to get it to work. Cor: what do you mean by FormToOpen The "FormToOpen" is a passed parameter. The code I posted is contained within a Sub that is passed "FormToOpen As String". Below is the entire Sub (except for the exception handling code). You don't actually need it. Public Sub OpenForm(ByVal FormToOpen As String, Optional ByVal View As AcView = AcView.acViewNormal, Optional ByVal FilterName As Object = "", Optional ByVal WhereCondition As Object = "", Optional ByVal DataMode As AcFormOpenDataMode = AcFormOpenDataMode.acFormPropertySettings, Optional ByVal WindowMode As AcWindowMode = AcWindowMode.acWindowNormal, Optional ByVal OpenArgs As Object = "") Try Dim objNewForm As Object = Activator.CreateInstance(Type.GetType(myApplicationName & "." & FormToOpen)) Dim frm As FrontierForm = DirectCast(objNewForm, FrontierForm) RaiseEvent OpenForm_Event(frm, View, FilterName, WhereCondition, DataMode, WindowMode, OpenArgs) Catch ex As Exception ... execption handling code (not important) End Try End Sub This Sub is contained within a Class that is in the Frontier project. Frontier is a Class project that will be used by multiple other application (Window) projects. That is why I need it here. I am positive that the problem is with the code "Type.GetType(myApplicationName & "." & FormToOpen)". It returns Nothing. I can see it when I am debugging the program. The reason is because it cannot find the form. I just need to know how to qualify it more. I'm just not sure how to do it, if it's even possible. My basic question is, "From one project, how to you fully qualify a reference to a form in a second project when both projects are in the same solution?" Paul,
Show quoteHide quote > This is exactly as I thought, I would refactor it in your case, than you > Cor: what do you mean by FormToOpen > > Public Sub OpenForm(ByVal FormToOpen As String, Optional ByVal View > As AcView = AcView.acViewNormal, Optional ByVal FilterName As Object = > "", Optional ByVal WhereCondition As Object = "", Optional ByVal > DataMode As AcFormOpenDataMode = > AcFormOpenDataMode.acFormPropertySettings, Optional ByVal WindowMode As > AcWindowMode = AcWindowMode.acWindowNormal, Optional ByVal OpenArgs As > Object = "") > Try > Dim objNewForm As Object = > Activator.CreateInstance(Type.GetType(myApplicationName & "." & > FormToOpen)) > Dim frm As FrontierForm = DirectCast(objNewForm, > FrontierForm) > RaiseEvent OpenForm_Event(frm, View, FilterName, > WhereCondition, DataMode, WindowMode, OpenArgs) > Catch ex As Exception > ... execption handling code (not important) > End Try > End Sub > will see your program will be look much nicer. What is wrong with setting all your possible forms in an hashtable and tell than what class you want depending on the string, you do the show and setting all those properties than of course in the class that is calling it, and don't make this kind of modulair code. I thought this because in past the term Open a form was used, what is a long time ago when the distinct in VB between a class and an object was messed up and the difference was not good to see (this especialy with the form, therefore some of us are not so lucky with the from past reinvented "my" class). Just my thought, Cor Thanks again Cor for the quick response.
I believe the scenario I am describing is not possible. I created a small example to test it. I created a simple two form project called WindowsApplication1. Its forms are Form1 and Form2 I created a second project called ClassLibrary1 which has one class called Class1. Both are contained within one solution. The WindowsApplication1 library has a project reference to ClassLibrary1. Form1 instantiates an object called c as Class1. I then tried the following code in Class1: Public Sub DoSomething() dim frm as New ??? End Sub I was going to hard-code the qualified reference to the WindowsApplication1.Form2. But ClassLibrary1 does not have a reference to WindowsApplication1. When I tried to add the reference to ClassLibrary1, it generated an error about circular references. This makes sense and what I'm wanting to do is not possible. So, I'll generate the forms within the applications project. Thanks for the help!
Threading
IsNumeric Bug or misunderstanding? what's the difference Function parameter function sorting inherited BindingList Simple but difficult to find solution for loading HTML into object problem with localization SOAP with Attachments Installing a .net windows service Help referencing a variable in HTML of an aspx page |
|||||||||||||||||||||||