Converting String into Object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rajesh Kr Shukla

    Converting String into Object

    Hi There,
    Can any one suggest me how to convert the string into object in VB.net.
    If you are familier with javascript you can use.

    var i = 1
    eval("textbox" + i).value = "Hello World"

    What is the equivalent of this in VB.net

    Cheers..Rajesh
  • Herfried K. Wagner [MVP]

    #2
    Re: Converting String into Object

    Hello,

    "Rajesh Kr Shukla" <rkshukla@india .com> schrieb:[color=blue]
    > Can any one suggest me how to convert the string into
    > object in VB.net. If you are familier with javascript you
    > can use.
    >
    > var i = 1
    > eval("textbox" + i).value = "Hello World"
    >
    > What is the equivalent of this in VB.net[/color]

    \\\
    Private Function FindControl( _
    ByVal ControlName As String, _
    ByVal CurrentControl As Control _
    ) As Control
    Dim ctr As Control
    For Each ctr In CurrentControl. Controls
    If ctr.Name = ControlName Then
    Return ctr
    Else
    ctr = FindControl(Con trolName, ctr)
    If Not ctr Is Nothing Then
    Return ctr
    End If
    End If
    Next ctr
    End Function
    ///

    Usage:

    \\\
    DirectCast(Find Control("btnBla ", Me), Button).Enabled = False
    ///

    Notice that the procedure listed above is "slow", if you have to access a
    lot of controls by name very often, you should store references to them in a
    'Hashtable' object. You can use the name of the control as key.

    --
    Herfried K. Wagner
    MVP · VB Classic, VB.NET
    Die Website von H. Wagner zu .NET, Visual Basic .NET, Classic Visual Basic, Webentwicklung und mehr.



    Comment

    • Rajesh Shukla

      #3
      Re: Converting String into Object

      Hi,
      Thanks for you reply, but my problem is different. Here I have just
      taken the example of TextBox, but in my example I have got a function
      which receives parameter as an Object (User defined object not the
      controls on form).

      That Object name I am creating in runtime.


      Cheers..Rajesh







      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Fergus Cooney

        #4
        Re: Converting String into Object

        Hi Rajesh,

        To find an object, 'someone' must know about the object. To find an object
        within a set, 'someone' must have a set of references.

        If you know in advance that you will need this facility with certain
        objects, you can use a class, derived from HashTable, with which you register
        those objects when they are created. It could be in a Module for convenience.

        If this doesn't address what you're after, can you give us some more
        details?

        Regards,
        Fergus


        Comment

        • Tom Spink

          #5
          Re: Converting String into Object

          Hi Rajesh,

          Basically: No.

          Object names are not kept at compilation, they are obliterated. This means
          you cannot reference an object by name.

          Why do you want to do this? Perhaps we can suggest a better way.

          --
          HTH,
          -- Tom Spink, Über Geek

          Please respond to the newsgroup,
          so all can benefit

          "Chaos, Panic, Disorder, my work here is done"


          "Rajesh Shukla" <rkshukla@india .com> wrote in message
          news:OnX4XQLgDH A.2072@TK2MSFTN GP10.phx.gbl...
          : Hi,
          : Thanks for you reply, but my problem is different. Here I have just
          : taken the example of TextBox, but in my example I have got a function
          : which receives parameter as an Object (User defined object not the
          : controls on form).
          :
          : That Object name I am creating in runtime.
          :
          :
          : Cheers..Rajesh
          :
          :
          :
          :
          :
          :
          :
          : *** Sent via Developersdex http://www.developersdex.com ***
          : Don't just participate in USENET...get rewarded for it!


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: Converting String into Object

            Hello,

            "Rajesh Shukla" <rkshukla@india .com> schrieb:[color=blue]
            > Thanks for you reply, but my problem is different. Here
            > I have just taken the example of TextBox, but in my
            > example I have got a function which receives parameter as
            > an Object (User defined object not the controls on form).[/color]

            Sorry, I don't understand what you want to do. You may want to have a look
            at the 'Activator.Crea teInstance' method.

            --
            Herfried K. Wagner
            MVP · VB Classic, VB.NET
            Die Website von H. Wagner zu .NET, Visual Basic .NET, Classic Visual Basic, Webentwicklung und mehr.



            Comment

            Working...