473,322 Members | 1,314 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,322 software developers and data experts.

Type.GetType with two projects

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(myApplicatio nName & "." &
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?

Feb 8 '06 #1
6 1424
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.CreateInstance(Type.GetType(myApplicati onName & "." &
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?

Feb 9 '06 #2
TSI
I think the error is here
GetType(myApplicationName & "." &
FormToOpen))

cuz you try to pass a String value instead of you passing an Object .

Feb 9 '06 #3
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
Feb 9 '06 #4
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(myApplicatio nName & "." &
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?"

Feb 9 '06 #5
Paul,

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(myApplicatio nName & "." &
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 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
Feb 9 '06 #6
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!

Feb 9 '06 #7

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

Similar topics

7
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...
4
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...
3
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'. ...
2
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...
6
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...
13
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)
7
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}"...
1
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...
3
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.