Specialized Strings Collection, where do they stand?
Introduction
Hey guys, been really bored lately and I was stuck at home for the whole day for a family thing so I was looking for something to do. I was racking my brains for a tutorial I could write and I started a massive 'Settings' tutorial, 15 pages later I abandoned it because it had horrible structure and I just couldn't condense it so everyone would "TL;DR" the shit out of it.
However, I did learn one valuable thing from writing that tut and that was of string collections and their power as a settings saver! No joke these badboys can be used to replace a basic ini document and hence remove the need of an external file. As it's a child of the My.Settings class it's built into VB and saved internally. So yeah, I'll endeavour to explain as much as i can about String Collections today and how YOU can use them to replace your annoying .inis
The Tutorial
Alrighty, first off: how the hell do I make this "Specialized String Collection"? Simple, go to add a setting as you normally would and for the "type" choose "Specialized.StringCollection" and hit ok, you now have a string collection. NOTE: In this tutorial I named my StringCollection setting "StrCollect" orginal right?
Now, it's theory time so you understand the basics of a String Collection and what it's capable of. I'll try and compare a string collection with an external file as much as I can so that you can compare it with something most people are familiar with.
I find it hard to explain the concept of a string collection without writing 30,000 lines trying to explain the details. But basically a string collection contains a collection of string, each separate entry is indexed within the collection with an index number, starting at 0 and continuing for as long as you want. To retrieve a specific entry you just have to tell the collection which index you want to look at. Adding new entries is as simple as an ".add" whacked on the end.
So how can we use this to simulate an XML or an INI? Well, try and image that each index is similar to each separate line of an ini/XML/dat or whatever, and go from there. Here's a brief example of adding 3 separate "lines" to a string collection:
Firstly you need to add this to your Form_Load or you'll get null reference errors aplenty, thank you to the guy from MSDN that solved this problem for me!
Snippet #1
[php]
If My.Settings.StrCollect Is Nothing Then
My.Settings.StrCollect = New System.Collections.Specialized.StringCollection
End If
[/php]
Basically that says that if the String Collection has no contents, define it as a new stringcollection which will substantiate the object and stop those damn null reference errors!
Alrighty, now let's also add 3 items to the collection like so: (Put this in the form_load event AFTER the previous snippet (gotta substantiate the collection before we add to it!))
Snippet #2
[php]
My.Settings.StrCollect.Add("Hello")
My.Settings.StrCollect.Add("Welcome to")
My.Settings.StrCollect.Add("My tutorial")
[/php]
Okay now we have 3 sets of string in our collection, all contained within seperate indexes. Remember, the first index starts at 0, not 1! Now, let's try extracting one of these lines, it's pretty simple. Add this to a button_click event or something similar.
Snippet #3
[php]
MsgBox(My.Settings.StrCollect(1))
[/php]
This will pop-up a messagebox with whatever was contained in index "1", remembering that the indexes start at 0, we should expect this to return the seconds line, "Welcome to" and lo and behold, it does just that! Neat huh? So hopefully now you get the concept of each line having its own index which can be easily accessed. Now we'll move on to adding a bunch of separate lines as the contents of a listbox!
Snippet #4
[php]
For each s As String in ListBox1.Items
My.Settings.StrCollect.Add(s)
Next
My.Settings.Save() 'DON'T FORGET TO SAVE!
My.Settings.Reload()
[/php]
Now we want to add each of these items back again!
Snippet #5
[php]
For i as integer = 0 to My.Settings.StrCollec*****unt
ListBox1.Items.Add(My.Settings.StrCollect(i))
Next
[/php]
What do you know, that added all the items! By doing "For i as integer = 0 to My.Settings.StrCollec*****unt" we’re executing the code contained between “For” and “Next” with an integer “i” that increases by one each time it loops through until it reaches the total number of currently used indices in the StringCollection, cool.
But this method may not work for retrieving settings very effectively, how are you going to know that those 3 lines need to be added to a listbox is you have 100 lines to choose from? Well firstly, let's condense those 3 entries into a single line with a nice separator between each entry. We can condense them all to one line with a simple for-next block, such as this:
Snippet #6
[php]
Dim TotalString As String = “” ‘first we declare a string variable to hold the info until we need it
For each s As String in Listbox1.Items
If TotalString <> “” then
TotalString &= (“,” & s) ‘if this is not the first entry into “TotalString” it will add a separator “,” between the previous entry and the current entry
Else
TotalString &= s
End if
Next
If Not TotalString = “” then ‘if there are items in the listbox
My.Settings.StrCollect.Add(TotalString)
My.Settings.Save()
My.Settings.Reload()
Else: Exit sub
[/php]
Okay with this little bit of code, we add each item to a string variable with a “,” between entries so it looks like this “item1,item2,item3,item4”. Now, you may ask, “why do we add it all to one line, won’t that make it harder to get the values we need!?” The answer is no, with basic string manipulation we can separate each entry, this is why I added the “,” separator between each item. So, this is how we would get each entry back again.
Snippet #7
[php]
For each item As String in My.Settings.StrCollect(0).Split(“,”)
ListBox1.Items.Add(item)
Next
[/php]
Okay I used the index “0” for My.Settings.StrCollect because I only had one index in the collection so I knew it was going to be stored in the first index. Later in the tut I’ll explain looping through all the indexes and getting only what we need (it’s similar to "Snippet #5). By using the “Split” function you can split a line of string into separate items by using a common separator, in this case “,” was common. I then added them all to the textbox.
That might be all fine and dandy, but we still need to make it UNIQUE so we can find it easily in amongst dozens of other lines. This is where wrappers(?) come in handy, I prefer to call them headers and footers just because it makes sense to me but call them what you will. By header I mean the section of string that precedes the content and by footer I meant the section of string that comes after contents ie:
I personally use headers for the unique factor (i.e “<ListBox1></ListBox>”, “<MiscInfo></ListBox>”) In both cases I left the footer as </ListBox> to illustrate the fact that both are listboxes but changed the header to something unique. This an example of me creating a sub procedure that will generate a unique header and save a listboxes content:Code:<HEADER>CONTENTLALALA</FOOTER>
Snippet #8
[php]
Private Sub SaveListBox(ByVal lbox As ListBox)
Dim TotalString As String = ""
For Each item As String In lbox.Items
If TotalString = "" Then
TotalString &= item
Else
TotalString &= ("," & item)
End If
Next
If Not TotalString = "" Then 'if there are entries in the listbox, add them else exit.
Dim header As String = "<" & lbox.Name & ">"
Dim footer As String = "</ListBox>"
Dim output As String = header & TotalString & footer
My.Settings.StrCollect.Add(output)
My.Settings.Save()
My.Settings.Reload()
Else
Exit Sub
End If
End Sub
'EXAMPLE HERE:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SaveListBox(ListBox1)
End Sub
[/php]
As you can see, I make the programmer pass which ListBox they want to save into the parameters of the sub-procedure. Because no control can have the same name in a form generating the header as the ListBox.Name ensures a nice unique header, it also makes it easy to retrieve again. Although we covered the basics of splitting in "Snippet #6", we didn’t factor in the header and footer. In light of this we need to extract the contents as a line, then split that. Here’s my LoadListBox Sub (note the programmer must pass which listbox to load)
Snippet #9
[php]
Private Sub LoadListBox(ByVal lBox As ListBox)
Dim header As String = "<" & lBox.Name & ">"
Dim contents As String = ""
Dim footer As String = "</ListBox>"
Dim footerIndex As Integer
For i As Integer = 0 To My.Settings.StrCollec*****unt
If My.Settings.StrCollect(i).StartsWith(header) Then
footerIndex = My.Settings.StrCollect(i).LastIndexOf(footer)
contents = My.Settings.StrCollect(i).Substring((header.Length ), footerIndex - header.Length)
For Each entry As String In contents.Split(",")
lBox.Items.Add(entry)
Next
End If
Next
End Sub
'EXAMPLE HERE:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
LoadListBox(ListBox1)
End Sub
[/php]
And that’s all there is to it! Now to go more in depth, I first define all the needed sections of string for ease of use as well as an integer for where the index of the footer is. I'm not going to explain that any further because if i start getting into all the little tidbits I'm never going to finish! Next up, we loop through all the indexes contained withing My.Settings.StrCollect looking for one that starts with our unique header. If we find one, we extract the contents between the header and footer and then split that into individual items, then add to the listbox.
Well that about concludes the tutorial for today, I'll leave you with a similar method for adding a textbox to the collection, it's much the same except you don't need to split it because you only have one entry to consider.
Snippet #10
[php]
Private Sub SaveTextBox(ByVal tBox As TextBox)
Dim header As String = "<" & tBox.Name & ">"
Dim footer As String = "</TextBox>"
Dim contents As String = tBox.Text
Dim output As String = header & contents & footer
If contents <> "" Then
My.Settings.StrCollect.Add(output)
My.Settings.Save()
My.Settings.Reload()
Else
Exit Sub
End If
End Sub
'EXAMPLE HERE:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SaveTextBox(TextBox1)
End Sub
[/php]
Snippet #10
[php]
Private Sub LoadTextBox(ByVal tBox As TextBox)
Dim header As String = "<" & tBox.Name & ">"
Dim footer As String = "</TextBox>"
Dim contents As String = ""
Dim footerIndex As Integer
For i As Integer = 0 To My.Settings.StrCollec*****unt
If My.Settings.StrCollect(i).StartsWith(header) Then
footerIndex = My.Settings.StrCollect(i).LastIndexOf(footer)
contents = My.Settings.StrCollect(i).Substring(header.Length, footerIndex - header.Length)
End If
Next
If contents <> "" Then
tBox.Text = contents
End If
End Sub
'EXAMPLE HERE
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
LoadTextBox(TextBox1)
End Sub
[/php]
That concludes my contribution for today, I hope someone learns something from this. If you have any questions please feel free to ask and I'll try explain to the best of my ability.
Cheers
J-Deezy
P.S this tutorial is reserved for the sole use of MPGH and it's users, if i see it leaked on another site (which is doubtful but whatever) I will be mighty pissed off.
Lat0rz
You can win the rat race,Originally Posted by Jeremy S. Anderson
But you're still nothing but a fucking RAT.
++Latest Projects++
[Open Source] Injection Library
Simple PE Cipher
FilthyHooker - Simple Hooking Class
CLR Injector - Inject .NET dlls with ease
Simple Injection - An in-depth look
MPGH's .NET SDK
eJect - Simple Injector
Basic PE Explorer (BETA)
Make a .pdf out of it.
You can win the rat race,Originally Posted by Jeremy S. Anderson
But you're still nothing but a fucking RAT.
++Latest Projects++
[Open Source] Injection Library
Simple PE Cipher
FilthyHooker - Simple Hooking Class
CLR Injector - Inject .NET dlls with ease
Simple Injection - An in-depth look
MPGH's .NET SDK
eJect - Simple Injector
Basic PE Explorer (BETA)
Good Job . .
You can win the rat race,Originally Posted by Jeremy S. Anderson
But you're still nothing but a fucking RAT.
++Latest Projects++
[Open Source] Injection Library
Simple PE Cipher
FilthyHooker - Simple Hooking Class
CLR Injector - Inject .NET dlls with ease
Simple Injection - An in-depth look
MPGH's .NET SDK
eJect - Simple Injector
Basic PE Explorer (BETA)
Cool, this is really helpful! Does this mean I don't need to use ini in my injector!? I am a little bit noob I dont fully understnad
You can win the rat race,Originally Posted by Jeremy S. Anderson
But you're still nothing but a fucking RAT.
++Latest Projects++
[Open Source] Injection Library
Simple PE Cipher
FilthyHooker - Simple Hooking Class
CLR Injector - Inject .NET dlls with ease
Simple Injection - An in-depth look
MPGH's .NET SDK
eJect - Simple Injector
Basic PE Explorer (BETA)
Omg, nice work man !
Heal Ur Allies, And You Will Win The War.
RESPECT LIST :
- Very Good Friend - Sn0wn00b
- All the Mpgh minions ( special m3 )
- th3reaper
- respeckt52
- s3liskar
- thecamels8
You can win the rat race,Originally Posted by Jeremy S. Anderson
But you're still nothing but a fucking RAT.
++Latest Projects++
[Open Source] Injection Library
Simple PE Cipher
FilthyHooker - Simple Hooking Class
CLR Injector - Inject .NET dlls with ease
Simple Injection - An in-depth look
MPGH's .NET SDK
eJect - Simple Injector
Basic PE Explorer (BETA)
Heal Ur Allies, And You Will Win The War.
RESPECT LIST :
- Very Good Friend - Sn0wn00b
- All the Mpgh minions ( special m3 )
- th3reaper
- respeckt52
- s3liskar
- thecamels8
Added to tuts.
You can win the rat race,Originally Posted by Jeremy S. Anderson
But you're still nothing but a fucking RAT.
++Latest Projects++
[Open Source] Injection Library
Simple PE Cipher
FilthyHooker - Simple Hooking Class
CLR Injector - Inject .NET dlls with ease
Simple Injection - An in-depth look
MPGH's .NET SDK
eJect - Simple Injector
Basic PE Explorer (BETA)
Very good job
Now that I am back, I am thinking to create some string formatting tutorials =P
HASSAN ISS BACK!!!
WELCOME BACK!!
Man i been waiting for you to get back so you can give me a hand
J-deezy, you're a fucking legend..
Excuse my language ↑↑
You can win the rat race,Originally Posted by Jeremy S. Anderson
But you're still nothing but a fucking RAT.
++Latest Projects++
[Open Source] Injection Library
Simple PE Cipher
FilthyHooker - Simple Hooking Class
CLR Injector - Inject .NET dlls with ease
Simple Injection - An in-depth look
MPGH's .NET SDK
eJect - Simple Injector
Basic PE Explorer (BETA)