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.
- Declare a variable of an appropriate type to hold the
<longest value so far>
, initialize it with the "shortest" value for that type.
- 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
Function
s, 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.