Results 1 to 8 of 8

Thread: Struggling Need Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    2

    Struggling Need Help

    Hi

    Ok so I seem to be getting a problem when I try and amend data on a file I have highlighted below the point where VB highlights the error to me any help would be much appreciated.

    Thanks,

    Ben

    Public Class Form3
    Dim searchitem, numberofitemsinarray, count As Integer
    Dim runnerfound As Boolean

    Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
    searchitem = txtsearch.Text

    txtsearch.Clear()
    txtfirstnameresult.Clear()
    txtsecondnameresult.Clear()
    txtphonenumberresult.Clear()
    txtaddressresult.Clear()
    txtidresult.clear()
    txtdobresult.Clear()
    txtgenderresult.Clear()
    txtweightresult.Clear()
    txtheightresult.Clear()

    FileOpen(1, "runners.txt", OpenMode.Input)

    count = 0

    While Not EOF(1)
    count = count + 1

    Input(1, runnerdetails2(count).firstname)
    Input(1, runnerdetails2(count).secondname)
    Input(1, runnerdetails2(count).phonenumber)
    Input(1, runnerdetails2(count).address)
    Input(1, runnerdetails2(count).runnerid)
    Input(1, runnerdetails2(count).dob)
    Input(1, runnerdetails2(count).gender)
    Input(1, runnerdetails2(count).weight)
    Input(1, runnerdetails2(count).height)


    End While

    FileClose(1)

    numberofitemsinarray = count

    count = 0

    runnerfound = False

    While (count < numberofitemsinarray) And (runnerfound = False)

    count = count + 1

    If runnerdetails2(count).runnerid = searchitem Then
    runnerfound = True
    End If

    End While

    If runnerfound = True Then


    txtfirstnameresult.Text = runnerdetails2(count).firstname
    txtsecondnameresult.Text = runnerdetails2(count).secondname
    txtphonenumberresult.Text = runnerdetails2(count).phonenumber
    txtaddressresult.Text = runnerdetails2(count).address
    txtidresult.Text = runnerdetails2(count).runnerid
    txtdobresult.Text = runnerdetails2(count).dob
    txtgenderresult.Text = runnerdetails2(count).gender
    txtweightresult.Text = runnerdetails2(count).weight
    txtheightresult.Text = runnerdetails2(count).height

    Else
    MsgBox("The runner you are searching for dosent exist.")

    txtsearch.Clear()
    txtfirstnameresult.Clear()
    txtsecondnameresult.Clear()
    txtphonenumberresult.Clear()
    txtaddressresult.Clear()
    txtidresult.Clear()
    txtdobresult.Clear()
    txtgenderresult.Clear()
    txtweightresult.Clear()
    txtheightresult.Clear()
    End If
    End Sub

    Private Sub btnammenddetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnammenddetails.Click

    runnerdetails2(count).firstname = txtfirstnameresult.Text
    runnerdetails2(count).secondname = txtsecondnameresult.Text
    runnerdetails2(count).phonenumber = txtphonenumberresult.Text
    runnerdetails2(count).address = txtaddressresult.Text
    runnerdetails2(count).runnerid = txtidresult.Text
    runnerdetails2(count).dob = txtdobresult.Text
    runnerdetails2(count).gender = txtgenderresult.Text
    runnerdetails2(count).weight = txtweightresult.Text
    runnerdetails2(count).height = txtheightresult.Text

    FileOpen(1, "runners.txt", OpenMode.Output)

    count = 0

    While count <> numberofitemsinarray <----------- Seem to be getting the problem here highlighted in red saying "private numberofitemsin array as integer"
    count = count + 1
    WriteLine(1, runnerdetails2(count).firstname, runnerdetails2(count).secondname, runnerdetails2(count).phonenumber, runnerdetails2(count).address, runnerdetails2(count).runnerid, runnerdetails2(count).dob, runnerdetails2(count).gender, runnerdetails2(count).weight, runnerdetails2(count).height)

    End While

    FileClose(1)

    txtsearch.Clear()
    txtfirstnameresult.Clear()
    txtsecondnameresult.Clear()
    txtphonenumberresult.Clear()
    txtaddressresult.Clear()
    txtidresult.Clear()
    txtdobresult.Clear()
    txtgenderresult.Clear()
    txtweightresult.Clear()
    txtheightresult.Clear()
    End Sub


    NOTE: This is only the code on that particular form.

  2. #2
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Struggling Need Help

    You could really do with cleaning up your code and making it shorter. And FileOpen is a old method to open files. I personally hate it. I prefer using IO.File.ReadlAllLines()
    and use a list not an array for this.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    2

    Struggling Need Help

    I found the problem just needed to ignore it stopping me. The only reason why i use open file is because that's what I've been taught at A level and I don't want to stray to far from that thanks though.

  4. #4
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Struggling Need Help

    "I've been taught at A level and I don't want to stray to far from that thanks though." You're usually taught bad coding habits in school, and you should not be using FileOpen. Like I can see you have been taught to use MsgBox() when you should really be using MesssageBox.Show() instead. But not only is IO.File.ReadAllLines() easier to use, but it helps to maintain cleaner code in this situation.

    You can just do.

    Dim lines() As String = IO.File.ReadAllLines("runners.txt")
    then go on from there.

    Sadly, I do not know the format of the data in your .txt file so I cannot do anything further.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: Struggling Need Help

    numberofitemsinarray is a pretty useless variable as far as I can see. It's a bit hard to read that code since it isn't formatted. You can format by selecting the code and pressing the # button (though you probably have to get a few more posts before you are allowed to edit the first post to add the tags). As far as I can tell, the sole purpose of numberofitemsinarray is exactly what the variable sounds like: A count of the number of items in the array. You don't need that, because you always have it. Furthermore, keeping it in a form level variable like that means that if the array changes you HAVE to keep it in sync. Why not just use runnerdetails2.Length instead?

    Another point you might consider is using some camel casing for names like those variables. Make the variable numberOfItemsInArray and runnerDetails2. This is somewhat easier to read. Better still, you then type the variable names in all lower case and VB will case correct them. If VB does NOT case correct, then you have an error in the line that you can't ignore (though it is rare to have an error that doesn't result in the squiggly underline).
    My usual boring signature: Nothing

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Struggling Need Help

    Quote Originally Posted by BenBilly View Post
    I found the problem just needed to ignore it stopping me. The only reason why i use open file is because that's what I've been taught at A level and I don't want to stray to far from that thanks though.
    i'd change your teacher.

  7. #7
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Struggling Need Help

    OP are you telling me your teacher didn't teach you how to use For Each Loops or the use of subroutines. You do know you could just do.

    Code:
    Private Sub ClearTxtControls()
     For Each txtbox As TextBox In Me.Controls.OfType(Of TextBox)()
      txtbox.Clear()
     Next
    End Sub
    And you could call that sub rotuine in your code. That saves you from doing.
    Code:
    txtsearch.Clear()
    txtfirstnameresult.Clear()
    txtsecondnameresult.Clear()
    txtphonenumberresult.Clear()
    txtaddressresult.Clear()
    txtidresult.Clear()
    txtdobresult.Clear()
    txtgenderresult.Clear()
    txtweightresult.Clear()
    txtheightresult.Clear()
    three times.
    Last edited by Toph; Dec 20th, 2014 at 08:17 PM.

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Struggling Need Help

    If you are seriously being taught VB6 legacy functions like fileopen at A level i would quit your course. You're programming in VB not classic vb. Replace all old functions (like FileOpen, FreeFile etc) with the appropriate .Net classes.

    MSDN http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx

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