|
-
Aug 24th, 2012, 11:19 PM
#1
Thread Starter
Lively Member
[RESOLVED] Storing data in arrays
Hi all. I'm trying to get words from a csv file and then store them in my program. Like this....
Open the file
Read the first word
Store the word in wordat(1)
Read the second word
Store that word in wordat(2)
Close the file
But I'm getting a "Value of type '1-dimensional array of String' cannot be converted to 'String'." error.
Code:
thefil = My.Application.Info.DirectoryPath & "\test.txt"
Using therea As New Microsoft.VisualBasic.FileIO.TextFieldParser(thefil)
therea.TextFieldType = FileIO.FieldType.Delimited
therea.SetDelimiters(",")
Dim wordat(9) As String
x = 0
While Not therea.EndOfData
wordat(x) = therea.ReadFields() <<< PROBLEM HERE
For Each currentField In wordat(x)
MsgBox("wordat " & x + 1 & " " & currentField)
x = x + 1
Next
End While
End Using
Is the problem the conversion (eg - a string can't be converted to a number) or is it the array (eg - the size of the array is too small)? Or something else? Thanks.
-
Aug 25th, 2012, 03:38 AM
#2
Re: Storing data in arrays
See here: http://msdn.microsoft.com/en-us/libr...eldparser.aspx
ReadFields
Reads all fields on the current line, returns them as an array of strings, and advances the cursor to the next line containing data.
Code:
wordat(x) = therea.ReadFields() <<< PROBLEM HERE
wordat(x) is an element of a string array, therefore you must assign a string value to it. The method returns an array of strings, which is not a string.
-
Aug 25th, 2012, 06:00 AM
#3
Thread Starter
Lively Member
Re: Storing data in arrays
Thanks for that.
Code:
Dim wordat(9)
Dim thefil As String
Dim therow As String()
Dim datval As String
thefil = My.Application.Info.DirectoryPath & "\test.txt"
Using therea As New Microsoft.VisualBasic.FileIO.TextFieldParser(thefil)
therea.TextFieldType = FileIO.FieldType.Delimited
therea.SetDelimiters(",")
While Not therea.EndOfData
therow = therea.ReadFields()
x = 0
For Each datval In therow
wordat(x) = datval
MsgBox("wordat is " & wordat(x))
Next
End While
End Using
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
|