Wednesday, December 15, 2010

Wednesday 12/15/10

I have started using a book written by Tim Patrick. Here is a tutorial by him. A list of his books are available at http://www.timaki.com/books.htm



Public Class Form1
Private WithEvents helloDecoder As HelloStuff.SayHello
' What does withEvents mean?
' "The withEvents keyword included in the statement is what lets the control know that someone wants to monitor event notifications.

Private Sub button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles button1.Click
'show the message
helloDecoder = New HelloStuff.SayHello("!iqwtB ,tqqjM", True)
helloDecoder.DecodeMessage(-5)
helloDecoder.ReportMessage()
End Sub

Private Sub HelloDecoder_MessageDecoded(ByVal decodedMessage As String) _
Handles helloDecoder.MessageDecoded
'show the decoded message
MsgBox(decodedMessage)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

End Sub


End Class

Namespace HelloStuff
Friend Class SayHello
Private secretMessage As String
Private reverseFlag As Boolean
Private decoded As Boolean

Public Event MessageDecoded(ByVal decodedMessage As String)

'The New constructor allows the user to create a new instance of SayHello
'with an initial message text, and a flag which tells
' the program whether the text should be reversed before display.


Public Sub New(ByVal codedMessage As String, ByVal reverseIt As Boolean)
'save the secret message. It will be decoded later.
secretMessage = codedMessage
reverseFlag = reverseIt
decoded = False
End Sub

'The decodeMessage method converts each letter of the
'encoded message by shifting each letter a number of spaces
'determined by the rotationFactor


Public Sub DecodeMessage(ByVal rotationFactor As Integer)
'The original message was coded. Rotate each character through the alphabet by the specified number
'of times to get the true value
Dim result As String
Dim oneChar As Char
Dim counter As Integer
Dim charValue As Integer

'Don't decode twice
If (decoded = True) Then Return

'Process each character
result = ""
For counter = 1 To Len(secretMessage)
oneChar = CChar(Mid(secretMessage, counter, 1))
If (Char.IsLower(oneChar) = True) Then
'rotate the character. convert it to an integer,
'rotate it, then convert it back
charValue = Asc(oneChar) - Asc("a"c)
charValue = (charValue + rotationFactor + 26) Mod 26
oneChar = Chr(charValue + Asc("a"c))
result &= oneChar
ElseIf (Char.IsUpper(oneChar) = True) Then
'rotate the character. convert it to an integer, rotate it, then convert it back.
charValue = Asc(oneChar) - Asc("A"c)
charValue = (charValue + rotationFactor + 26) Mod 26
oneChar = Chr(charValue + Asc("A"c))
result &= oneChar
Else
'no special translation on this character.
result &= oneChar
End If
Next counter

'store the converted value
secretMessage = result
decoded = True
End Sub

Public Sub ReportMessage()
'fire event for the caller
If (reverseFlag = True) Then
RaiseEvent MessageDecoded(StrReverse(secretMessage))
Else
RaiseEvent MessageDecoded(secretMessage)
End If
End Sub


End Class
End Namespace

No comments: