Friday, December 3, 2010

Friday 12/3/2010

Here is a tutorial by Evangelos Peroutsos


Public Class Form1

'FYI this tutorial was written by Evangelos Petroutsos, the notations in it are mostly made by myself, but are quoted from his book.
'Anyone interested should take a look at his book "Mastering Microsoft Visual Basic 2008" it is pretty good

Dim clearDisplay As Boolean
Dim Operand1 As Double
Dim Operand2 As Double
Dim theOperator As String


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

End Sub


Private Sub DigitClick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnZero.Click, btnOne.Click, btnTwo.Click, btnThree.Click, btnFour.Click, btnFive.Click, btnSix.Click, btnSeven.Click, btnEight.Click, btnNine.Click

If clearDisplay = True Then
lblDisplay.Text = ""
clearDisplay = False
End If
' take a look at the expression sender.text
lblDisplay.Text = lblDisplay.Text + sender.text
End Sub

Private Sub bttnClear_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnClear.Click
lblDisplay.Text = ""
End Sub



Private Sub btnPeriod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPeriod.Click
'indexOf is a method that can be applied to any string...if this number is zero or positive, the number enetered contains a period already, and another can't be entered
If lblDisplay.Text.IndexOf(".") >= 0 Then
Exit Sub
Else
lblDisplay.Text = lblDisplay.Text & "."
End If
End Sub

'coding the math operations - the math operations will involve two operands and an operator (+,-,/,*, etc)
'As Petroutsos states, when the user clicks one of the math symbols, the value on the display is then set to be operand1, the math symbol pressed becomes the operator, then
'whatever numbers are entered next become the second number in the operation, aka operand2

'Petroutsos wants us to remember as well here that the variables are local in the subroutines in which they are declared.

'The plus button
Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
Operand1 = Convert.ToDouble(lblDisplay.Text)
theOperator = "+"
clearDisplay = True
End Sub

Private Sub btnMultiply_Click(ByVal send As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
Operand1 = Convert.ToDouble(lblDisplay.Text)
theOperator = "*"
clearDisplay = True
End Sub

'The equals button
Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
Dim result As Double
Operand2 = Convert.ToDouble(lblDisplay.Text)
Select Case theOperator
Case "+"
result = Operand1 + Operand2
Case "-"
result = Operand1 - Operand2
Case "*"
result = Operand1 * Operand2
Case "/"
result = Operand1 / Operand2
End Select
lblDisplay.Text = result.ToString
clearDisplay = True
End Sub

Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
Operand1 = Convert.ToDouble(lblDisplay.Text)
theOperator = "*"
clearDisplay = True
End Sub
End Class


Next, I am going to do my first WPF tutorial. Honestly, the more I learn about vb.net, the more I realize I don't know.


What is XAML? I am going to look on wikipedia, as this is the most obscure and incomprehensible source that I can think of off the top of my head.

Nevermind, I am going to finish the first Jesse Liberty book instead


Here is one of his tutorials on passing by value between classes etc.


Option Strict On
Imports System
Public Class Cat
Private mWeight As Integer

Public Sub New(ByVal weight As Integer)
mWeight = weight
End Sub

Public Property Weight() As Integer
Get
Return mWeight
End Get
Set(ByVal value As Integer)
mWeight = value
End Set
End Property

Public Overrides Function ToString() As String
Return mWeight.ToString()
End Function



End Class


Public Class tester
Public Sub run()
'declare a cat and initialize to 5
Dim theVariable As New Cat(5)

'display its value
Console.WriteLine("In run. theVariable: {0}", theVariable)
'call a method & pass the variable
Doubler(theVariable)


End Sub

Public Sub doubler(ByVal param As Cat)
'display the value that was passed in
Console.writeLine("In method1. received param: {0}", param)

'double the value
param.Weight = param.Weight * 2

'display the doubled value before returning
Console.WriteLine("Updated param. returning new value: {0}", param)
End Sub


End Class ' tester

Module Module1
Sub Main()
Dim t As New tester()
t.run()
Console.ReadLine()
End Sub
End Module


Here is a tutorial on FileStreams


Imports System.IO
Imports System.Text

Public Class Tester
Public Sub Run()
Dim mySourceFileStream As FileStream
Dim myDestFileStream As FileStream
Dim bteRead() As Byte
Dim intByte As Integer
Dim bteMessage(128) As Byte
Dim strCopyMessage As String = ""

Try
myDestFileStream = New FileStream("C:\Users\Alfred\Documents\dest.txt", FileMode.OpenOrCreate, FileAccess.Write)

bteMessage = Encoding.ASCII.GetBytes(strCopyMessage)
myDestFileStream.Write(bteMessage, 0, bteMessage.Length)

mySourceFileStream = New FileStream("C:\Users\Alfred\Documents\texttime.text", FileMode.OpenOrCreate, FileAccess.Read)
intByte = mySourceFileStream.Length
ReDim bteRead(intByte)
mySourceFileStream.Read(bteRead, 0, intByte)
myDestFileStream.Write(bteRead, 0, intByte)

myDestFileStream.Close()
mySourceFileStream.Close()

Catch ex As Exception
Console.WriteLine("oh oh")

End Try


End Sub
End Class

Module module1
Sub main()
Dim testerTime As New Tester

testerTime.Run()

End Sub
End Module



Here is a little something I wrote myself on arrays and classes.


Public Class stock

Dim ticker As String
Dim stockName As String
Dim purchaseValue As Double
Dim todaysValue As Double

Public Sub New(ByVal subTicker As String, ByVal subStockName As String, ByVal subPurchaseValue As Double, ByVal subTodaysValue As Double)
ticker = subTicker
stockName = subStockName
purchaseValue = subPurchaseValue
todaysValue = subTodaysValue
End Sub

Public Sub verify()
Console.WriteLine("The information for stock " & ticker & " has been entered")

End Sub

End Class

Module module1
Sub main()
Dim stock1 As New stock("BMS", "Bank of Montreal", 24.37, 32.75)
stock1.verify()

Console.ReadLine()

Dim stockArray(9) As stock

stockArray(0) = New stock("FTE", "France Telecom", 25.21, 21.35)

stockArray(0).verify()

Console.ReadLine()

End Sub
End Module

No comments: