Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Add Collection to array of string

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    174

    Resolved [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 !

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    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>.

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Add Collection to array of string

    Why not use a List(Of String) instead of a string array?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    174

    Resolved 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 !

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width