[RESOLVED] What am i missing? (Probably really easy!)
Hi Guys, this is the code i have:
VB Code:
Dim i As Integer = 0
While sqlRdr.Read()
Dim arrChar As Char() = sqlRdr.Item("photoType").ToString.ToCharArray()
If Not Char.IsDigit(arrChar(1)) Then
For j As Integer = 0 To photoTypes.Length - 1
If Not photoTypes(j).ToString = sqlRdr.Item("photoType").ToString Then
If j = photoTypes.Length - 1 Then
photoTypes(i) = sqlRdr.Item("photoType").ToString
ReDim Preserve photoTypes(i + 1)
i += 1
End If
End If
Next
End If
End While
photoTypes() is just a string array declared as follows:
VB Code:
Dim photoTypes(0) as string
Now what i am trying to do is to add to an array only if the value doesn't already exist. Now when i run my program i get a null exception error when i get to the line:
VB Code:
If Not photoTypes(j).ToString = sqlRdr.Item("photoType").ToString Then
I know there is something really simple that i am missing that will fix my problem but i cant seem to figure out what it is so if anyone could help me i'd really appreciate it.
Thanks in advance for any help
:afrog:
Re: What am i missing? (Probably really easy!)
You've created a String array with one element that is a null reference. You then get that element and call its ToString method. Firstly, it is a null reference so it is no object so it has no members, hence your exception. Secondly, if it was an object it would be a String, so why call its ToString method?
If you nee4d to dynamically resize your array then I strongly suggest that you use a collection instead, which are specifically designed to be easier and more efficient in that case. In VB 2005 (can you please specify your version, even if you don't think it matters) use a List(Of String). In previous versions use a Specialized.StringCollection. If you really need an array you can create one after you've added all the items.
Re: What am i missing? (Probably really easy!)
Sorry i forgot to specify. I'm using .net compact framework 2.0