|
-
Dec 26th, 2003, 11:36 AM
#1
Thread Starter
Frenzied Member
looping to assign an array [RESOLVED]
I have an array array(2) that will store 3 values. - 0,1, and 2 (that IS right, i think)
I need to assign array(0) a value that is returned by an indexof() function and repeat this two more times.
how can I make the index number increase by one when assigning the array?
array(0) = first occurance
array(1) = 2nd
array(2) - 3rd
Any suggestions?
Last edited by Andy; Feb 11th, 2004 at 09:54 PM.
-
Dec 26th, 2003, 11:54 AM
#2
Sleep mode
Note : VB.NET , C#.NET arrays are zero-based . Meaning the first element is 0 , next is 1 . You can iterate through the array by assigning the index property of the array to get the specific element .
Example :
All elements in the array are initialized with zero . Note how it adds 1 to each element .
VB Code:
Private Sub anotherTest()
Dim myar(2) As String
For i As Integer = 0 To 2
'Here you can assign whatever to your array elemnts
'Like this
myar(i) += i
MessageBox.Show(myar(i))
Next
End Sub
-
Dec 26th, 2003, 06:58 PM
#3
Thread Starter
Frenzied Member
what I tried was close. I had the the syntax a bit wrong! I am starting to get this@!!@
-
Dec 26th, 2003, 08:26 PM
#4
Do you mean adding elements to the array after its declared? In other words changing it from 3 elements to 4 and so on. If that is what you mean than you can't declare it a fixed size at the begining but its easier to just use an ArrayList object instead. Otherwise if you are just talking about assigning values to existing elements then Pirates code should work fine.
-
Dec 26th, 2003, 10:06 PM
#5
Thread Starter
Frenzied Member
right. I know all I need is 3 so I am going to assign 0,1 and 2 unknown values. Pirate's been a BIG help lately.
-
Dec 26th, 2003, 10:29 PM
#6
-
Dec 26th, 2003, 10:37 PM
#7
Sleep mode
-
Dec 29th, 2003, 02:08 AM
#8
Thread Starter
Frenzied Member
great. I got my array to get the needed info. What I'm thinking now is to make this code into a function. More code will take the integers that the array is assigned and extract strings out of another string. Can I return more than one thing from a function?
ie:
return strArtist, strAlbum, strTrackNum
(is that even how to do it?)
Here is what I have so far if anyone cares to peruse it:
Code:
Public Function FindDashes(ByVal str) As Integer
'declare the variables first
Dim start, at, [end], count, myar(3), i As Integer
'initialize the variables
[end] = str.Length
start = 3
count = 0
at = 0
i = 0
'go through the string and find and mark all the dashes
While start <= [end] AndAlso at > -1 ' start+count must be a position within -str-.
count = [end] - start
at = str.IndexOf("-", start, count)
myar(i) = at
If at = -1 Then
Exit While
End If
'erase this, it's just for debugging
Console.WriteLine("{0}", at)
Console.WriteLine("myar(" & i & ") = " & myar(i))
i = i + 1
start = at + 1
End While
Return strArtist, strAlbum, strTrackNum
End Function
keeping in mind that the code to get the string info hasn't been added yet.
-
Dec 29th, 2003, 05:53 AM
#9
Sleep mode
No , you can't return more than one value at a time . This can be done in C# , but alternatively , use ArrayList or string array to return the extracted strings that you mentioned . For the rest , I'm not sure what you mean exactly .
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|