creating a dynamic object based upon a variable???


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: creating a dynamic object based upon a variable???

  1. #1
    Patrick Buchanan Guest

    creating a dynamic object based upon a variable???


    Does anyone know if there is a way to create an object using the contents
    of a variable as a name (instead of hard coding the object name?).

    In the example below, I have the code for creating a connection object (with
    the name hard-coded)...

    set conn=server.createobject("adodb.connection")
    conn.open "DSN=dataset name"
    conn is hard coded and becomes the connection object.

    Is there a way to do something like this???

    connvar = "conn"
    set {evaluated value of variable "connvar"}=
    server.createobject("adodb.connection")
    {evaluated value of variable "connvar"}.open "DSN=dataset name"

    Thanks in advance,
    Pat


  2. #2
    Matt M Guest

    Re: creating a dynamic object based upon a variable???


    Patrick,
    I'm not sure if this would work but just throwing out the idea. How bout
    sending the variable to a sub routine that creates the connection? Something
    like...
    'fills the variable with a value
    connvar = "whatever"

    'Calls the sub and passes the variable with the value
    createConnObject connvar

    'Creates a connection object named whatever
    Sub createConnObject(conn)
    Set conn = Server.CreateObject("ADODB.Connection")
    .....
    End Sub

    Matt M



    "Patrick Buchanan" <patrick.buchanan@tap.com> wrote:
    >
    >Does anyone know if there is a way to create an object using the contents
    >of a variable as a name (instead of hard coding the object name?).
    >
    >In the example below, I have the code for creating a connection object (with
    >the name hard-coded)...
    >
    > set conn=server.createobject("adodb.connection")
    > conn.open "DSN=dataset name"
    >conn is hard coded and becomes the connection object.
    >
    >Is there a way to do something like this???
    >
    > connvar = "conn"
    > set {evaluated value of variable "connvar"}=
    > server.createobject("adodb.connection")
    > {evaluated value of variable "connvar"}.open "DSN=dataset name"
    >
    > Thanks in advance,
    > Pat
    >



  3. #3
    Matt M Guest

    Re: creating a dynamic object based upon a variable???


    I just tried it and it doesn't work sorry
    Matt M
    "Matt M" <mmather@cybervisiondesign.com> wrote:
    >
    >Patrick,
    >I'm not sure if this would work but just throwing out the idea. How bout
    >sending the variable to a sub routine that creates the connection? Something
    >like...
    >'fills the variable with a value
    >connvar = "whatever"
    >
    >'Calls the sub and passes the variable with the value
    >createConnObject connvar
    >
    >'Creates a connection object named whatever
    >Sub createConnObject(conn)
    > Set conn = Server.CreateObject("ADODB.Connection")
    > .....
    >End Sub
    >
    >Matt M
    >
    >
    >
    >"Patrick Buchanan" <patrick.buchanan@tap.com> wrote:
    >>
    >>Does anyone know if there is a way to create an object using the contents
    >>of a variable as a name (instead of hard coding the object name?).
    >>
    >>In the example below, I have the code for creating a connection object

    (with
    >>the name hard-coded)...
    >>
    >> set conn=server.createobject("adodb.connection")
    >> conn.open "DSN=dataset name"
    >>conn is hard coded and becomes the connection object.
    >>
    >>Is there a way to do something like this???
    >>
    >> connvar = "conn"
    >> set {evaluated value of variable "connvar"}=
    >> server.createobject("adodb.connection")
    >> {evaluated value of variable "connvar"}.open "DSN=dataset name"
    >>
    >> Thanks in advance,
    >> Pat
    >>

    >



  4. #4
    Chadwick Posey Viajo.com Guest

    Re: creating a dynamic object based upon a variable???


    Yes, you can create dynamic variable names using the VB's execute statement,
    here's an example piece of code:

    '=======================================================
    connvar = "conn"
    execute "Set " & connVar & _
    "=server.createobject(""adodb.connection"")"

    execute connVar & ".open ""DSN=dataset name"""
    '=======================================================

    Hope this helps...

    Chadwick Posey
    Viajo.com

    "Matt M" <mmather@cybervisiondesign.com> wrote:
    >
    >I just tried it and it doesn't work sorry
    >Matt M
    >"Matt M" <mmather@cybervisiondesign.com> wrote:
    >>
    >>Patrick,
    >>I'm not sure if this would work but just throwing out the idea. How bout
    >>sending the variable to a sub routine that creates the connection? Something
    >>like...
    >>'fills the variable with a value
    >>connvar = "whatever"
    >>
    >>'Calls the sub and passes the variable with the value
    >>createConnObject connvar
    >>
    >>'Creates a connection object named whatever
    >>Sub createConnObject(conn)
    >> Set conn = Server.CreateObject("ADODB.Connection")
    >> .....
    >>End Sub
    >>
    >>Matt M
    >>
    >>
    >>
    >>"Patrick Buchanan" <patrick.buchanan@tap.com> wrote:
    >>>
    >>>Does anyone know if there is a way to create an object using the contents
    >>>of a variable as a name (instead of hard coding the object name?).
    >>>
    >>>In the example below, I have the code for creating a connection object

    >(with
    >>>the name hard-coded)...
    >>>
    >>> set conn=server.createobject("adodb.connection")
    >>> conn.open "DSN=dataset name"
    >>>conn is hard coded and becomes the connection object.
    >>>
    >>>Is there a way to do something like this???
    >>>
    >>> connvar = "conn"
    >>> set {evaluated value of variable "connvar"}=
    >>> server.createobject("adodb.connection")
    >>> {evaluated value of variable "connvar"}.open "DSN=dataset name"
    >>>
    >>> Thanks in advance,
    >>> Pat
    >>>

    >>

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center