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.Creat eInstance(Type. GetType(myAppli cationName & "." &
FormToOpen))
Dim frm As FrontierForm = DirectCast(objN ewForm, FrontierForm)
The "myApplicationN ame & "." & FormToOpen" code fully qualifies the
form. The myApplicationNa me 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? 6 1441
Did you reference the other project?
T
Paul wrote: 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.Crea teInstance(Type .GetType(myAppl icationName & "." & FormToOpen)) Dim frm As FrontierForm = DirectCast(objN ewForm, FrontierForm)
The "myApplicationN ame & "." & FormToOpen" code fully qualifies the form. The myApplicationNa me 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(myAppli cationName & "." &
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(m yApplicationNam e & "." &
FormToOpen)" the variable myApplicationNa me 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.acViewNo rmal, Optional ByVal FilterName As Object =
"", Optional ByVal WhereCondition As Object = "", Optional ByVal
DataMode As AcFormOpenDataM ode =
AcFormOpenDataM ode.acFormPrope rtySettings, Optional ByVal WindowMode As
AcWindowMode = AcWindowMode.ac WindowNormal, Optional ByVal OpenArgs As
Object = "")
Try
Dim objNewForm As Object =
Activator.Creat eInstance(Type. GetType(myAppli cationName & "." &
FormToOpen))
Dim frm As FrontierForm = DirectCast(objN ewForm,
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(m yApplicationNam e & "." & 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, Cor: what do you mean by FormToOpen
Public Sub OpenForm(ByVal FormToOpen As String, Optional ByVal View As AcView = AcView.acViewNo rmal, Optional ByVal FilterName As Object = "", Optional ByVal WhereCondition As Object = "", Optional ByVal DataMode As AcFormOpenDataM ode = AcFormOpenDataM ode.acFormPrope rtySettings, Optional ByVal WindowMode As AcWindowMode = AcWindowMode.ac WindowNormal, Optional ByVal OpenArgs As Object = "") Try Dim objNewForm As Object = Activator.Creat eInstance(Type. GetType(myAppli cationName & "." & FormToOpen)) Dim frm As FrontierForm = DirectCast(objN ewForm, 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 is exactly as I thought, I would refactor it in your case, than you
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
WindowsApplicat ion1. 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 WindowsApplicat ion1 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
WindowsApplicat ion1.Form2. But ClassLibrary1 does not have a reference
to WindowsApplicat ion1. 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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Clint Herron |
last post by:
Howdy!
I posted this question on CSharpCorner.com, but then realized I should
probably post it on a more active newsgroup. This will be my only cross-post.
I'm creating a game engine, and using CodeDOM for my scripting needs (I
realize I could use yacc or something else, but I wanted to try using CodeDOM
-- this is more of an exercise for...
|
by: Chris Bower |
last post by:
Reposted from aspnet.buildingcontrols:
Ok, I've got a bunch of derived controls that all have a property Rights of
type Rights (Rights is an Enumerator). I wrote a custom TypeConverter so
that I can use comma separated values in design-time. The TypeConverter
works great in design-time. It converts to and from just fine... However,
when I try...
|
by: Imran Aziz |
last post by:
Hello All,
I am getting the following error on our production server, and I dont
get the same error on the development box.
Unable to cast object of type 'System.Byte' to type 'System.String'.
here is the code that I used to create a table and then add columns to it
later, later I populate the rows in the table.
|
by: S. Justin Gengo |
last post by:
Hi,
I've created a component that allows me to store database information for
various types of databases my company uses. It uses a collection for each
type of database. Everything is working perfectly except when the component
is deleted from the page. Here are the details.
I add the component to the page from the toolbox and then add...
|
by: Charles Law |
last post by:
I want to do something like this:
obj = CType(value, Value.Type)
Well, not exactly, but I think that captures the essence. I realise it won't
work as I have written it, and it looks a bit like a nebulous statement, but
I am looking for a generic way to convert a variable of unknown type to its
actual type.
Perhaps a better example...
| |
by: Don |
last post by:
How do I get an Enum's type using only the Enum name?
e.g.
Dim enumType as System.Type
Dim enumName as String = "MyEnum"
enumType = ???(enumName)
|
by: Sky |
last post by:
I have been looking for a more powerful version of GetType(string) that will
find the Type no matter what, and will work even if only supplied
"{TypeName}", not the full "{TypeName},{AssemblyName}"
As far as I know yet -- hence this question -- there is no 'one solution
fits all', but instead there are several parts that have to be put...
|
by: Sky |
last post by:
Yesterday I was told that GetType(string) should not just be with a Type, but
be Type, AssemblyName. Fair enough, get the reason. (Finally!).
As long as it doesn't cause tech support problems down the line...
What happens when my code is run on a station that only has framework 3.0 or
4.0, and this assembly, with version number defined for...
|
by: Nathan Sokalski |
last post by:
I have the following code:
Dim values As New ArrayList()
values.Add("Yes")
values.Add("No")
values.Add("Maybe")
values.Add("Whatever")
dim x as String()=values.ToArray()
However, I recieve an error saying an Object array cannot be implicitly
|
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...
|
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...
| |
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |