Arrays in VB.NET 2005? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Arrays in VB.NET 2005?

SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
Is this possible? They were easy in VB6, but I am having trouble with VB.NET under VS2005. The examples below all fail after the "New" statement.
[code]
Private arrayInt As Integer()
Private arrayLong() As Long

Me.arrayInt = New Integer(32)
Me.arrayLong = New Long(64)
[/code]
If I specify the array size in the declaration it works fine, but that is not acceptable since one instance may need two integers and another may need a thousand. Ideas or suggestions?

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    You need to get closer to VB6 ;)

    ReDim ftw

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
    : You need to get closer to VB6 ;)
    :
    : ReDim ftw
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry
    Yes and no. I was almost correct but you need to declare the array initially as "Private someName(-1) As Int". This produces what is the VB.NET equivelant of "int *pArray;" in C/C++. It isn't a pointer, but it isn't "nothing" either. It's an empty array which can then be used by ReDim. Using no value when declaring the array can sometimes cause problems according to what i read on the MSDN, so using -1 is better unless you have specific reason not to.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : Yes and no. I was almost correct but you need to declare the array
    : initially as "Private someName(-1) As Int".

    Not in my version of VB6. Just declaring it without a dimension works fine - you just can't use any functions on it untill you've ReDim'd it.
    Moreso, someName(-1) should cause an Index out of bounds exception, since default array base is 0.


    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
    : Not in my version of VB6.

    That's the problem, VB6 is deprecated. I found tons of tutorials on arrays for VB6 and upon referencing the MSDN, you'll see that those methods no longer work. That si why I posted on the VB.NET forum, since VB6 is deprecated and many aspects of VB6 won't work correctly, if at all, under .NET. I have done a lot of reading on this via the MSDN recently and It seems to me as though Microsoft is trying to completely kill VB6 since VB.NET is out and can do more with less code.

    Reminds me of how they try to trick new C/C++ programmers into using their own versions of "sprintf()", "memset()", etc by saying the standard C versions are deprecated. This means anybody who falls for that trick won't be able to compile their C/C++ stuff under any other OS.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : : Not in my version of VB6.
    :
    : That's the problem, VB6 is deprecated. I found tons of tutorials on
    : arrays for VB6 and upon referencing the MSDN, you'll see that those
    : methods no longer work. That si why I posted on the VB.NET forum,
    : since VB6 is deprecated and many aspects of VB6 won't work
    : correctly, if at all, under .NET. I have done a lot of reading on
    : this via the MSDN recently and It seems to me as though Microsoft is
    : trying to completely kill VB6 since VB.NET is out and can do more
    : with less code.
    :
    : Reminds me of how they try to trick new C/C++ programmers into using
    : their own versions of "sprintf()", "memset()", etc by saying the
    : standard C versions are deprecated. This means anybody who falls for
    : that trick won't be able to compile their C/C++ stuff under any
    : other OS.
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/gre
    : en][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h
    : [/red][/b][/italic]

    On the bright side, .NET executables are made in such a way that any OS that exposes a .NET framework can use them ;)
    Anyway, VB6 is almost as incompatible as VB.NET.

    And VB.NET really is a good language and the framework is quite nice for developing code with nice UI.


    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
    : On the bright side, .NET executables are made in such a way that any
    : OS that exposes a .NET framework can use them ;)
    Yeah that's the problem, .NET is Microsoft, which means that any application using .NET will only run under Windows until proper emulation is supported on other systems. For example, you cannot run .NET apps under Linux, Mac, BSD, or other OSes. Maybe WINE will support .NET someday, or even Cedega, but I am not sure how non-linux OSes such as MacOS will ever support .NET. It's a trade-off, use .NET and develop faster but be confined to Windows, or use your own code from scratch and allow most of the code to compile on any OS.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : : On the bright side, .NET executables are made in such a way that any
    : : OS that exposes a .NET framework can use them ;)
    : Yeah that's the problem, .NET is Microsoft, which means that any
    : application using .NET will only run under Windows until proper
    : emulation is supported on other systems. For example, you cannot run
    : .NET apps under Linux, Mac, BSD, or other OSes. Maybe WINE will
    : support .NET someday, or even Cedega, but I am not sure how
    : non-linux OSes such as MacOS will ever support .NET. It's a
    : trade-off, use .NET and develop faster but be confined to Windows,
    : or use your own code from scratch and allow most of the code to
    : compile on any OS.
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/gre
    : en][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h
    : [/red][/b][/italic]

    This story goes for VB6 as well, even though it should be much easier to emulate a VB6 environment than it is a .NET environment.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • Bharathi-kBharathi-k Posts: 22Member
    Hi,

    Declaring an array

    An array needs to be declared before it can be used in a program. We can declare an array by using the following syntax.

    Dim arrayname(Number-of-elements) as datatype

    For example, to create an array named AccountNames for storing 5 string values, we can use the following statement.

    Dim AccountNames(5) as string

    In Visual basic 2005, the array subscript always starts with zero. Therefore, the above statement creates an array of strings containing 5 (0 to 4) elements. We can also create an integer array containing 5 elements with the following statement.

    Dim AccountBalances() as Integer = New Integer(5) {}

    In the statement, the curly braces {} are used to initialize the array elements. For example, to create and initialize an integer array containing 5 elements, we can use the following statement.

    Dim AccountBalances() as Integer = new Integer(5) {0, 10, 20, 30, 40}


    Dynamic Arrays in vb 2005

    Sometimes we may not know how large the array should be. Instead of making it large enough to hold the maximum number of data, we can declare a dynamic array. the size of a dynamic array can vary during the execution of a program. To create a dynamic array, declare it with the Dim statement without the dimensions as shown below.

    Dim myarray() As String

    Later in the program, if we know how many elements we want to store in that array, we can use the ReDim statement to redimension the array, this time to its required size.

    ReDim myarray(10)

    The ReDim statement can appear only in a procedure. Unlike the Dim statement, ReDim is executable, it forces the application to carry out an action at runtime. Dim statements are not executable, and they appear outside procedures. Note that the ReDim statement can
  • DrMartenDrMarten Posts: 748Member
    : Hi,
    :
    : Declaring an array
    :
    : An array needs to be declared before it can be used in a program. We
    : can declare an array by using the following syntax.
    :
    : Dim arrayname(Number-of-elements) as datatype
    :
    : For example, to create an array named AccountNames for storing 5
    : string values, we can use the following statement.
    :
    : Dim AccountNames(5) as string
    :
    : In Visual basic 2005, the array subscript always starts with zero.
    : Therefore, the above statement creates an array of strings
    : containing 5 (0 to 4) elements. We can also create an integer array
    : containing 5 elements with the following statement.
    :
    : Dim AccountBalances() as Integer = New Integer(5) {}
    :
    : In the statement, the curly braces {} are used to initialize the
    : array elements. For example, to create and initialize an integer
    : array containing 5 elements, we can use the following statement.
    :
    : Dim AccountBalances() as Integer = new Integer(5) {0, 10, 20, 30,
    : 40}
    :
    :
    : Dynamic Arrays in vb 2005
    :
    : Sometimes we may not know how large the array should be. Instead of
    : making it large enough to hold the maximum number of data, we can
    : declare a dynamic array. the size of a dynamic array can vary during
    : the execution of a program. To create a dynamic array, declare it
    : with the Dim statement without the dimensions as shown below.
    :
    : Dim myarray() As String
    :
    : Later in the program, if we know how many elements we want to store
    : in that array, we can use the ReDim statement to redimension the
    : array, this time to its required size.
    :
    : ReDim myarray(10)
    :
    : The ReDim statement can appear only in a procedure. Unlike the Dim
    : statement, ReDim is executable, it forces the application to carry
    : out an action at runtime. Dim statements are not executable, and
    : they appear outside procedures. Note that the ReDim statement can
Sign In or Register to comment.