Archive for January, 2011

ToggleNic- toggling the enabled network connection in win7

I found this site http://channel9.msdn.com/playground/Sandbox/154712/#Page=1 which ironically had a project under the same title I was using.

I wrote a togglenic app for xp some time ago but it was pretty limited in the features. This week i got a new win7 pc and found my togglenic no longer worked. I found the listed site, got the code, and went to work.

Its now a windows forms application, it will start with windows if you set it (minimized of course), will provide a list of networks, you can refresh this list, choose the network, outputs log to richtextbox, minimized to tray. Warning- i threw this together so its provided with no warranty. Sorry.

Pretty basic. I plan on adding xp support, but haven’t done so yet. So to be clear this should work on vista,7,and 2008 though I’ve only tested in win7.

Also to be clear i have no affiliation with the author of the listed post, we’ve just happened to name it the same thing. I did steal his icon…cuz it was nice.

Im not releasing the source since the working code has already been released. Any ideas for improvements are welcome.

rar – http://www.mediafire.com/?jaj9a1lgw34bv66

Update on char counter (vb.net)

Well my previous post spoke about a char counter. After posting it on a different blog several months ago I followed it with the below post.

Today I went to put it into a program and quickly realized some short comings. Hopefully this fixes them.

sFor previously could only be one character long. Well what if you want to search for an entire string. You need to divide the length by the length of the searching string. Also, If you don’t care about case since the sIn could be mixed case we need to lower that and then lower the sFor, instead of just lowering and upping the sFor. This in theory will slow it down since the string could be longer than lowering the sFor and sIn, but I cant think of a way around that.

Function cnt_occ(ByRef sIn As String, ByRef sFor As String, Optional ByVal casesensitive As Boolean = False) As Integer
If casesensitive Then
Return (sIn.Length – sIn.Replace(sFor, “”).Length) / sFor.Length
Else
Return (sIn.Length – sIn.ToLower.Replace(sFor.ToLower, “”).Length) / sFor.Length
End If
End Function

Char Count Speed Test (vb.net)

A few months ago i posted the below post on a different blog. Since this is my dedicated programming blog, I thought I’d bring it over here. I commonly need to return the number of a specific character in a string, and was never really happy with what i found on the internet. Here are some concepts of different ways to do it.

Function cnt_occ1(ByRef sIn As String, ByRef sFor As String, Optional ByVal casesensitive As Boolean = False) As Integer
Dim startTime As DateTime = DateTime.Now
Dim ts As String = “”
If casesensitive Then
ts = sIn.Replace(sFor, “”)
Else
ts = sIn.Replace(sFor.ToUpper, “”).Replace(sFor.ToLower, “”)
End If
Dim executionTime As TimeSpan = DateTime.Now – startTime
MsgBox(executionTime.Seconds.ToString() & “:” & executionTime.Milliseconds.ToString())
Return sIn.Length – ts.Length
End Function

Function cnt_occ2(ByRef OrigString As String, ByVal Chars As String, Optional ByVal CaseSensitive As Boolean = False) As Integer
Dim startTime As DateTime = DateTime.Now
http://www.freevbcode.com/ShowCode.Asp?ID=1025
Dim lLen As Long
Dim lCharLen As Long
Dim lAns As Long
Dim sInput As String
Dim sChar As String
Dim lCtr As Long
Dim lEndOfLoop As Long
Dim bytCompareType As Byte

sInput = OrigString
If sInput = “” Then Return 0
lLen = Len(sInput)
lCharLen = Len(Chars)
lEndOfLoop = (lLen – lCharLen) + 1
bytCompareType = IIf(CaseSensitive, vbBinaryCompare, _
vbTextCompare)

For lCtr = 1 To lEndOfLoop
sChar = Mid(sInput, lCtr, lCharLen)
If StrComp(sChar, Chars, bytCompareType) = 0 Then lAns = lAns + 1
Next
Dim executionTime As TimeSpan = DateTime.Now – startTime
MsgBox(executionTime.Seconds.ToString() & “:” & executionTime.Milliseconds.ToString())
Return lAns
End Function

Function cnt_occ3(ByRef sIn As String, ByRef sFor As String, Optional ByVal casesensitive As Boolean = False) As Integer
Dim startTime As DateTime = DateTime.Now
Dim oc As Integer
Dim pos As Integer
Dim ts As String = “”
Dim lp As Integer
If casesensitive = False Then
ts = sIn.ToLower
sFor = sFor.ToLower
lp = ts.LastIndexOf(sFor)
Else
lp = sIn.LastIndexOf(sFor)
ts = sIn
End If

While 1 = 1
oc += 1
pos = ts.IndexOf(sFor, pos) + 1
If pos >= lp Then
Exit While
End If
End While
Dim executionTime As TimeSpan = DateTime.Now – startTime
MsgBox(executionTime.Seconds.ToString() & “:” & executionTime.Milliseconds.ToString())
Return oc
End Function

Function cnt_occ4(ByRef sIn As String, ByRef sFor As String, Optional ByVal casesensitive As Boolean = False) As Integer
Dim startTime As DateTime = DateTime.Now
Dim oc As Integer

Dim ts As String = “”
If casesensitive = False Then
ts = sIn.ToLower
sFor = sFor.ToLower
Else
ts = sIn
End If
While 1 = 1
oc += 1
ts = ts.Substring(ts.IndexOf(sFor) + 1)
If ts.IndexOf(sFor) = -1 Then
Exit While
End If
End While
Dim executionTime As TimeSpan = DateTime.Now – startTime
MsgBox(executionTime.Seconds.ToString() & “:” & executionTime.Milliseconds.ToString())
Return oc
End Function

I thought that to count the occurrences of a character in a string I could replace it with nothing, then diff the lengths. Indeed it is faster. Surprisingly the 2nd fastest is looping through each character. Next we have the index of the string saved and looking for the next index of. Lastly we have the string cut for each occurrence.

Here’s the results:
Small number of occurrences-
1-0.46
2-0.93
3-0.812
4-5.640

Large number of occurrences-
1-0.31
2-0.93
3-4.843
4-34.508

key:
seconds.milliseconds

conclusion:
My code is not only always faster but it’s also much cleaner and readable. I’m sure someone else has done this but I hadn’t read about it before.

The code can then be reduced to:

Function cnt_occ(ByRef sIn As String, ByRef sFor As String, Optional ByVal casesensitive As Boolean = False) As Integer
If casesensitive Then
Return sIn.Length – sIn.Replace(sFor, “”).Length
Else
Return sIn.Length – sIn.Replace(sFor.ToUpper, “”).Replace(sFor.ToLower, “”).Length
End If
End Function