[RESOLVED] Help me please... PLEASE!
Ok, so i am trying to make a program that holds info in the settings... it holds it in an array so that you can add to it and take away from it... he is the code i have so far....
So far it is just the adding part but im still haveing some problems... help me please!
Code:
Public Class Form1
Public Function array_use1(ByVal array_string As String, ByVal number As Integer)
Dim num_holder As Integer = number
Dim num_holder2 As Integer = number
Dim users_string As String = array_string
Dim run(num_holder) As String
If users_string = "" Then
Return (run)
Else
Dim holder As Integer = 0
Dim numbre As String = ""
Do Until num_holder = 0
If Not holder = users_string.Length + 1 Then
Dim a As String = users_string.Chars(holder)
If a = "," Then
If Not numbre = "" Then
run(num_holder) = numbre
numbre = ""
a = ""
num_holder -= 1
End If
Else
numbre = numbre & a
End If
Else
End If
holder += 1
Loop
Return (run)
End If
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim new1 As String = TextBox2.Text
Dim intloop As Integer = My.Settings.Number
Dim j As String = ""
If Not intloop = 0 Then
Do Until intloop = 0
j = j & array_use1(My.Settings.Users, My.Settings.Number)(intloop) & ","
intloop -= 1
Loop
j = j & new1
My.Settings.Users = j
Dim h As Integer = My.Settings.Number
Dim i As Integer = h + 1
My.Settings.Number = i
Else
My.Settings.Users = TextBox2.Text & ","
My.Settings.Number = 1
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = ""
Dim l As Integer = My.Settings.Number
If Not l = 0 Then
Do Until l = 0
TextBox1.Text = TextBox1.Text & vbNewLine & array_use1(My.Settings.Users, My.Settings.Number)(l)
l -= 1
Loop
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
My.Settings.Number = 0
My.Settings.Users = ""
End Sub
End Class
i keep getting an error in the function at this line:
Code:
Dim a As String = users_string.Chars(holder)
This is the error:
http://thefiscster510.hostzi.com/test/error.bmp
Please help... im trying to get this done so i can demonstrate how it works to some friends. Thankyou for ANY help.
And also note that i dont want you to like write out a bunch of code then tell me its the right way... i would REALLY like it explained... but either works... thanks.
Re: Help me please... PLEASE!
The error is specific to you trying to access a character in the value of users_string, and the value of your 'holder' variable is greater than the number of characters in the string (minus 1, since indexes start at 0).
So if I had a string like this:
Dim myString = "ABC"
So:
myString.Chars(0) 'A
myString.Chars(1) 'B
myString.Chars(2) 'C
myString.Chars(3) 'IndexOutOfRangeException
Re: Help me please... PLEASE!
so then would i make the if statement:
Code:
If Not holder = users_string.Length - 1 Then
'blah blah blahy
else
end if
???
Re: Help me please... PLEASE!
I would suggest that you look into the List (of String). If you will be adding or removing strings from the collection, the List will work FAR better than an array, especially because it has Add, RemoveAt, and Insert methods to put strings into places and remove them from locations. You don't have to deal with sizing the thing, either.
However, if you want to stick with the array, there are a couple points I would make about the code you have. The first is that you pass in an argument called array_string as a string. Is that really a string, or is it supposed to be an array of strings?
Second, you don't have a return value from the function. I'm not sure how you can even compile that, as you generally need to be returning something, and you are clearly returning an array of strings.
However, the problem that is causing the error is here:
Code:
If Not holder = users_string.Length + 1 Then
Dim a As String = users_string.Chars(holder)
The string is a 0-based array of characters, so it runs from 0 as the first character up to length - 1 as the highest character. I would say that you can get it to work just by removing the +1, as that would mean that your loop will end when holder is equal to the length.
Re: Help me please... PLEASE!
Get rid of the -1, or change the logic such that you loop until holder <= length -1
My last reply was way too slow.
Re: Help me please... PLEASE!
Would i be able to store the List (of String)'s data in the programs settings?
Re: Help me please... PLEASE!
Re: Help me please... PLEASE!
Actually, on second thought, you might just want to use a StringCollection since that is the main string collection type you will find in the dropdown list in my settings. I don't think you can specify a generic list type directly (you have to make your own class that inherits from List(Of String) and then you can use that class you made as the data type for the settings), but that seems like an extra step if you just want to store strings.
So if your setting was of type System.Collections.Specialized.StringCollection, you can write code like this:
Code:
Dim myUsers As New Collections.Specialized.StringCollection
myUsers.Add("John")
myUsers.Add("Bob")
myUsers.Add("Mike")
My.Settings.MyUsers = myUsers
My.Settings.Save()
Re: Help me please... PLEASE!
Thank you all so much! that string collection will definitely come in handy. however i have found what my problem was... this is how the function should have been.
Code:
Dim num_holder As Integer = number
Dim num_holder2 As Integer = number
Dim users_string As String = array_string
Dim run(num_holder) As String
If users_string = "" Then
Return (run)
Else
Dim holder As Integer = 0
Dim numbre As String = ""
Do Until num_holder = 0
If Not holder = users_string.Length Then
Dim a As String = users_string.Chars(holder)
If a = "," Then
If Not numbre = "" Then
run(num_holder) = numbre
numbre = ""
a = ""
num_holder -= 1
End If
Else
numbre = numbre & a
End If
Else
End If
holder += 1
Loop
Return (run)
End If
and this is what the problem was... i forgot a comma in the add user button code... here is the CORRECT code...
Code:
Dim new1 As String = TextBox2.Text
Dim intloop As Integer = My.Settings.Number
Dim j As String = ""
If Not intloop = 0 Then
Do Until intloop = 0
j = j & array_use1(My.Settings.Users, My.Settings.Number)(intloop) & ","
intloop -= 1
Loop
j = j & new1 & ","
My.Settings.Users = j
Dim h As Integer = My.Settings.Number
Dim i As Integer = h + 1
My.Settings.Number = i
Else
My.Settings.Users = TextBox2.Text & ","
My.Settings.Number = 1
End If
Thank you all so much for your suggestions :D