|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Get object from nameI need to identify an object from its name, something like this: Class User dim Username as string property Name() as string get Return Username end Get Set(value as string) Username = value end Set end property End Class Class MyClass Friend User1 as new User Friend User2 as new User Sub Main User1.Username = "Bill" User2.Username = "George" end sub Function GetUsername(strUser as string) as String dim oUser as Object oUser = GetObjectFromName(strUser) return oUser.Username end Function Function GetObjectFromName(strName as string) as Object ??????? ??????? End Function End Class Anyone knows how this function "GetObjectFromName" would look like? (Btw, the object could be an instance of any Class, this is just a simplified example) Thanks, Egbert Perhaps the following will assist.
Public Shared Function CreateObjectByStringName(ByRef sObjectName As String, Optional ByRef sAssemblyFile As String = "") As Object Try If sAssemblyFile = "" Then Return (System.Activator.CreateInstance(System.Type.GetType(sObjectName))) Else Return Activator.CreateInstanceFrom(sAssemblyFile, sObjectName).Unwrap() End If Catch ex As Exception Throw New System.IO.FileNotFoundException("Unable to locate resource: " & sAssemblyFile & "." & sObjectName, ex) End Try End Function Jim Wooley http://devauthority.com/blogs/jwooley/default.aspx Show quoteHide quote > Hi, > > I need to identify an object from its name, something like this: > > Class User > dim Username as string > property Name() as string > get > Return Username > end Get > Set(value as string) > Username = value > end Set > end property > End Class > Class MyClass > Friend User1 as new User > Friend User2 as new User > Sub Main > User1.Username = "Bill" > User2.Username = "George" > end sub > Function GetUsername(strUser as string) as String > dim oUser as Object > oUser = GetObjectFromName(strUser) > return oUser.Username > end Function > Function GetObjectFromName(strName as string) as Object > ??????? > ??????? > End Function > End Class > Anyone knows how this function "GetObjectFromName" would look like? > (Btw, the object could be an instance of any Class, this is just a > simplified example) > > Thanks, > Egbert Jim, thanks for your help, but this is not wat I am looking for. Your function returns a new instance of the class "sObjectName".
So sObjectName should be a Class name like "myNamespace.User", and not "User1". What I want to do is to make a "copy" of an object like: dim NewObject as Object = User1 but then: dim NewObject as Object = GetObjectFromName("User1") I am not sure if this is possible at all !! Egbert Show quoteHide quote "Jim Wooley" <jimNOSPAMwooley@hotmail.com> wrote in message news:24f81e8f4b098c86e2a36cb00ad@msnews.microsoft.com... > Perhaps the following will assist. > > Public Shared Function CreateObjectByStringName(ByRef sObjectName As String, > Optional ByRef sAssemblyFile As String = "") As Object > Try > If sAssemblyFile = "" Then > Return (System.Activator.CreateInstance(System.Type.GetType(sObjectName))) > Else > Return Activator.CreateInstanceFrom(sAssemblyFile, sObjectName).Unwrap() > End If > Catch ex As Exception > Throw New System.IO.FileNotFoundException("Unable to locate resource: > " & sAssemblyFile & "." & sObjectName, ex) > End Try > End Function > Jim Wooley > http://devauthority.com/blogs/jwooley/default.aspx > >> Hi, >> >> I need to identify an object from its name, something like this: >> >> Class User >> dim Username as string >> property Name() as string >> get >> Return Username >> end Get >> Set(value as string) >> Username = value >> end Set >> end property >> End Class >> Class MyClass >> Friend User1 as new User >> Friend User2 as new User >> Sub Main >> User1.Username = "Bill" >> User2.Username = "George" >> end sub >> Function GetUsername(strUser as string) as String >> dim oUser as Object >> oUser = GetObjectFromName(strUser) >> return oUser.Username >> end Function >> Function GetObjectFromName(strName as string) as Object >> ??????? >> ??????? >> End Function >> End Class >> Anyone knows how this function "GetObjectFromName" would look like? >> (Btw, the object could be an instance of any Class, this is just a >> simplified example) >> >> Thanks, >> Egbert > > Can't you just store the user objects in a Dictionary so that you can access
them directly by name? Andrew Bert wrote:
> I need to identify an object from its name If you want to identify an instance of an object by a given key, use one of the Collection classes within the Framework - HashTable would be ideal. Class MyClass Private m_oTable As New HashTable Sub Main() Dim oNewUser As New User( "Bill" ) m_oTable.Add( "Bill", New User( "Bill" ) ) m_oTable.Add( "George", New User( "George" ) ) Console.Writeline( m_oTable.Item( "Bill" ).ToString() ) End Sub End Class > Btw, the object could be an instance of any Class, this is just a simplified example That's(a) heading off into the land of Reflection, where you can find out just about anything about any method or property and (b) IMHO, probably not the best solution. What is it you're actually trying to achieve? Regards, Phill W. Phill,
> That's The land of Reflection is a very fascinating land, and can be very, very usefull in creating dynamic and flexible solutions.> (a) heading off into the land of Reflection, where you can find out just > about anything about any method or property and > (b) IMHO, probably not the best solution. > > What is it you're actually trying to achieve? I'm just trying to avoid to keep up hashtables or dictionaries or so, although that solution will work in my case. What I want is to be able to invoke any member of any object at runtime based on input or database-data. regards, Egbert Show quoteHide quote "Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> wrote in message news:e8gh2g$aot$1@yarrow.open.ac.uk... > Bert wrote: > >> I need to identify an object from its name > > If you want to identify an instance of an object by a given key, use one > of the Collection classes within the Framework - HashTable would be ideal. > > Class MyClass > Private m_oTable As New HashTable > Sub Main() > Dim oNewUser As New User( "Bill" ) > m_oTable.Add( "Bill", New User( "Bill" ) ) > m_oTable.Add( "George", New User( "George" ) ) > > Console.Writeline( m_oTable.Item( "Bill" ).ToString() ) > End Sub > End Class > >> Btw, the object could be an instance of any Class, this is just a simplified example > > That's > (a) heading off into the land of Reflection, where you can find out just > about anything about any method or property and > (b) IMHO, probably not the best solution. > > What is it you're actually trying to achieve? > > Regards, > Phill W. Bert wrote:
> The land of Reflection is a very fascinating land, and can be very, very usefull in creating dynamic and flexible solutions. Wholely agree with you, but would still emphasises the "/can be/" part of the above. Im my experience, a truly Reflection-based solution is oftem more laborious than a more restrictive, but better-targeted approach. > What I want is to be able to invoke any member of any object at runtime based on input or database-data. Would you consider a kind of "command-line" interface into any of your classes? This is an approach I've used recently: Public Interface IControllable Execute( ByVal sCommand as String ) as String End Interface Any class that I want to "drive" from my external source implements this interface (the method is deliberately /over/-simplified because the client-end is a TCP socket). Within the method's implementation, though, I can make the object do anything that it can do internally. Regards, Phill W.
Multithread Revisted
how to parse an Enum Structure in vb.net Removing the " Web Form Designer Generated Code " region after converting from ASP.NET 1.1 to ASP.NE Need help on windows service and timer Getting form to run its load event without showing form Confused About Dispose and Finalize in VS.Net Who is using a given port? Convert Color image to Gray Shade Image Which would be better MS ACESS or SQL Server for VB Desktop application Active Directory and VB.Net |
|||||||||||||||||||||||