Results 1 to 11 of 11

Thread: Help with Arrays

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    31

    Help with Arrays

    Ok this is probably really easy but i cant seem to get an array of mine to populate.

    array Code:
    1. Private Sub ButtonGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGenerate.Click
    2.  
    3.         Dim counter As Integer = 0
    4.         If TextBox4.Text = TextBox3.Text And TextBox4.Text = TextBox2.Text And TextBox4.Text = TextBox1.Text Then
    5.             Dim OldName, NewName, Folder As String
    6.             Dim logCount As Integer = 0
    7.             Dim TrueFalse As Boolean
    8.             StartDir = TextBoxDir.Text
    9.             'Variables for writing text log
    10.             Dim LogArray() As String
    11.             Dim FileWriter As StreamWriter
    12.             Dim LogFile As String = StartDir & "LogFile.txt"
    13.             'Loop to change names and make log
    14.             For counter = 0 To ArrayLengthGlobal - 1 Step 1
    15.                 'Need Logic To skip files that can not be found.  Need to make a log when Skips.  
    16.                 'Have box 1 show log of files skiped
    17.                 'Have box 2 Show completed message
    18.                 OldName = StartDir & "\" & Lines2(counter) & "\" & Lines3(counter) & "\" & Lines1(counter)
    19.                 NewName = Lines4(counter)
    20.                 Folder = StartDir & "\" & Lines2(counter) & "\" & Lines3(counter)
    21.                 TrueFalse = My.Computer.FileSystem.FileExists(OldName)
    22.                 If TrueFalse Then
    23.  
    24.                     My.Computer.FileSystem.RenameFile(OldName, NewName)
    25.                 Else
    26.                     LogArray(logCount) = OldName
    27.                     logCount = logCount + 1
    28.                 End If
    29.             Next
    30.             FileWriter = New StreamWriter(LogFile, False)
    31.             For counter1 = 0 To logCount Step 1
    32.                 FileWriter.Write(LogArray(counter1))
    33.             Next
    34.             FileWriter.Close()
    35.             'Dim LogString() As String = LogArray
    36.             'TextBoxOldNameDir.Text = LogString()
    37.             TextBoxNewNameDir.Text = "Complete!"
    38.         Else
    39.             TextBoxNewNameDir.Text = "Error! Please make sure all files are of same length."
    40.         End If
    41.     End Sub

    Whats happening is that LogArray under the else statement is saying Variable 'LogArray' is used before it is assigned a value.

    Again I'm new to VB and I'm not to sure what this is trying to do. I thought I assigned LogArray above when I put
    logarray Code:
    1. Dim LogArray() As String
    In my code.

  2. #2
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Somewhere else today
    Posts
    355

    Re: Help with Arrays

    Should that not read:

    dim LogArray() as Array

    Computerman.
    It was much easier in VB6, but I am now liking Vb.Net alot more.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    31

    Re: Help with Arrays

    If i do that then i get an error where it says

    Value of type 'String' cannot be converted to 'System.Array'

    Im just lost on why I cant use that LogArray() as a string array if i do it that why.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Help with Arrays

    You declare an array but you did not give it any size, so that array is just Nothing.
    If you don't know the size yet at the time the array is declared, you should be using a List(Of T) instead.
    So change your array declaration to use a list(Of String)
    Code:
    Dim logList As New List(Of String)
    And when you have something to add to the list, you just call its Add method
    Code:
    logList.Add(oldName)
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    31

    Re: Help with Arrays

    So its pretty much anytime u make an array you need to know the size of the array other wise u need to use new list

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Help with Arrays

    Quote Originally Posted by csbarone View Post
    So its pretty much anytime u make an array you need to know the size of the array other wise u need to use new list
    Not quite that way... I'd say every time you use an array, you have to know its size... That's because you can create an array with just 1 element if you don't know how big it needs to be at the time of the declaration. Later, when you use the array and finds out that 1 element is not enough, you then can resize the array to make it bigger by using the key words "ReDim" or "Redim Preserve"... But the point is, when you declare an array, you must give it a size.
    The only exception to this is when you call a method that returns an array.
    Code:
    Dim files() as String = IO.Directory.GetFiles(folderpath)
    In that code line, the files() array is declared without a size, that's because it's just a variable that refers to the actual array returned by the Directory.GetFiles method.

    Back to the topic of creating an array of size 1 and redim it as needed later. This technique works, but it's not preferred because the cost associated with "redim preserve" an array is rather big. So you want to minimize the number of times you havve to redim your array. And that's why using a list(of T) is preferred for this situation. The list(Of T) actually does call redim internally when needed, but it's designed in such a way that would require a minimal redim call.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  7. #7
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Help with Arrays

    vb.net Code:
    1. Dim LogArray(ArrayLengthGlobal-1) As String
    should work.

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Help with Arrays

    Quote Originally Posted by minitech View Post
    vb.net Code:
    1. Dim LogArray(ArrayLengthGlobal-1) As String
    should work.
    Yes, that would work but it also creates an array larger than what is needed (because the OP only assign a value to the array in the Else block). This won't be a problem in most cases, but if the OP needs a count of the things he has in the array, logArray.Length would give an incorrect answer because there no guarantee that for every iteration of the loop, the execution is branched to the Else block. This means that some of the elements in the array is left empty (nothing). And to get an acutal count, he would have to loop thru the array and test each element to see if it's not = nothing and add 1 to the count.
    Using a List(Of T), you don't have this problem.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    31

    Re: Help with Arrays

    thanks guys that helps alot.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help with Arrays

    Quote Originally Posted by stanav View Post
    But the point is, when you declare an array, you must give it a size.
    I know you know how arrays work but this terminology is wrong. You don't declare arrays. Just like any other type, you declare variables and create objects. Arrays are objects, i.e. instances of the Array class. This code declares a variable but does not assign an object to it, so the variable is Nothing:
    vb.net Code:
    1. Dim obj As Object
    This code declares a variable, creates an object and assigns that object to the variable:
    vb.net Code:
    1. Dim obj As Object = New Object
    VB provides the following shorthand for that code:
    vb.net Code:
    1. Dim obj As New Object
    The equivalent for arrays would be declaring a variable but creating no object, so the variable is Nothing:
    vb.net Code:
    1. Dim arr() As Object
    declaring a variable, creating an object and assigning the object to the variable:
    vb.net Code:
    1. Dim arr() As Object = New Object(upperBound) {}
    and the VB shorthand:
    vb.net Code:
    1. Dim arr(upperBound) As Object
    If we use the correct terminology all the time then the meaning is clear. To create an array object you must specify the number of elements. This makes perfect sense given that an array object cannot be resized. If you haven't specified the size of an array then you haven't created an object; you have just declared a variable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help with Arrays

    Further to my previous post, note that Object can be replaced with any other type and the principle still holds. Note also that the middle code snippet in each case can be separated into two separate lines, demonstrating how you would declare a variable that is initially Nothing and then assign to it later:
    vb.net Code:
    1. Dim obj As Object
    2. Dim arr() As Object
    3.  
    4. 'blah
    5. 'blah
    6. 'blah
    7.  
    8. obj = New Object
    9. arr = New Object(upperBound) {}
    As I said before, the array object is not create until you specify a size or, more specifically, an upper bound.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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