Use a string to refered to an object

First, sorry for my poor english... My native language is french.

Now my question:

I need to have a function that return a string. Something like this:

Function test(p1 as integer) as String
  Select Case p1
    Case 1
      test = "object1"
    ...
    Case n
      test = "objectn"
  End select
End function

So if i use "test(2).name" i will access the name property of object2.

Sorry if it's confuse.

Vincent

Nothing99Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
VbmasterConnect With a Mentor Commented:
You can set the function to return a object, like this..

Function test(p1 as integer) As Object

  Select Case p1
  Case 1
    Set test = object1
  ...
  Case n
    Set test = objectn
  End select

End function

This will make code like test(2).name work the way you want.

Any reason why you are not using a control array? To control a control array you create one object on the form and then copy'n'paste the object, VB will automatically create a control array. Whenever you want to access a object you use code like name_of_the_objects(index).
0
 
AzraSoundCommented:
the way you have the function set up it will return a single value for test...all you should have to do is return test and youll get the object from the select case statement

Dim str As String
str = test(3)
Msgbox str
0
 
AzraSoundCommented:
he says he wants to return a string...maybe he means this:

Function test(p1 as integer) as String
  Select Case p1
    Case 1
      test = object1.name
    ...
    Case n
      test = objectn.name
  End select
End function


now test will return the name of whichever object is selected according to the p1 value that is passed to the function.  let us know if we're reading your question correctly.


however this wouldnt make much sense to have a control array as they all bear the same name (different indexes)
how does the p1 parameter reference a particular object?  what is the correlation between the two?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Public Function Text(p1 as Integer) as String
Text = me.Controls("object" & p1).Name
End Function

This may be what you are looking for, Me is referring to the Form
0
All Courses

From novice to tech pro — start learning today.