create varible at runtime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SHVzYW0=?=

    create varible at runtime

    Hi EveryBody:

    I am using vb.net 2005 and the following code trying to create varible at
    runtime:

    For n=1 to 4
    dim v+n.tostring(n) as double
    Next n

    the resone behaind using the previous code is to create

    Dim v1(4) as double
    dim v2(4) as double
    dim v3(4) as double
    dim v4(4) as double

    but it dose not work,Can some body help me or direct me how can I do so?

    regard's

    Husam
  • Michel Posseth  [MCP]

    #2
    Re: create varible at runtime

    AFAIK what you try to do is not possible ( or i am sure missing something )
    however you could acomplish your task by using a array of doubles or if you
    need to access the values as key value pairs you could go for a hashtable
    both of them would work fine .

    hth

    Michel



    "Husam" <Husam@discussi ons.microsoft.c omschreef in bericht
    news:B32D1D1C-0AD7-4DBC-A491-17A9624C5B3A@mi crosoft.com...
    Hi EveryBody:
    >
    I am using vb.net 2005 and the following code trying to create varible at
    runtime:
    >
    For n=1 to 4
    dim v+n.tostring(n) as double
    Next n
    >
    the resone behaind using the previous code is to create
    >
    Dim v1(4) as double
    dim v2(4) as double
    dim v3(4) as double
    dim v4(4) as double
    >
    but it dose not work,Can some body help me or direct me how can I do so?
    >
    regard's
    >
    Husam

    Comment

    • =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=

      #3
      Re: create varible at runtime

      Husam wrote:
      Hi EveryBody:
      >
      I am using vb.net 2005 and the following code trying to create varible at
      runtime:
      >
      For n=1 to 4
      dim v+n.tostring(n) as double
      Next n
      >
      the resone behaind using the previous code is to create
      >
      Dim v1(4) as double
      dim v2(4) as double
      dim v3(4) as double
      dim v4(4) as double
      >
      but it dose not work,Can some body help me or direct me how can I do so?
      >
      regard's
      >
      Husam
      What Michael says is true, it's not possible to create variables at
      runtime. It simply doesn't make sense to have dynamic variables in a
      compiled language.

      There are several options for accomplishing something that you can use
      similar to dynamic variables. Here are some:

      1. A two dimensional array:

      Dim v(3, 3) As Double
      v(0, 0) = 3.14
      v(3, 3) = 42

      2. A jagged array, i.e. an array of arrays.

      3. A list or arrays:

      Dim v As new List(Of Double())

      4. A list of lists:

      Dim v As new List(Of List(Of Double))
      v.Add(new List(Of Double)())
      v(0).Add(3.14)

      5. A dictionary of arrays:

      Dim v As new Dictionary(Of Integer, Double())

      6. A Dictionary of lists:

      Dim v As New Dictionary(Of Integer, List(Of Double))
      v(42) = new List(Of Double)()
      v(42).Add(3.14)

      ....and so on. It all depends on what you want to use it for.

      It would perhaps be better if you explained what it is that you want to
      accomplish, instead of asking about how to do it the way that you think
      that it should be done...

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      Working...