Reputation: 380
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
Upvotes: 0
Views: 743
Reputation: 51091
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.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
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.myString.Length
Function
s, consider writing a function that takes an array as a parameter, and returns the longest value in the array.In response to Edit 1:
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.LongName.Length
to LengthOfLongestName
, you need to compare the length of an entry in your array to LengthOfLongestName
Members(0).Name.Length
, but you can't just check element 0
; you have to check every element in the array.<An entry in your array>.Name
to LongName
<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...
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)UltimateName
variable; you can just use LongName
;-]<stringVariable>.Length
instead of Len(<stringVariable>)
to be consistent.Upvotes: 10
Reputation: 700552
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.
Upvotes: 2