About Creating Objects Dynamically

T

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
 
G

Guest

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
 
T

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 ?
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top