Searchi an array

torg

Regular
Joined
Nov 7, 2002
Messages
60
Location
Norway
I`ve been sitting here for a while now, but can`t figure out what I`m doing wrong. I`m hoping that someone out here can help me to solve my problem. I receive the message. that there are noe object reference set to this instance of object. Can anybody tell meg Why?
Ive got:
Visual Basic:
Structure individual
        Dim name1 As String
        Dim dato As String
        Dim address As String
        Dim Phonenumber As String
    End Structure

    Dim Person(100) As individual
   Dim numPosts as integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     
             Dim sr As StreamReader = File.OpenText("Personalia.txt")
        Do While (sr.Peek <> -1) And (numPosts< Person.GetUpperBound(0))

            numPosts += 1
            Person(numPosts ).name1 = sr.ReadLine
            Person(numPosts ).dato = sr.ReadLine
            Person(numPosts ).address = sr.ReadLine
            Person(numPosts ).Phonenumber = sr.ReadLine
        Loop
....
end sub

Function FindName(ByVal name1) As String
        Dim sr As StreamReader = File.OpenText("Personalia.txt")
        Dim First, Mid, Last As Integer

        Dim foundFlag As Boolean
        First= 1
        Last= numPosts
        Do While (first<= last) And (Not foundFlag)
            Mid= CInt((first+ last) / 2)
            Select Case Person(Mid).name1.ToUpper
                Case name
                    foundFlag = True 
                Case Is > name1
                    last= first- 1
                Case Is < name1
                    last= first+ 1
            End Select
           Loop

        If foundFlag Then
            ListBox1.Items.Add(Person(mid).name1)
        Else
            MessageBox.Show("nothing")
        End If
    End Function
 
I notice you are incrementing a variable called antPoster in the While loop but it doesn't appear to be declared anywhere, on a related note you are not incrementing the numPosts variable within the loop and as such you are just going to keep overwriting the first element in the array.
 
Sorry, that was just a write fault. antPosts is numPosts..As it is in my original code. (had to do some amendments before copying the code to this board) So problem is stilll here...
 
Try

Visual Basic:
Dim sr AS StreamReader 

 sr = New StreamReader(New FileStream("Personalia.txt"), FileMode.Open))

I hope this helps

:)
 
I receive the following message when trying your proposal. overload resolution failed because no accessible 'New' accepts the number of arguments.
I`ve used
Visual Basic:
Dim sr As StreamReader = File.OpenText("Personalia.txt")
other places in my code, and have not had any problems with that. so I doubt that`s my problem. If it is there must be another answer to it.
 
Sorry, I lead you astray:

Can you try?

Visual Basic:
Dim sr As StreamReader 
Dim fs As FileStream

 fs = New New FileStream(("Personalia.txt"),  FileMode.Open)
 sr = New StreamReader(fs)
 
Doesn`t help either.
i`m using
Visual Basic:
Option Explicit On 
Imports System.IO
Public Class Form4
in the top of my programming code, så I don`t see that what you suggest would help very much.

My problem is.: I`ve got at file "txtPersonalia.txt" where you can get certain information. this informatiin are to be placed into an array on form load where it will be sorted in an ascending order .When I type a text in a textbox, the file will be searched to find a match, and write all information that is relevant for that reslut

Visual Basic:
   Structure individual
        Dim name As String
        Dim dato As String
        Dim address As String
        Dim Phonnumber As String
    End Structure

    Dim Person(100) As individual

when trying to run I receive the following message
Additional information: Object reference not set to an instance of an object. Which drives me crazy
 
I`m not sure I understand what you`re asking about.
The array reads data from a file , as long as ithere are "Data" to read. If there are only 49 posts in the file, then the arrayindex 50 would be empty.As is the case in my example. I`ve only registered 6 Posts/persons in the file.
 
the problem is probably the fact that the line
Visual Basic:
Mid= CInt((first+ last) / 2)

will always equal 50 on the first run through regardless of how many items you've read from the file. You would probably need to have a seperate variable to keep track of the number of valid records read rather than just basing it on the size of the array.
If you still have a problem reply and I'll try to knock a sample together.
 
Just a few other things.
I notice you are opening a StreamReader in the first line of the FindName function - did you mean to?

Also within the select case statement you have a lne that reads
Case Name

should that be
Case Name1
instead?

also you appear to be doing a binary search of the data - am I correct?
If so is the data sorted? If so are the blank entries being moved to the start of the array rather than the end?

finally are you modifying the numPosts variable anywhere else in the Form_Load?
 
Yes, that should be Case Name1. Yes, it`s supposted to be a binarysearch of the data And no, the streamreader was not supposted to be in the function.

I`ve never taken into concern to move blank fields to the end or start. How do I do that?

The posts in the file are already sorted alphabeticalla by name
But that`s just while trying to get the code to work. I`m going to use the sort algorithm downbelow

As you`ll see downbelow, It`s only in the sort algorithm where I use the variable except from in the FindName function. But it`s not modified there.

Visual Basic:
 Sub sort()
         Dim gap As Integer, doneFlag As Boolean
        Dim index As Integer, temp As String
        gap = CInt(numPosts/ 2)

        Do While gap >= 1
            Do
                doneFlag = True
                For index = 1 To numPosts- gap
                    If person(index).name1> person(index + gap).name1 Then
                        temp = person(index).name1
                        person(index).name1= person(index + gap).name1
                        person(index + gap).name1 = temp
                        doneFlag = False
                    End If
                Next
            Loop Until doneFlag = True
            gap = CInt(gap / 2)
        Loop
    End Sub

There may be some write faults in the code that may have occured during translation of code. Ignore them. Or assume the syntax is correct.
 
I may be stating the obvious, but make sure your string, is actually filled with something before you try calling ToUpper.

Visual Basic:
Dim testStr As String
MsgBox(testStr.ToUpper)'Would error

Dim testStr As String = ""
MsgBox(testStr.ToUpper)'Would not error

Dim testStr As String
MsgBox(UCase(testStr))'Would not error


I stay away from using ToUpper, ToLower, Trim, Length of the string object because of this, and I use functions themselves. :eek:
 
I`ve used
Visual Basic:
If IsNothing(individ(i).navn) Then individ(i).navn = ""
in the form_load procedure.
Still there is something wrong. It seems as if there are not performed any search at all, atleast no result are shown

the search algorithm now looks like this:
Visual Basic:
 Function findName(ByVal name2 As String) As Integer
        Dim first, middle, last As Integer
        first = 0
        last = numOfRecords
        Do While (first <= last)
            middle = CInt((first + last) / 2)
            Select Case UCase(Person(middle).name1) 
                Case name2
                    Return middle
                Case Is > name2
                    last = middle - 1
                Case Is < name2
                    first = middle + 1
            End Select
        Loop
        Return 0
    End Function
 
Back
Top