Results 1 to 2 of 2

Thread: Converting List Collection & String

  1. #1

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    95

    Question Converting List Collection & String

    I am trying to configure a export settings option in my program. I have everything completed except for the my.settings that are list collections. I converted them to a string prior to exporting them and when a user imports it, it needs to go back to a list collection to be added to the app.config. Here is my code:

    Export Option

    VB Code:
    1. Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
    2.         Dim M_Count As String = String.Join("*", My.Settings.M_Count)
    3.         Dim M_IPs As String = String.Join("*", My.Settings.M_IPs)
    4.         Dim M_Hostnames As String = String.Join("*", My.Settings.M_Hostnames)
    5.         Dim M_Descriptions As String = String.Join("*", My.Settings.M_Descriptions)
    6.         Dim M_Results As String = String.Join("*", My.Settings.M_Results)
    7.         Dim M_Exclusions As String = String.Join("*", My.Settings.M_Exclusions)
    8.  
    9.         Dim SaveFile As New SaveFileDialog
    10.         SaveFile.FileName = ""
    11.         SaveFile.Filter = "Client Watch Settings (*.k22cw)|*.k22cw"
    12.         SaveFile.Title = "Save"
    13.         SaveFile.ShowDialog()
    14.         Try
    15.             File.WriteAllText(SaveFile.FileName, String.Join("|", New String() {My.Settings.S_Email, My.Settings.S_Password, My.Settings.S_Port,
    16.                                                              My.Settings.S_SendTo, My.Settings.S_Hostname, M_IPs, M_Hostnames, M_Descriptions, M_Results,
    17.                                                              M_Exclusions, My.Settings.S_PingEveryM, My.Settings.Stats_Count, My.Settings.M_FailedIPs,
    18.                                                              My.Settings.M_SentCount, My.Settings.S_Tolerence, My.Settings.Logs, My.Settings.Stats_RunTime,
    19.                                                              My.Settings.Stats_SentAlerts, My.Settings.Stats_YTDfail, My.Settings.Stats_YTDsuccess}))
    20.         Catch ex As Exception
    21.             MsgBox(ex.Message)
    22.         End Try
    23.  
    24.  
    25.     End Sub

    Import Option *This is where I am having the issue of converting back to a list collection
    vb Code:
    1. Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
    2.         Dim OpenFile As New OpenFileDialog
    3.         OpenFile.FileName = ""
    4.         OpenFile.Filter = "Client Watch Settings (*.k22cw)|*.k22cw"
    5.         OpenFile.Title = "Open"
    6.         OpenFile.ShowDialog()
    7.         Try
    8.             Dim values() As String = File.ReadAllText(OpenFile.FileName).Split("|"c)
    9.             My.Settings.S_Email = values(0)
    10.             My.Settings.S_Password = values(1)
    11.             My.Settings.S_Port = values(2)
    12.             My.Settings.S_SendTo = values(3)
    13.             My.Settings.S_Hostname = values(4)
    14.             Dim M_IPs As String = String.Join(",", values(5).ToArray())
    15.             Dim M_Hostnames As String = values(6)
    16.             Dim M_Descriptions As String = values(7)
    17.             Dim M_Results As String = values(8)
    18.             Dim M_Exclusions As String = values(9)
    19.             My.Settings.S_PingEveryM = values(10)
    20.             My.Settings.Stats_Count = values(11)
    21.             My.Settings.M_FailedIPs = values(12)
    22.             My.Settings.M_SentCount = values(13)
    23.             My.Settings.S_Tolerence = values(14)
    24.             My.Settings.Logs = values(15)
    25.             My.Settings.Stats_RunTime = values(16)
    26.             My.Settings.Stats_SentAlerts = values(17)
    27.             My.Settings.Stats_YTDfail = values(18)
    28.             My.Settings.Stats_YTDsuccess = values(19)
    29.  
    30.  
    31.             Dim M_Ips2 As String() = M_IPs.Split("*")
    32.             My.Settings.M_IPs = M_Ips2.ToList   'This is where the error is happening. I haven't added the other settings yet...
    33.  
    34.         Catch ex As Exception
    35.  
    36.         End Try
    37.     End Sub

    The goal is to export the app.config and be able to import it. I tried following this article: http://vbcity.com/forums/t/163200.aspx however, it became more of a mess than what it was worth and figured I could accomplish the same result... but with more leg work of course. So that's where converting a list collection to a string came into play because some of my settings are list collections. I followed the instructions here: https://www.dotnetperls.com/convert-list-string-vbnet and the options for converting it back throws me a message saying I cannot convert array to list collection.

    Any help or criticism would be awesome.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Converting List Collection & String

    This:
    vb.net Code:
    1. My.Settings.M_IPs = M_Ips2.ToList
    should be this:
    vb.net Code:
    1. My.Settings.M_IPs.AddRange(M_Ips2)
    You may want to call Clear on the existing collection first, if it may already contain items.

Tags for this Thread

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