|
-
Aug 26th, 2009, 10:56 AM
#1
Thread Starter
Junior Member
Help with Arrays
Ok this is probably really easy but i cant seem to get an array of mine to populate.
array Code:
Private Sub ButtonGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGenerate.Click
Dim counter As Integer = 0
If TextBox4.Text = TextBox3.Text And TextBox4.Text = TextBox2.Text And TextBox4.Text = TextBox1.Text Then
Dim OldName, NewName, Folder As String
Dim logCount As Integer = 0
Dim TrueFalse As Boolean
StartDir = TextBoxDir.Text
'Variables for writing text log
Dim LogArray() As String
Dim FileWriter As StreamWriter
Dim LogFile As String = StartDir & "LogFile.txt"
'Loop to change names and make log
For counter = 0 To ArrayLengthGlobal - 1 Step 1
'Need Logic To skip files that can not be found. Need to make a log when Skips.
'Have box 1 show log of files skiped
'Have box 2 Show completed message
OldName = StartDir & "\" & Lines2(counter) & "\" & Lines3(counter) & "\" & Lines1(counter)
NewName = Lines4(counter)
Folder = StartDir & "\" & Lines2(counter) & "\" & Lines3(counter)
TrueFalse = My.Computer.FileSystem.FileExists(OldName)
If TrueFalse Then
My.Computer.FileSystem.RenameFile(OldName, NewName)
Else
LogArray(logCount) = OldName
logCount = logCount + 1
End If
Next
FileWriter = New StreamWriter(LogFile, False)
For counter1 = 0 To logCount Step 1
FileWriter.Write(LogArray(counter1))
Next
FileWriter.Close()
'Dim LogString() As String = LogArray
'TextBoxOldNameDir.Text = LogString()
TextBoxNewNameDir.Text = "Complete!"
Else
TextBoxNewNameDir.Text = "Error! Please make sure all files are of same length."
End If
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 In my code.
-
Aug 26th, 2009, 11:02 AM
#2
Hyperactive Member
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. 
-
Aug 26th, 2009, 11:08 AM
#3
Thread Starter
Junior Member
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.
-
Aug 26th, 2009, 11:13 AM
#4
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 -
-
Aug 26th, 2009, 11:18 AM
#5
Thread Starter
Junior Member
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
-
Aug 26th, 2009, 11:35 AM
#6
Re: Help with Arrays
 Originally Posted by csbarone
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 -
-
Aug 26th, 2009, 12:52 PM
#7
Re: Help with Arrays
vb.net Code:
Dim LogArray(ArrayLengthGlobal-1) As String
should work.
-
Aug 26th, 2009, 01:10 PM
#8
Re: Help with Arrays
 Originally Posted by minitech
vb.net Code:
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 -
-
Aug 26th, 2009, 01:15 PM
#9
Thread Starter
Junior Member
Re: Help with Arrays
thanks guys that helps alot.
-
Aug 26th, 2009, 10:04 PM
#10
Re: Help with Arrays
 Originally Posted by stanav
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:This code declares a variable, creates an object and assigns that object to the variable:
vb.net Code:
Dim obj As Object = New Object
VB provides the following shorthand for that code:The equivalent for arrays would be declaring a variable but creating no object, so the variable is Nothing:declaring a variable, creating an object and assigning the object to the variable:
vb.net Code:
Dim arr() As Object = New Object(upperBound) {}
and the VB shorthand:
vb.net Code:
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.
-
Aug 26th, 2009, 10:14 PM
#11
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:
Dim obj As Object Dim arr() As Object 'blah 'blah 'blah obj = New Object 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|