creating a new instance of an object XXX type

J

jernej goricki

Hi,
Is it possible to do this in vb.net :

1.)To make a function that would have a string parameter ClassName
2.)This function would return an object of the same type as the ClassName
parameter was.

For example, in my project a have this classes : ClassPerson, ClassCar,
ClassXXX...........

Dim objectA as Object = GetNewObject(ClassPerson)
' ObjectA would be of a type ClassPeron

Public function GetNewObject(ClassName as string)
return new object instance of a type ClassName
End function

Thanks for your help!
 
S

Scott M.

So you are saying that you want a function to return a dynamically
determined class.

You can't do what you have below unless you plan on returning a string
because that's what the function says it returns. In your example below,
you don't indicate what the return type from the function is.

You need to know up front what type your function will return. You could do
this I suppose:

Public Function Test(x as object) as Object
some code
Dim x as new x.GetType
return x
End Function
 
O

One Handed Man [ OHM# ]

Can see why you want to do this but !


(Untested )

Class CGetter

public FunctionGetObject( cname as String ) return Object

Select Case case cname

case "ClassPerson"
return New ClassPerson()
case "ClassCar"
return New ClassCar()
case Else
return Nothing
End Select

End Function

End Class





jernej said:
Hi,
Is it possible to do this in vb.net :

1.)To make a function that would have a string parameter ClassName
2.)This function would return an object of the same type as the
ClassName parameter was.

For example, in my project a have this classes : ClassPerson,
ClassCar, ClassXXX...........

Dim objectA as Object = GetNewObject(ClassPerson)
' ObjectA would be of a type ClassPeron

Public function GetNewObject(ClassName as string)
return new object instance of a type ClassName
End function

Thanks for your help!

Regards - OHM# OneHandedMan{at}BTInternet{dot}com
 
O

One Handed Man [ OHM# ]

Class CGetter

public FunctionGetObject( cname as String ) As Object ( Not return object as
f*&ked up in above post )

Select Case case cname

case "ClassPerson"
return New ClassPerson()
case "ClassCar"
return New ClassCar()
case Else
return Nothing
End Select

End Function

End Class
 
B

Blumidoo

Hi,
Is it possible to do this in vb.net :

1.)To make a function that would have a string parameter ClassName
2.)This function would return an object of the same type as the ClassName
parameter was.

What you are trying to do is dirty, very dirty. You should rather design and
implement an AbstractFactory design pattern class. AbstractFactory design
pattern allows you to returns an instance of one of several possible classes
depending on the data provided to it.
Read http://www.dotnetextreme.com/code/SimpleFactory.asp for more
information about simple AbstractFactory design pattern implementations.

sincerely,
--
Sebastian Zaklada
Skilled Software
http://www.skilledsoftware.com
************************************
SQL Source Control 2003 - for
SQL Server Source Safe integration
and custom databases documentation
 
J

jernej goricki

yes this with select case would probably work, but I want to do this without
writing a new "select case" line for every ne class I will create.

Is possible to do this dynamically something like this:

return new object.Gettype("Class Name")
 
O

One Handed Man [ OHM# ]

There may well be some obscure way to achieve this, but It does not sound
very structured because. What you imply is that your factory class does not
need to validate if a class exists before it tries to create it.

IMHO This is not good sense.

Regards - OHM


jernej said:
yes this with select case would probably work, but I want to do this
without writing a new "select case" line for every ne class I will
create.

Is possible to do this dynamically something like this:

return new object.Gettype("Class Name")

Regards - OHM# OneHandedMan{at}BTInternet{dot}com
 
H

Herfried K. Wagner [MVP]

* "One Handed Man said:
I knew this would not be something simple, well done for finding it.

;-)

It's easy for me to find messages I have written previously...
 
B

Blumidoo

What a beautiful way to make a total mess in the code... :-/
Why should this code promote "bad programming habits"?

Not the code itself, I wrote I like it. But the idea is wrong - Jernej
should should rather design and implement an AbstractFactory design pattern
class, not create objects of any type whenever he wants and wherever in the
code. It is messy. Development is design, than design even more, and after
that you may code... And even than you will always end up refactoring what
you have written before... :)

sincerely,
--
Sebastian Zaklada
Skilled Software
http://www.skilledsoftware.com
************************************
SQL Source Control 2003 - for
SQL Server Source Safe integration
and custom databases documentation
 
H

Herfried K. Wagner [MVP]

* "Blumidoo said:
Not the code itself, I wrote I like it. But the idea is wrong - Jernej
should should rather design and implement an AbstractFactory design pattern
class, not create objects of any type whenever he wants and wherever in the
code. It is messy. Development is design, than design even more, and after
that you may code... And even than you will always end up refactoring what
you have written before... :)

ACK, but that depends on the project. If you want to write a little
tool in very short time, then creating a good architecture will take too
much time and a quick and dirty solution will fit the requirements.
 
T

Tom Leylan

Herfried...

You are aware that every suggestion ever posted here can be answered with
that paragraph right? The next time you suggest somebody takes their
ASP.Net question to another group they just say "it depends upon my purpose"
and how they were in a hurry or some such thing.

It doesn't take much longer to cut and paste the code from one reply over
the code from other reply wouldn't you agree?
 
J

Jay B. Harlow [MVP - Outlook]

Blumidoo,
Not the code itself, I wrote I like it. But the idea is wrong - Jernej
should should rather design and implement an AbstractFactory design
pattern
I hope you will agree there are times when the Abstract Factory Pattern (or
any of its Factory Pattern siblings) require that you use
Activator.CreateInstance to implement them.

Which is exactly what Herfried's example showed, Herfried's
CreateClassByName is a Factory Method that is implemented in terms of the
Activator.CreateInstance method.

A prime example is the Plugin Pattern
http://www.martinfowler.com/eaaCatalog/plugin.html.

Herfried's example is another, as there are more control types available,
then Herfried could possible hard code into his Factory Method.

Jernej's example may be a third, as most Factory Methods (at least ones I
design) return objects that all inherit from a common base (a base other
then System.Object!) I would not expect ClassPerson & ClassCar to inherit
from a common base. Granted Jernej may need a Person Factory method & a Car
Factory method, however that is another topic ;-)

However I will agree that Activator.CreateInstance is dangerous in that it
allows any type to be created, even types that maybe should not be created!

Just a thought
Jay
 
C

Cor

Hi OHM,

I think this kind of typoes don't need to be corrected in this newsgroup (or
it has to be for fun), if somebody ask a question like this and don't see
this, than he should not ask the question.

Just a thought,

Cor
 
C

Cor

I agree

I was thinking to put a question in this newsgroup like.

"Would somebody help me I want to make a class for converting an Integer to
a String".

But I think the message from Blumido fits this also


Cor
 

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