Results 1 to 7 of 7

Thread: Arrays in VB.NET

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    2

    Arrays in VB.NET

    What I want to do is create an array, of say 5 elements, but instead of doing it the old VB6 way:

    Dim myArr(4) as variant
    myArr(0) = "1"
    myArr(1) = 23
    myArr(2) = "2"
    myArr(3) = "other"
    myArr(4) = 2.2

    I want to be able to clean it up and do it on one line, somthing like this:

    Dim myArr(4) as VariantType = new Array("1",2,2.2,"4",5)

    But I get the error:

    'New' cannot be used on a class that is declared 'MustInherit'.

    I know I am doing something wrong, anyone know what???

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    VB Code:
    1. Dim myArr() As VariantType = {"1", 23, "2", "other", 2.2}

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Use Object instead of VariantType. Object is pretty much the equvilant of Variant.

    VB Code:
    1. Dim myArr() As Object= {"1", 23, "2", "other", 2.2}

  4. #4
    Lively Member freightliner's Avatar
    Join Date
    Nov 2002
    Location
    Belgium
    Posts
    123
    there is only one problem, all the variables will use the same object/class, not a new instance of it.

    so:

    why don't you do it like

    dim arr() as object
    arr(0)=new object
    arr(1)=new object
    ...
    very handy: [vbcode][/vbcode]
    VB.NET - VB6 - VBA - ASP - RPG(AS/400) - C++ - java - SQL

    look in the help, many probs can be solved that way.
    I know, i'm to lazy too.

    PLEASE PUT RESOLVED IF RESOLVED!!

  5. #5
    Lively Member freightliner's Avatar
    Join Date
    Nov 2002
    Location
    Belgium
    Posts
    123
    this way you can even make triangular arrays
    like
    1
    1 1
    1 1 1
    ...

    or even


    1
    1 1 1
    1 1
    1 1 1 1
    ...
    very handy: [vbcode][/vbcode]
    VB.NET - VB6 - VBA - ASP - RPG(AS/400) - C++ - java - SQL

    look in the help, many probs can be solved that way.
    I know, i'm to lazy too.

    PLEASE PUT RESOLVED IF RESOLVED!!

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Actually this:

    VB Code:
    1. dim arr() as object
    2. arr(0)=new object
    3. arr(1)=new object

    And

    VB Code:
    1. Dim myArr() As Object= {New Object,New Object}

    Are the samething.

    The only difference is you used objects and the previous examples used more common data types.

  7. #7
    Member
    Join Date
    Sep 2002
    Location
    California
    Posts
    52
    Remember that you can use a For Each loop with arrays too.

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