|
-
Apr 22nd, 2008, 10:42 AM
#1
Thread Starter
Addicted Member
[RESOLVED] [2005] Add Collection to array of string
Hi,
I have an string array such as:
string1() = {"qw", "1", "test"}
and I have a collection from the My.settings
so it's my.settings.collection1
It's a 1D collection which means the content of collection is
"asdasda"
"as"
"aawerfwe"
...
How can i copy all of collection1 in string1 ?
thanks !
-
Apr 22nd, 2008, 10:52 AM
#2
Re: [2005] Add Collection to array of string
The first thing you need to do is increase the size of string1. Then you need to use the CopyTo method of the collection (which I assume is also an array of strings) to add the contents of collection1 to string1:
Code:
Dim string1As String() = {"1", "2", "3"}
Dim len As Integer = string1.Length
ReDim Preserve string1(len + my.settings.collection1.Length - 1)
my.settings.collection1.CopyTo(string1, len)
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
Apr 22nd, 2008, 10:58 AM
#3
Re: [2005] Add Collection to array of string
Why not use a List(Of String) instead of a string array?
-
Apr 22nd, 2008, 12:01 PM
#4
Thread Starter
Addicted Member
Re: [2005] Add Collection to array of string
Why not use a List(Of String) instead of a string array?
Not sure... did not know of.
I just used whatever that I know of... 
PS: instead of "my.settings.collection1.Length" i used .Count
thanks !
-
Apr 22nd, 2008, 12:42 PM
#5
Re: [RESOLVED] [2005] Add Collection to array of string
or just a good old fashioned Array
Code:
Dim tstArray() As String
'My.Settings.arraySTR is String
Private Sub TEST()
Dim samplD As String = "12,13,a,b,1,2,3,4,5,6,7,8,9"
Dim sepCH As String = Convert.ToChar(255) 'seperator character
tstArray = My.Settings.arraySTR.Split(Convert.ToChar(sepCH)) 'load array from my.settings
If My.Settings.arraySTR = Nothing Or tstArray.Length = 0 Then 'first time?
Debug.WriteLine("SAMPLE") 'yes
tstArray = samplD.Split(","c) 'load array with sample/default data
End If
'add new item
Array.Resize(tstArray, tstArray.Length + 1)
tstArray(tstArray.Length - 1) = "LAST"
Array.Sort(tstArray) 'sorting strings !!!
For x As Integer = 0 To tstArray.Length - 1
Debug.WriteLine(x.ToString & " " & tstArray(x))
Next
My.Settings.arraySTR = String.Join(sepCH, tstArray) 'save in my.settings
Debug.WriteLine(My.Settings.arraySTR)
End Sub
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
|