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
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.
<longest value so far>, initialize it with the ‘shortest’ value for that type.FororFor Eachloop)Pseudo-code for the inside your loop:
When the loop finishes, the
<longest value so far>will be the longest value in the array.Notes
Forloop or aFor Eachloop (If you haven’t learnedForloops yet, you can also use aDo 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.myString.LengthFunctions, consider writing a function that takes an array as a parameter, and returns the longest value in the array.In response to Edit 1:
Ifstatement 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.LongName.LengthtoLengthOfLongestName, you need to compare the length of an entry in your array toLengthOfLongestNameMembers(0).Name.Length, but you can’t just check element0; you have to check every element in the array.<An entry in your array>.NametoLongName<array>.Length - 1or<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:
In response to Edit 2:
You’re getting warmer…
LongNameandLengthOfLongNameif the current value is the longest you’ve seen so far (i.e. they should be assigned inside of theIfstatement)UltimateNamevariable; you can just useLongName;-]<stringVariable>.Lengthinstead ofLen(<stringVariable>)to be consistent.