none
Need help with a function for a visual basic program RRS feed

  • Question

  • Hi guys,

    So im a bit stuck at the moment. A program that I have to complete REQUIRES me to use a function in order to return a value (in this case the value would be the furthest distance walked by someone in a walking club, as you'll see in the code) I've consulted google for answers but cannot seem to find any suitable solutions. So within the function FindMax i would need to write a piece of code which would go through each member in the array and find out which one of them has the highest value (so find max procedure i guess). Once that is found the function would return the highest value to me. Thanks in advance for reading.

    Structure Information 'Declares the record structure'
            'Declares the public variables for use inside the record structure'
            Public firstname As String
            Public lastname As String
            Public distance As Single
        End Structure 'Ends the record structure'
    
        'Declares the variables for use inside sub programs'
        Dim current_distance As Single
        Dim furthest As Single
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim walkingdata(20) As Information 'Declares the array which the information from the members file will be stored in'
    
            Dim inputfile = IO.File.OpenText("C:\Users\Tristan\Desktop\Computing Assignment\Software Development\members.txt") 'Reads in members file'
            Dim Members_array As String() = inputfile.ReadLine().Split(","c)
            Do Until inputfile.EndOfStream 'Begins a loop'
    
                Dim lineparts() As String = inputfile.ReadLine.Split(","c)
    
    
                walkingdata(20).firstname = (lineparts(0))
                walkingdata(20).lastname = (lineparts(1))
                walkingdata(20).distance = (lineparts(2))
    
                'Adds the walkers first name, last name and their total distance walked to seperate listboxes'
                ListBox1.Items.Add(walkingdata(20).firstname)
                ListBox2.Items.Add(walkingdata(20).lastname)
                ListBox3.Items.Add(walkingdata(20).distance)
    
            Loop 'ends the loop'
    
            inputfile.Close() 'closes the members file'
    
        End Sub
    
        Function FindMax(ByVal distance As Single, ByVal inputfile As Single) 'function used to find the maximum distance walked by a member'
    
    
    
    
    
    
    
    
            Return [furthest]
        End Function 'ends the function'
    End Class

    Tuesday, March 5, 2019 9:40 PM

All replies

  • Hi

    I have tried your code and most of it worked fine - however, there were one or two areas that meeded some manipulation for me to get a working copy.

    Below is an altered version of your code with some notification of what changes made (some are just relocation of declarations)

    The FindMax Function Returns an index into the (new) AllData List. The AllDataList is a storage for the incoming walkers data - I would suggest this as you do need to store the data as it is read in from file so that you can work with it.

    The code below is NOT ideal. There would be quite a few areas that could be improved - however, I have tried to stick with your code where I could.

    This code also uses an extra 3 Labels to display the result (maximum walker data) - you didn't provide any alternative so I was inventive.

    *

    Image

    Code

    ' Form1 with 3 ListBoxes
    ' 3 Labels, and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Structure Information 'Declares the record structure'
        'Declares the public variables for use inside the record structure'
        Public firstname As String
        Public lastname As String
        Public distance As Single
      End Structure 'Ends the record structure'
    
      'Declares the variables for use inside sub programs'
      Dim current_distance As Single
      Dim furthest As Single
      Dim walkingdata As Information 'Declares the array which the information from the members file will be stored in'
    
      ' ADDED *******************
      ' storage for all the walkers
      Dim AllData As New List(Of Information)
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        'Reads in members file'
        Dim inputfile As IO.StreamReader = IO.File.OpenText(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop & "\TestFile.txt"))
    
        Dim Members_array As String() = inputfile.ReadLine().Split(","c)
        Do Until inputfile.EndOfStream 'Begins a loop'
    
          Dim lineparts() As String = inputfile.ReadLine.Split(","c)
    
    
          walkingdata.firstname = (lineparts(0))
          walkingdata.lastname = (lineparts(1))
          walkingdata.distance = CSng(lineparts(2))
    
          'Adds the walkers first name, last name and their total distance walked to seperate listboxes'
          ListBox1.Items.Add(walkingdata.firstname)
          ListBox2.Items.Add(walkingdata.lastname)
          ListBox3.Items.Add(walkingdata.distance)
    
          ' ADDED *******************
          AllData.Add(walkingdata)
        Loop 'ends the loop'
    
        inputfile.Close() 'closes the members file'
    
        ' ADDED *******************
        Dim max As Information = AllData(FindMax)
        ' display the 3 data items of the
        ' maximum distance walker
        Label1.Text = max.firstname
        Label2.Text = max.lastname
        Label3.Text = max.distance.ToString
      End Sub
    
      Function FindMax() As Integer 'function used to find the maximum distance walked by a member'
        Dim Max As Single = 0
        Dim maxID As Integer = 0
        For ind As Integer = 0 To AllData.Count - 1
          If AllData(ind).distance > Max Then
            Max = AllData(ind).distance
            maxID = ind
          End If
        Next
        ' return the index of the maximum
        ' distance in the AllData list
        Return maxID
      End Function 'ends the function'
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Tuesday, March 5, 2019 10:48 PM
    Tuesday, March 5, 2019 10:45 PM
  • Thanks so much man that solution works !!!
    Tuesday, March 5, 2019 10:59 PM
  • Hi,

    I am glad you have got your solution, please mark the useful reply as answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 6, 2019 1:58 AM