|
-
May 4th, 2013, 06:36 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] String collections
Hi, I am using a string collection to collect user data, but I now need to save this collection.
any ideas?
The code I am using is:
Code:
Public Shared Sub Main()
Dim mycol As New StringCollection()
Dim base() As String = {My.Settings.calc1, My.Settings.calc2, My.Settings.calc3, My.Settings.calc4, My.Settings.calc5, My.Settings.calc6, My.Settings.calc7, My.Settings.calc8}
mycol.AddRange(base)
End Sub
-
May 4th, 2013, 08:09 AM
#2
Re: String collections
Hi
Unless you are streamlining your project, I am slightly confused as to why you would want to save a collection of strings that are already defined and saved within the settings of your project?
Aside from that, have a look at this post I made earlier today which will explain how to save a collection of strings in your project settings which can then be read when you need.
http://www.vbforums.com/showthread.p...as-a-variable.
Hope that helps.
Cheers,
Ian
-
May 4th, 2013, 09:02 AM
#3
Thread Starter
Hyperactive Member
Re: String collections
Cheers Ian, I have actually managed to get the values to save now, just working on displaying specific ones in specific places now. as for the reason to saving a string it is quite simple. The my.settings are only to pass information between elements within the app. as soon as I close the app the my.settings are emptied, hence why I need the string collection so I can "save" the values after the form is closed.
-
May 4th, 2013, 09:06 AM
#4
Thread Starter
Hyperactive Member
Re: String collections
This was my solution for saving the items.
Code:
Public Shared Sub Main()
Dim mycol As New StringCollection()
Dim base() As String = {My.Settings.name, My.Settings.calc1, My.Settings.calc2, My.Settings.calc3, My.Settings.calc4, My.Settings.calc5, My.Settings.calc6, My.Settings.calc7, My.Settings.calc8}
mycol.AddRange(base)
'Initialize the collections
'For each product in the list, add an entry to the Names and Prices settings
For Each p As Object In base
My.Settings.mycol.Add(p.ToString)
Next
My.Settings.Save()
End Sub
now I need to display every 8th item/object in my combo bow so I will need to get that sorted.
so far I have everything in the combo box using this code
Code:
Private Sub Saved_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
splash.Hide()
For Each p As Object In My.Settings.mycol
ComboBox1.Items.Add(p.ToString)
Next
End Sub
-
May 4th, 2013, 09:47 AM
#5
Thread Starter
Hyperactive Member
Re: String collections
Any suggestions here would be awesome I am a little lost in my own code here
-
May 4th, 2013, 10:40 PM
#6
Re: String collections
Hi,
The my.settings are only to pass information between elements within the app. as soon as I close the app the my.settings are emptied, hence why I need the string collection so I can "save" the values after the form is closed.
The first thing to address is your comment above. You seem to have misunderstood the fact that ALL variables that are declared within the My.Settings collection are, by default, AUTOMATICALLY saved by the project when the application closes. Therefore all your calc1, calc2 etc variables were already being saved and you did not need to declared a new sting collection and explicitly call My.Settings.Save to do this.
Now that everything is in a StringCollection you can get every 8th element using a For Loop with an increment Step of 8. Have a look here:-
Code:
For Counter = 7 To My.Settings.myStringValues.Count - 1 Step 8
ComboBox1.Items.Add(My.Settings.myStringValues(Counter))
Next
Hope that helps.
Cheers,
Ian
-
May 5th, 2013, 08:09 AM
#7
Thread Starter
Hyperactive Member
Re: String collections
Awsome, very big thank you for this. It works a treat.
Ok, I have just started receiving a null reference exception while saving. Havent a clue what could be causing this
Ok, so after looking into it a little the null reference exception occurs while adding p to my.settings.mycol. more specifically it happens while adding name (which is a string) to the my.settings.mycol
The above information is actually wrong. I am getting a null reference any time I try to access the my.settings.mycol
My mistake. I had forgotten to initialise it
Last edited by danielpalfrey; May 5th, 2013 at 09:13 AM.
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
|