473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Get object from name

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
Jul 5 '06 #1
6 4290
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.GetTy pe(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

Jul 5 '06 #2
Can't you just store the user objects in a Dictionary so that you can access
them directly by name?

Andrew
Jul 5 '06 #3
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

"Jim Wooley" <ji*************@hotmail.comwrote in message news:24*************************@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.GetTy pe(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
Jul 5 '06 #4
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.
Jul 5 '06 #5
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

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message news:e8**********@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.
Jul 5 '06 #6
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.
Jul 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
9
by: Keith Rowe | last post by:
Hello, I am trying to reference a Shockwave Flash Object on a vb code behind page in an ASP.NET project and I receive the following error: Guid should contain 32 digits with 4 dashes...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
2
by: Aaron | last post by:
I have a data sructure setup and I populate it in a loop like so: y=0 while X: DS.name = "ASDF" DS.ID = 1234 list = DS; y = y + 1
5
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
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,...
0
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$) { } ...
0
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...
0
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.