Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Beginner and stuck on something ridiculuosly easy

So far my byvals and strings are causing errors when i try to run it.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim name() As String = {kildare & spock & roberts & zhivago}
ListBox1.Items.Add(addtitle(name))
End Sub
Sub addtitle(ByVal a() As String, ByVal b() As String, ByVal c() As String, ByVal d() As String)
Dim name, i As Integer
name = "Dr."
For i = LBound(a) To UBound(d)
Next

End Sub
End Class
The only substantial piece of code i know that i need is:

For i = LBound(name) To UBound(name)

lstOutput.Items.Add(name(i))

Next

but i'm a beginer and don't know what the **** im doing.. so any ideas here is the problem:
Write a Sub called AddTitle that takes an array of String as a parameter and, for each String, adds the prefix "Dr." Use GetUpperBound so that your Sub works on any size array. Write a test program for your Sub that creates an array of the following strings, calls the Sub, and then displays the results. Note that the AddTitle Sub modifies the Strings in the array by concatenating "Dr." onto each existing String; it produces no output itself. Do not simply output "Dr." followed by the String in the array.
"Kildare," "Zhivago," "Spock," "Roberts"

HELP ME PLEASE!!!!!
[1383 byte] By [shaystone] at [2007-11-11 8:45:14]
# 1 Re: Beginner and stuck on something ridiculuosly easy
This line of code:

Dim name() As String = {kildare & spock & roberts & zhivago}

Creates an array with a single element: kildarespockrobertszhivago. If you want a four-element array, you must separate the elements with commas (and you should surround the values with quotes):

Dim name() As String = {"kildare", "spock", "roberts", "zhivago"}

Can you explain why your AddTitle function accepts four arrays? It looks like you only want to pass one array (name).
Phil Weber at 2007-11-11 21:47:03 >
# 2 Re: Beginner and stuck on something ridiculuosly easy
I am having the same issue... This is for a class assignment and I'm having some problems. Could someone take a look at my code below, and give me some pointers?

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim name() As String = {"kildare", "spock", "roberts", "zhivago"}
addtitle(name)
End Sub
Sub addtitle(ByRef name As String)
Dim add As String = ("Dr.")
Dim i As Integer
lstOutput.Items.Clear()
For i = 1 To UBound(name)
lstOutput.Items.Add(add & Trim(name(i)))
Next i
End Sub
End Class
jlavetan at 2007-11-11 21:48:09 >
# 3 Re: Beginner and stuck on something ridiculuosly easy
Your AddTitle method accepts a single string value; you want it to accept an array. Try this:

Sub addtitle(ByRef name() As String)
Phil Weber at 2007-11-11 21:49:03 >
# 4 Re: Beginner and stuck on something ridiculuosly easy
Thanks... that was it... I had another little code change, but got it working with:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim name() As String = {"kildare", "spock", "roberts", "zhivago"}
addtitle(name)
End Sub
Sub addtitle(ByRef name() As String)
Dim add As String = ("Dr.")
Dim i As Integer
lstOutput.Items.Clear()
For i = LBound(name) To UBound(name)
lstOutput.Items.Add(add & Trim(name(i)))
Next i
End Sub
End Class
jlavetan at 2007-11-11 21:50:08 >
# 5 Re: Beginner and stuck on something ridiculuosly easy
Thanks... that was it... I had another little code change, but got it working with:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim name() As String = {"kildare", "spock", "roberts", "zhivago"}
addtitle(name)
End Sub
Sub addtitle(ByRef name() As String)
Dim add As String = ("Dr.")
Dim i As Integer
lstOutput.Items.Clear()
For i = LBound(name) To UBound(name)
lstOutput.Items.Add(add & Trim(name(i)))
Next i
End Sub
End Class

>>Note that the AddTitle Sub modifies the Strings in the array by concatenating "Dr." onto each existing String; it produces no output itself. Do not simply output "Dr." followed by the String in the array.

You are not modifying the values in the array, you are just looping through the array and concatenating the add variable and the array value into your listbox. You want to do something like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim name() As String = {"kildare", "spock", "roberts", "zhivago"}
addtitle(name)
lstOutput.datasource = name
End Sub
Sub addtitle(ByRef name() As String)
Dim add As String = ("Dr. ")
Dim i As Integer
For i = LBound(name) To UBound(name)
name(i) = (add & Trim(name(i)))
Next i
End Sub

Here you are modifying the values in the array and then assigning it to the listboxs datasource. Note the space after the "." in add. This will display Dr. properly.
joewmaki at 2007-11-11 21:51:10 >