Results 1 to 4 of 4

Thread: Converting string name of an object to the actual object

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    163

    Converting string name of an object to the actual object

    Is it possible to dim an object as a evaluation of a string variable.

    VB Code:
    1. Friend gobjSalesRecord As SageDataObject100.SalesRecord
    2.  
    3. Public Sub New(ByVal strSDODataObjectName As String)
    4.  
    5.         Dim objSDODataObject As Object
    6.  
    7.         objSDODataObject = strSDODataObjectName
    8.  
    9.         ......
    10.  
    11. End Sub

    Where strSDODataObjectName = "gobjSalesRecord"

    I want to convert that string name of the object to the actual Object and pass that, ie make objSDODataObject = gobjSalesRecord
    chilling

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Converting string name of an object to the actual object

    Why not just pass the entire object into the sub?... (byval DataObject as SageDataObject100.SalesRecord)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    163

    Resolved Re: Converting string name of an object to the actual object

    Because I don't know which object I want to use until run time and the calling program does not know anything about SageDataObject100.SalesRecord .....

    Anyway fixed it ... just needed to change this line

    objSDODataObject = strSDODataObjectName

    to

    objSDODataObject = CObj(strSDODataObjectName)
    chilling

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Converting string name of an object to the actual object

    Um, no. All that does is cast your String as an Object and assign it to that variable. It absolutely DOES NOT get a reference to an object that is refered to by a variable of that name. strSDODataObjectName is a string variable and the value it contains has no relationship whatsoever to any other variable whose name is the same as that value. If you want to get a member variable from a name in a string then you need to use Reflection.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width