Sunday, December 6, 2009
Wordpress
http://vbguru.wordpress.com
Friday, December 4, 2009
Inheritance, and why it rocks!
Here we create our 'base' class. We declare it as MustInhert so that we can use the MustOverride property on the ProcessCard Function
You'll notice that we created a function signature, but it has no code attached. We add the code later when we create the gateway's class.
When your create your class that inherits your base class you'll notice when you add the line 'Inherits CreditCard' that it will autofill the MustOverride Function. Visual Studio makes it easy for you!
Your other team members will love you if you now use a 'wrapper' to manage the gateway, check this code out:
Now your UI guy just has to write something like this and he never has to worry about which credit card processing gateway you are using.
Thursday, December 3, 2009
Using a One-Way Hash on Passwords
Public Class Hash
Private Function ComputeHashValue(ByVal data() As Byte) As Byte()
Dim hashAlg As System.Security.Cryptography.SHA1 = System.Security.Cryptography.SHA1.Create()
Dim hashvalue() As Byte = hashAlg.ComputeHash(data)
Return hashvalue
End Function
Public Function HashPassword(ByVal password As String) As String
Dim encoder As System.Text.UTF8Encoding
Dim b() As Byte
encoder = New System.Text.UTF8Encoding
b = encoder.GetBytes(password)
b = ComputeHashValue(b)
Return Convert.ToBase64String(b)
End Function
End Class
Wednesday, December 2, 2009
Being Good With SQL, Using SQL Cursors
Today I wanted to talk for a moment about using SQL cursors, because they are very cool. Have you ever wanted to do a loop inside a Stored procedure? Well check out this code, you this will loop through all users that do not have a locker assigned (using a cool "NOT IN" SQL statement) and assign each of them the next available locker.
Tuesday, December 1, 2009
Threading
#1) Threading
#2) Registry Access
Back in my VB6 days my team tried many times to get things to work muti-threaded, there were magazine articles at the time that claimed to be able to do it using windows API hacks, and MDI forms, but we were never able to get it to work. In .NET you just have to call .Start on a thread object is just Amazing.
Registry access in VB6 was crazy. You had to write pages and pages of code using a bunch of Win32 API Calls. In .NET I changed my pages of code into just a few lines. Once I wrote my first .NET Registry access routine I never wanted to write in VB6 again.
Here is some code that I modified that does both Registry Access and threading. I'm not sure who wrote this code originally, but I modified it so it would be thread safe. Your form needs to have the following controls:
A timer named Timer1, set to go off every 5 seconds (5000 ms)
A datagrid control named DataGridView1
Two buttons Button1 and Button2
This code does have a bug that I didn't take the time to figure out, after it searches 200,000 or so keys it starts crashing. I'm not sure why, my guess is it is trying to access some System Protected Key, but this code works well enough that I am not going to worry about that.
Now that you've seen that I would like to discuss some of the concepts that you need to be aware of in Threading.
Mainly "Thread Safety" and "Thread Management".
"Thread Safety"
Most people new to threading do not think much about this, you spawn some thread and want to post it's results back to the UI. But you will find when you code tries to access a datagrid, or listbox that is on the UI thread that it will throw an error as a background thread will not be allowed to mess with the UI thread. There are a number of solutions to this, I generally use the Singleton pattern to create a buffer to hold data in. I'll talk more about Singletons in later posts. For this example I decided to use the timer control to pause the background thread, and update the UI with it's current results.
"Thread Management"
This concept is important as well. If you do not stop your other threads when your application is shut down they will keep running forever. You also need to be concerned about how many threads you create. The first time I wrote a threading application I wrote a program which searched all files and folders on a hard drive. The main thread searched for new folders, and it spawned another thread to search the files. As it ran it just kept spawning thread after thread until it hit around 32,000 threads at which time my computer, without giving an error message, turned itself off. In this example we only create one thread so you need not worry about it crashing you computer.
I generally create my own threading class which spawns a set number of threads (usually in it's constructor) and manages them. It keeps a Queue of new things to spawn threads for, and then checks the status of each thread, once the thread is done with it's task the threading class records the results, disposes the thread and creates a new thread with the next task in it's Queue. I believe this is called "Thread Polling" but I don't know I created this method on my own without reading about it.
i += 1
Monday, November 30, 2009
Create a variable without Dim
Do you ever Dim i as Int and use it over and over again, just to realize that you forgot to set i = 0 before one of your loops? Well fear no more, you can create a variable named i without diming it and it only lives inside a narrow scope. Check this out:
For i As Integer = 0 To 10
Next
You can even Dim a variable inside a IF Block (or any other block) and it is only available in the scope of that block.
If True = True Then
Dim i As Integer = 0
i = i + 1
End If
If True = True Then
Dim i As Integer = 0
i = i + 1
End If