views:

407

answers:

2

Hey i'm having problems creating a simple button for a programme which finds the largest word in an array and puts it into a textbox. I've done most of the coding (I hope) was wondering if somebody could help me actually with the code that finds the largest text in the array as I am struggling the most with that.

    Private Sub btnLongName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLongName.Click
        Dim LongName As String
        Dim LengthOfLongestName As Integer
        Dim UltimateName As String


        For i As Integer = 0 To 5
            LongName = Members(i).Name
            LengthOfLongestName = Len(LongName)
            If Members(i).Name.Length > LengthOfLongestName Then



            End If
        Next i

        txtResult.Text = "The longest name is " & UltimateName & " ."
    End Sub
End Class

Thanks for your time - its for college homework, struggling big time on it :(

edit: I've edited the code

+9  A: 

Since this is homework, I won't write the code for you; instead I'll try to give you some hints that will point you in the right direction.

  1. Declare a variable of an appropriate type to hold the <longest value so far>, initialize it with the "shortest" value for that type.
  2. Loop through all the values in the array (perhaps with a For or For Each loop)

Pseudo-code for the inside your loop:

If the Length of <the value being checked> exceeds _
   the Length of the <longest value so far> Then 

   Assign <the value being checked> to the <longest value so far> 

End If

When the loop finishes, the <longest value so far> will be the longest value in the array.

Notes

  • You can use MSDN as a reference on how to use a For loop or a For Each loop (If you haven't learned For loops yet, you can also use a Do Loop)
  • <the value being checked> will be different on each iteration through the loop; it should correspond to each consecutive value in your array. You can verify that this is working by setting a breakpoint.
  • You can get the length of a string by saying myString.Length
  • If you've learned about Functions, consider writing a function that takes an array as a parameter, and returns the longest value in the array.
  • There are certainly ways you could do this with LINQ, but I don't think that is the goal of the assignment ;-]


In response to Edit 1:

  • Your If statement needs to be inside of some sort of loop (For, For Each, Do, etc) I think this is the key concept that you are missing.
  • Instead of comparing LongName.Length to LengthOfLongestName, you need to compare the length of an entry in your array to LengthOfLongestName
  • You're on the right track with Members(0).Name.Length, but you can't just check element 0; you have to check every element in the array.
  • Given your current code, you'll probably be assigning <An entry in your array>.Name to LongName
  • The last index in a one-dimensional array is <array>.Length - 1 or <array>.GetUpperBound(0).

The following doesn't really address anything in your assignment, but I hope it will give you some ideas on how to go through all the items in your list:

' A For loop that does a message box for each of the numbers from 0 to 5 '
For i as Integer = 0 To 5
   MessageBox.Show(i)
Next i

' Code that does a message box with the names of the 2nd, 3rd and last '
' entries in Members '
' (Remember that the first item is at 0, the second item is at 1, etc...) '
MessageBox.Show(Members(1).Name)
MessageBox.Show(Members(2).Name)
MessageBox.Show(Members(Members.GetUpperBound()).Name)


In response to Edit 2:

You're getting warmer...

  • You should only update LongName and LengthOfLongName if the current value is the longest you've seen so far (i.e. they should be assigned inside of the If statement)
  • You have to go to the last index of the array, not 5. See above (the response to your first edit) on how to get that last index.
  • You don't really need the UltimateName variable; you can just use LongName ;-]
  • You might want to use <stringVariable>.Length instead of Len(<stringVariable>) to be consistent.
Daniel LeCheminant
In this case, you could initialize it to be length = -1 to start.
NoahD
I think what this comes down to is me not doing enough Revision >< - I'm trying desperately but I seem to be falling over myself in trying to figure out which variable is equal to this or should be checked against that one etc. I'll paste what I've got so far in a min
Haroon Ghazni
@Haroon: If you post your code, I'm glad to offer additional tips/pointers ...
Daniel LeCheminant
Ok i've edited the code at the top in my main post Daniel, don't bite my head off :@ I know its bad!
Haroon Ghazni
Aww, I hope I don't come across as the kind of person that would bite your head off :-/ I was once a student too; I remember how it can be!
Daniel LeCheminant
Yeah it's just that we have a teacher who teaches us VB who LOVES to shout and mope we if get something wrong, or if we attempt to do the code and make the slightest error - However the messageBoxes, erm I don't think that's what we're meant to do for the programme. Just show it in the txtbox
Haroon Ghazni
@Haroon: I understand; I'm just trying to give you an example of how a for loop works, and how to index into an array (without showing you exactly how to do it for your assignment)
Daniel LeCheminant
@ Daniel: Ok i've edited the code again in my original post just wanted to check if that was right so far D:
Haroon Ghazni
Hmm I've removed the unneeded variables but I don't think i'll have this ready by tomorrow morning (which is when it is in for) since its already 1:12am (doh) But thanks so much Daniel for trying to help! Maybe if we had more time I could have cracked it with your guidance but as it stands :(
Haroon Ghazni
@Daniel: Anyways gn :)
Haroon Ghazni
+1  A: 

What you are missing is a loop that checks each member, and putting the If statement inside it and make it compare the length of the name to the longest name that you have found so far. If the name is longer, put it in the variable for the longest found, and update the length variable.

You can either initialise the variables with the name of the first member and loop from the second member and on, or you can initialise the variables with an empty string and loop all the members. Personally I prefer the latter one.

Guffa