Results 1 to 7 of 7

Thread: array length unknown? adding more to an array? [resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    southwest pennsylvania
    Posts
    65

    array length unknown? adding more to an array? [resolved]

    i dont know how to do things with arrays, please help me

    im using this code:

    Dim sr As StreamReader = File.OpenText("users.txt")
    Dim users()() As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim i As Integer = 0
    Dim x As Integer = 0
    Do While sr.Read
    users(x)(0) = sr.ReadLine
    users(x)(1) = sr.ReadLine
    x += 1
    Loop
    sr.Close()
    End Sub
    Dim sr As StreamReader = File.OpenText("users.txt")
    Dim users()() As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim i As Integer = 0
    Dim x As Integer = 0
    Do While sr.Read
    users(x)(0) = sr.ReadLine
    users(x)(1) = sr.ReadLine
    x += 1
    Loop
    sr.Close()
    End Sub

    im getting this error:

    An unhandled exception of type 'System.NullReferenceException' occurred in first.exe

    Additional information: Object reference not set to an instance of an object.

    can someone plz help me?
    Last edited by joeframbach; Dec 30th, 2002 at 10:06 PM.
    if you choose not to decide you still have made a choice!

    RUSH rocks!

  2. #2
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651
    You have to dimension the array before use. Since it is a 2 dimensional array, you can only use Redim on the last index. But, you cannot use Redim on a module level variable.
    Why don't you use the ArrayList datatype?
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    southwest pennsylvania
    Posts
    65
    can you tell me more about arraylists? i cant seem to find it in the help index
    if you choose not to decide you still have made a choice!

    RUSH rocks!

  4. #4
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Search for "ArrayList Class" in the help and choose the topic with the same name.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    southwest pennsylvania
    Posts
    65
    i must be missing something here...

    it seems to me that arraylists are not for .net? i get an error: arrays cannot be declared with 'new'.
    if you choose not to decide you still have made a choice!

    RUSH rocks!

  6. #6
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651
    You have to reference the System.Collections class and then
    try this:
    VB Code:
    1. dim myList as new Arraylist

    As for help system, do a search on "ArrayList". You should get something even if you have just the .NET SDK and don't have VS.NET
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Location
    southwest pennsylvania
    Posts
    65
    should i put Imports System.Collections at the top, or can i do Dim myList As New System.Collections.ArrayList()
    if you choose not to decide you still have made a choice!

    RUSH rocks!

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