Results 1 to 3 of 3

Thread: [RESOLVED] What am i missing? (Probably really easy!)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Resolved [RESOLVED] What am i missing? (Probably really easy!)

    Hi Guys, this is the code i have:
    VB Code:
    1. Dim i As Integer = 0
    2.             While sqlRdr.Read()
    3.                 Dim arrChar As Char() = sqlRdr.Item("photoType").ToString.ToCharArray()
    4.                 If Not Char.IsDigit(arrChar(1)) Then
    5.                     For j As Integer = 0 To photoTypes.Length - 1
    6.                         If Not photoTypes(j).ToString = sqlRdr.Item("photoType").ToString Then
    7.                             If j = photoTypes.Length - 1 Then
    8.                                 photoTypes(i) = sqlRdr.Item("photoType").ToString
    9.                                 ReDim Preserve photoTypes(i + 1)
    10.                                 i += 1
    11.                             End If
    12.                         End If
    13.                     Next
    14.                 End If
    15.             End While
    photoTypes() is just a string array declared as follows:
    VB Code:
    1. 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:
    1. 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
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

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

    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.
    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

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: What am i missing? (Probably really easy!)

    Sorry i forgot to specify. I'm using .net compact framework 2.0
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

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