Get object from name

  • Thread starter Thread starter Bert
  • Start date Start date
B

Bert

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
 
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
 
Can't you just store the user objects in a Dictionary so that you can access
them directly by name?

Andrew
 
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
 
Bert said:
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
(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?

The land of Reflection is a very fascinating land, and can be very, very usefull in creating dynamic and flexible solutions.

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
 
Bert said:
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.
 
Back
Top