473,406 Members | 2,220 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,406 software developers and data experts.

About Creating Objects Dynamically

Tom
Hi All :

I'm VB.Net Beginner I try to create object Dynamically :

In my testing, I create 2 .net project
One is MyApp (Type is windows application) with one button name "Button1"
The another one is "prj01" (Type is Class libary) with one public Class name
"dbLib"

In this prj01.dbLib I create the simple code "

Public Class dbLib
Public Function SendMsg()
MsgBox("a")
End Function
End Class

and success to compile.

In the MyApp.Form1 I create the following Simple code :

Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oCar As Object
Dim ty As Type = Type.GetType("prj01.dbLib")
oCar = Activator.CreateInstance(ty)
oCar.SendMsg()

End Sub
End Class

However , while I run the project , The object "ty" return NOTHING.

And show error "Value Cannot be Null"

How can fix it

Thanks
Nov 21 '05 #1
3 2960
If you have a class in a separate project, make sure the start up project has
a reference to that project in the references section of the solution tree
view.

'******code******

Public Class GetThis
Public Sub GetThisMessage(ByVal Message as String)
MessageBox.Show(Message)
End Class

'*****end of code******

The code is simpler that what you were trying to do.
Inside your button event handler, write the following code

'******code*******

Dim GT as New Proj1.GetThis
GT.GetThisMessage("Go Sox")

'*****end of code*******

The new keyword allows you to construct an instance of the class from the
class, which can be thought of as a template.

Once you have the instance, you can manage the properties of the instance,
and use its methods.

www.charlesfarriersoftware.com
"Tom" wrote:
Hi All :

I'm VB.Net Beginner I try to create object Dynamically :

In my testing, I create 2 .net project
One is MyApp (Type is windows application) with one button name "Button1"
The another one is "prj01" (Type is Class libary) with one public Class name
"dbLib"

In this prj01.dbLib I create the simple code "

Public Class dbLib
Public Function SendMsg()
MsgBox("a")
End Function
End Class

and success to compile.

In the MyApp.Form1 I create the following Simple code :

Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oCar As Object
Dim ty As Type = Type.GetType("prj01.dbLib")
oCar = Activator.CreateInstance(ty)
oCar.SendMsg()

End Sub
End Class

However , while I run the project , The object "ty" return NOTHING.

And show error "Value Cannot be Null"

How can fix it

Thanks

Nov 21 '05 #2
Tom
Is that mean of Add Reference in parent project :

Fox example

ProjectParent.ClassA call ProjectChild.ClassB The I must add reference
(ProjectChild) in ProjectParent

However, If I don't add reference . Just like to use createobject)_ while I
don't like add reference. How can I do these ?


"Charlie" <Ch*****@discussions.microsoft.com> ¦b¶l¥ó
news:D1**********************************@microsof t.com ¤¤¼¶¼g...
If you have a class in a separate project, make sure the start up project has a reference to that project in the references section of the solution tree
view.

'******code******

Public Class GetThis
Public Sub GetThisMessage(ByVal Message as String)
MessageBox.Show(Message)
End Class

'*****end of code******

The code is simpler that what you were trying to do.
Inside your button event handler, write the following code

'******code*******

Dim GT as New Proj1.GetThis
GT.GetThisMessage("Go Sox")

'*****end of code*******

The new keyword allows you to construct an instance of the class from the
class, which can be thought of as a template.

Once you have the instance, you can manage the properties of the instance,
and use its methods.

www.charlesfarriersoftware.com
"Tom" wrote:
Hi All :

I'm VB.Net Beginner I try to create object Dynamically :

In my testing, I create 2 .net project
One is MyApp (Type is windows application) with one button name "Button1" The another one is "prj01" (Type is Class libary) with one public Class name "dbLib"

In this prj01.dbLib I create the simple code "

Public Class dbLib
Public Function SendMsg()
MsgBox("a")
End Function
End Class

and success to compile.

In the MyApp.Form1 I create the following Simple code :

Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oCar As Object
Dim ty As Type = Type.GetType("prj01.dbLib")
oCar = Activator.CreateInstance(ty)
oCar.SendMsg()

End Sub
End Class

However , while I run the project , The object "ty" return NOTHING.

And show error "Value Cannot be Null"

How can fix it

Thanks

Nov 21 '05 #3
Yes. In the Start Up Project, the one that contains the forms, go to View,
Solution Explorer. If your Class Project is not shown under References,
right click References and go to the Projects Tab, and double click the Class
Project and click OK. Then from your Start Up Project, just use the name of
the Class Project + dot to get to the classes in the Class Project.

When you are working on the project, set Build>>Configuration Manager to
Debug for both projects. For deployment, set the projects to Release. The
compiled files: the .exe (Start Up Project), and the .dll (Class Project)
will be in the bin folder of the Start Up Project folder. You can deploy the
project to any computer that has the dot net runtime files just by copying
the files to a folder on that computer.
"Tom" wrote:
Hi All :

I'm VB.Net Beginner I try to create object Dynamically :

In my testing, I create 2 .net project
One is MyApp (Type is windows application) with one button name "Button1"
The another one is "prj01" (Type is Class libary) with one public Class name
"dbLib"

In this prj01.dbLib I create the simple code "

Public Class dbLib
Public Function SendMsg()
MsgBox("a")
End Function
End Class

and success to compile.

In the MyApp.Form1 I create the following Simple code :

Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oCar As Object
Dim ty As Type = Type.GetType("prj01.dbLib")
oCar = Activator.CreateInstance(ty)
oCar.SendMsg()

End Sub
End Class

However , while I run the project , The object "ty" return NOTHING.

And show error "Value Cannot be Null"

How can fix it

Thanks

Nov 21 '05 #4

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

Similar topics

6
by: Bhavin | last post by:
Anybody quickly replies it, would be a great help! I try to create dynamic array of textboxes control in ASP.NET(C#). I got the following error while I tried to assign value to ID property of...
7
by: Snake | last post by:
Hi guys, I have question about classes. when u create class called Test. and you define variable Test c; so does this act like( a variable c of type Test pointing to an abject )? The thing that...
1
by: chris | last post by:
I know I've asked this before, but I didn't really get an answer and I bet it's because I didn't explain myself very well. Here goes again. I have this code: Dim arrData(intNoOfRows,...
0
by: Tom | last post by:
Thanks Chris : However, during run time, it show error message "File / component cannot find " during run in Dim asm As =...
2
by: Tom | last post by:
Thanks Chris : However, during run time, it show error message "File / component cannot find " during run in Dim asm As =...
105
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
75
by: Steven T. Hatton | last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash, Mathematica, SML, or LISP. A college teacher recently posted to this newsgroup regarding her observation that there has...
1
by: krishna81m | last post by:
I am unable to create a list of objects dynamically. I run a SQL query on the database and populate objects of types that I also know aprior. I read the query as a string, run it on the database and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...

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.