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! 20 3239
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 goricki wrote: 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
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
"jernej goricki" <ph*****@obmocje51.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... 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!
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
> 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
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")
"One Handed Man [ OHM# ]" <OneHandedMan{at}BTInternet{dot}com> wrote in
message news:uC**************@tk2msftngp13.phx.gbl...
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 goricki wrote: 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
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 goricki wrote: 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")
"One Handed Man [ OHM# ]" <OneHandedMan{at}BTInternet{dot}com> wrote in message news:uC**************@tk2msftngp13.phx.gbl...
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 goricki wrote: 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
Regards - OHM# OneHandedMan{at}BTInternet{dot}com
* "jernej goricki" <ph*****@obmocje51.net> scripsit: 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.
<http://groups.google.com/groups?selm=bqns3k%2424ghqf%242%40ID-208219.news.uni-berlin.de>
--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
that is just the thing im looking for, thanks
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:OP**************@TK2MSFTNGP09.phx.gbl... * "jernej goricki" <ph*****@obmocje51.net> scripsit: 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.
<http://groups.google.com/groups?selm...-208219.news.u
ni-berlin.de> -- Herfried K. Wagner [MVP] <http://www.mvps.org/dotnet>
I knew this would not be something simple, well done for finding it.
Regards - OHM
Herfried K. Wagner [MVP] wrote: * "jernej goricki" <ph*****@obmocje51.net> scripsit: 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.
< http://groups.google.com/groups?selm...-208219.news.u
ni-berlin.de>
Regards - OHM# OneHandedMan{at}BTInternet{dot}com
>
< http://groups.google.com/groups?selm...-208219.news.u
ni-berlin.de>
What a beautiful way to make a total mess in the code... :-/
Not that I do not like the solution, but I don't like to promote bad
programming habits.
sincerely,
--
Sebastian Zaklada
Skilled Software http://www.skilledsoftware.com
************************************
SQL Source Control 2003 - for
SQL Server Source Safe integration
and custom databases documentation
I agree. The principal itself is poor.
Regards
Blumidoo wrote:
<http://groups.google.com/groups?selm...-208219.news.u ni-berlin.de>
What a beautiful way to make a total mess in the code... :-/ Not that I do not like the solution, but I don't like to promote bad programming habits.
sincerely,
Regards - OHM# OneHandedMan{at}BTInternet{dot}com
* "Blumidoo" <sz*********************@skilledsoftware.com> scripsit: <http://groups.google.com/groups?selm...-208219.news.u ni-berlin.de>
What a beautiful way to make a total mess in the code... :-/ Not that I do not like the solution, but I don't like to promote bad programming habits.
Why should this code promote "bad programming habits"?
--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
* "One Handed Man [ OHM# ]" <OneHandedMan{at}BTInternet{dot}com> scripsit: 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...
--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
> > What a beautiful way to make a total mess in the code... :-/ Not that I do not like the solution, but I don't like to promote bad programming habits.
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
* "Blumidoo" <sz*********************@skilledsoftware.com> scripsit: 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.
--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
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?
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote... 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.
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
"Blumidoo" <sz*********************@skilledsoftware.com> wrote in message
news:bs**********@atlantis.news.tpi.pl... What a beautiful way to make a total mess in the code... :-/ Not that I do not like the solution, but I don't like to promote bad programming habits.
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
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 public FunctionGetObject( cname as String ) As Object ( Not return object
as f*&ked up in above post )
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
* "Tom Leylan" <ge*@iamtiredofspam.com> scripsit: 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.
Sorry, I don't understand how this is related to a VB.NET question.
;->
--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: David Elliott |
last post by:
I have a 3rd Party DLL that is a Win32 NOT .NET Assembly that is misbehaving.
I am trying to isolate it in its own AppDomain. I got to load but can not create an
instance. I receive the following...
|
by: Nanda |
last post by:
hi,
I am trying to generate parameters for the updatecommand
at runtime.
this.oleDbDeleteCommand1.CommandText=cmdtext;
this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
|
by: Rajesh Tammana |
last post by:
Hi,
I am trying to create an SMS package using GetPDFData
method. When I call this method it returns the PackageData
object instance with C# object type ManagementBaseObject.
Usually I call...
|
by: Davinci_Jeremie |
last post by:
Hi Newbee here to C# I have a simple questions...
In a Hello world example how does the class object Hello exist with out
creating it? I come from object pascal where everything object is...
|
by: Carlos Lozano |
last post by:
Hi,
What is the right way to create an OCX COM component. The component is
already registerred, but can't create an instance. I am using the reference
to the interop module created.
If I use...
|
by: Mats Lycken |
last post by:
Hi,
I'm creating a CMS that I would like to be plug-in based with different
plugins handling different kinds of content.
What I really want is to be able to load/unload plugins on the fly
without...
|
by: Lee Harr |
last post by:
I understand how to create a property like this:
class RC(object):
def _set_pwm(self, v):
self._pwm01 = v % 256
def _get_pwm(self):
return self._pwm01
pwm01 = property(_get_pwm, _set_pwm)
|
by: wcc |
last post by:
Hello,
How do I create a class using a variable as the class name?
For example, in the code below, I'd like replace the line
class TestClass(object):
with something like
class...
|
by: mark.norgate |
last post by:
Hello
I want to create a reference to an object, so that changes to the
referenced object are reflected in the other object. Like this:
object o = 123;
object p = o;
o = 456;
|
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= |
last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting
more often than creating an instance of objects.
like :
protected void gvOrderDetailsRowDataBound(object sender,...
|
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...
|
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: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |