Monday, December 13, 2010

Monday December 13th 2010

The following code is from a tutorial by Evangelo Petroutsos, but I didn't seem to get it working all that well.



Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim srchWord As String = TextBox1.Text.Trim
If srchWord.Length = 0 Then Exit Sub
Dim wordIndex As Integer
wordIndex = ListBox1.FindStringExact(srchWord)
If wordIndex >= 0 Then
ListBox1.TopIndex = wordIndex
ListBox1.SelectedIndex = wordIndex
Else
wordIndex = ListBox1.FindString(srchWord)
If wordIndex >= 0 Then
ListBox1.TopIndex = wordIndex
ListBox1.SelectedIndex = wordIndex
Else

End If
End If
End Sub
End Class


Here is another tutorial by Jesse Liberty


'Object is the base class for all other classes.

Dim myIntegerVariable As Integer = 123

Sub Main()
'Boxing
Dim myObjectVariable As Object = myIntegerVariable
Console.WriteLine("myObjectVariable: {0} ", myObjectVariable.ToString)

'unboxing
Dim anotherIntegerVariable As Integer = _
DirectCast(myObjectVariable, Integer)
Console.WriteLine("anotherIntegerVariable {0}", anotherIntegerVariable)

Console.ReadLine()

End Sub


Here is the next Jesse Liberty Tutorial, from "Learning Visual Basic .NET"



'from a tutorial by Jesse Liberty. Some modifications by myself.
Imports System
'This set of notes are from Srinivasa Sivkumar:
'Namespaces are the basic builidng block for the .net framework
'System is the basic namespace of every .net code
Namespace StructureDemonstration

'declare a structure named Location
Public Structure Location
'the structure has private data
Private myXVal As Integer
Private myYVal As Integer

'constructor

Public Sub New( _
ByVal xCoordinate As Integer, ByVal yCoordinate As Integer)
myXVal = xCoordinate
myYVal = yCoordinate
End Sub 'new

'Property
Public Property XVal() As Integer
Get
Return myXVal
End Get
Set(ByVal Value As Integer)
myXVal = Value
End Set
End Property

Public Property YVal() As Integer
Get
Return myYVal
End Get
Set(ByVal value As Integer)
myYVal = value
End Set
End Property

'Display the structure as a string
Public Overrides Function ToString() As String
Return [String].Format("{0}, {1}", XVal, YVal)
End Function 'toString
End Structure 'Location

Class tester
Public Sub Run()
'create an instance of the structure
Dim locl As New Location(200, 300)

'display the values in the structure
Console.WriteLine("Locl location: {0}", locl)

'invoke the default constructor
Dim loc2 As New Location()
Console.WriteLine("Loc2 location: {0}", loc2)

'pass the structure to a method
myFunc(locl)

'redisplay the value in the structure
Console.WriteLine("Locl location:{0} ", locl)
End Sub 'run

'method takes a structure as a parameter
Public Sub myFunc(ByVal loc As Location)
'modift the values through the properties
loc.XVal = 50
loc.YVal = 100
Console.WriteLine("Locl location: {0}", loc)

End Sub 'myFunc

End Class
End Namespace

Namespace circleDemonstration
'this is my own variation note that I got circumference and diameter confused
Public Structure cCircle
Private rRadius As Integer
Private cColor As String
Private dDiameter As Double

Public Sub New(ByVal inputRadius As Integer, ByVal inputColor As String)
rRadius = inputRadius
cColor = inputColor
End Sub

Public Sub getCircumference()
dDiameter = (rRadius ^ 2) * 3.14
End Sub

Public Overrides Function toString() As String
Return [String].Format("The circumference is {0} ", dDiameter)
End Function

Public Property myRadius() As Integer
Get
Return rRadius
End Get
Set(ByVal value As Integer)
rRadius = value
End Set
End Property

End Structure

Class newCircle

Public Sub run()
Dim circ As New cCircle
Console.WriteLine("Please enter the radius of the circle")
circ.myRadius = Console.ReadLine()
circ.getCircumference()
Console.WriteLine(circ.toString())

End Sub

End Class

End Namespace
Module module1
Sub main()
Dim t As New StructureDemonstration.tester

t.Run()
Console.ReadLine()

Dim c As New circleDemonstration.newCircle

c.run()
Console.ReadLine()

End Sub
End Module


Here is a tutorial by Michael Halverson on drawing



Public Class Form1

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

End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
'prepare GraphicsFun Variable for graphics calls
Dim GraphicsFun As System.Drawing.Graphics
GraphicsFun = Me.CreateGraphics

'use a red pen color to draw a line and an ellipse
Dim PenColor As New System.Drawing.Pen(System.Drawing.Color.Red)
GraphicsFun.DrawLine(PenColor, 20, 30, 100, 80)
GraphicsFun.DrawEllipse(PenColor, 10, 120, 200, 160)

'use a green brush color to create a filled rectangle
Dim BrushColor As New SolidBrush(Color.Green)
GraphicsFun.FillRectangle(BrushColor, 150, 10, 250, 100)

'Create a blue cardinal spline curve with four points
Dim Points() As Point = {New Point(358, 280), _
New Point(300, 320), New Point(275, 155), New Point(350, 180)}
For tension As Single = 0 To 2.5 Step 0.5
GraphicsFun.DrawCurve(Pens.DodgerBlue, Points, tension)
Next
End Sub
End Class

Friday, December 10, 2010

Friday December 10 2010


Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

If e.KeyCode = Keys.Enter And Not (e.Alt Or e.Control) Then
If Me.ActiveControl.GetType Is GetType(TextBox) Or _
Me.ActiveControl.GetType Is GetType(CheckBox) Or _
Me.ActiveControl.GetType Is GetType(DateTimePicker) Then
If e.Shift Then
Me.ProcessTabKey(False)
Else
Me.ProcessTabKey(True)
End If
End If
End If

End Sub


Here is a program I made myself


Dim masterArray() As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fileGrab As New Connection(OpenFileDialog1)
fileGrab.Connect()
fileGrab.buildArray()

masterArray = fileGrab.returnStringArray

Dim sString As String
For Each sString In masterArray
ListBox1.Items.Add(sString)
Next

Dim indexIn As Integer = masterArray.Length - 1

MessageBox.Show(indexIn)

End Sub

Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

End Sub


Public Class Connection
Dim stringFileName As String = ""
Dim holdingString As String = ""
Dim stringArray() As String

Function returnStringArray()

Return stringArray
End Function

Sub buildArray()
Dim index As Integer = 0
Dim holdingArray() = holdingString.Split(vbCrLf)
stringArray = holdingArray

End Sub

Sub Connect()
MessageBox.Show(stringFileName)
Dim fileReader As New System.IO.StreamReader(stringFileName)
holdingString = fileReader.ReadToEnd
fileReader.Close()
End Sub



Sub New(ByVal OpenFileDialog1)

OpenFileDialog1.Title = "Select a text file"
OpenFileDialog1.Filter = "Text files (*.txt) | *.txt"
OpenFileDialog1.InitialDirectory = "C:\Users\Alfred\Documents\VBPractice"
OpenFileDialog1.ShowDialog()
MessageBox.Show(OpenFileDialog1.FileName)
stringFileName = OpenFileDialog1.FileName

End Sub
End Class

Thursday, December 9, 2010

Thursday, Decemeber 12 2010

Here is the code from a tutorial by Evangelos Petroutsos that I have been working on:




Public Class frmTextPad

Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
If TextBox1.SelectionLength > 0 Then
Clipboard.SetText(TextBox1.SelectedText)
End If
End Sub

Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
Clipboard.SetText(TextBox1.SelectedText)
TextBox1.SelectedText = ""
End Sub

Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
If Clipboard.ContainsText Then
TextBox1.SelectedText = Clipboard.GetText
End If
End Sub

Private Sub NumberLinesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumberLinesToolStripMenuItem.Click
Dim iLine As Integer
'notice the use of StringBuilder
Dim newText As New System.Text.StringBuilder()
For iLine = 0 To TextBox1.Lines.Length - 1
'adds a number before each line, then tabs and then the text for that line
newText.Append((iLine + 1).ToString & vbTab & TextBox1.Lines(iLine) & vbCrLf)
Next
TextBox1.SelectAll()
Clipboard.SetText(newText.ToString)
TextBox1.Paste()
End Sub

Private Sub FindReplaceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindReplaceToolStripMenuItem.Click
findReplace.ShowDialog()
End Sub

Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click
If UndoToolStripMenuItem.Text = "Undo" Then
If TextBox1.CanUndo Then
TextBox1.Undo()
UndoToolStripMenuItem.Text = "Redo"
End If
Else
If TextBox1.CanUndo Then
TextBox1.Undo()
EditToolStripMenuItem.Text = "Undo"
End If
End If
End Sub
End Class


Here is another tutorial by Jesse Liberty



Imports System
Public Class window
'constructor takes two integers to fix location on the console
Public Sub New(ByVal top As Integer, ByVal left As Integer)
Me.top = top
Me.left = left
End Sub

'simulates drawing the window
Public Overridable Sub drawWindow()
Console.WriteLine("Window: drawing window at {0}, {1}", top, Left)
End Sub

'these members are protected and thus invisible to derived class methods
'we'll examine this later in the chapter

Protected top As Integer
Protected left As Integer
End Class


'ListBox derives from Window
Public Class listBox
Inherits window

'the keyword MyBase indentifies the base class for the current object
'constructor adds a parameter
Public Sub New(ByVal top As Integer, ByVal left As Integer, ByVal contents As String)
MyBase.New(top, left) 'call base constructor
listBoxContents = contents
End Sub

'an overrident version(note keyword) because in the derived method we change the behavior
Public Overrides Sub drawWindow()
MyBase.drawWindow() 'nvokes the base method
Console.WriteLine( _
"Writing string to the listbox: {0} ", listBoxContents)
End Sub 'DrawWindow

Private listBoxContents As String 'new member variable

End Class 'listbox

Public Class Button
Inherits window

Public Sub New(ByVal top As Integer, ByVal left As Integer)
MyBase.New(top, left)
End Sub 'new

'an overridden version (again, note keyword) because in the derived method we change the behavior

Public Overrides Sub drawWindow()
Console.WriteLine("Drawing a button at {0}, {1}" & ControlChars.Lf, top, left)
End Sub 'drawWindow

End Class 'button

Module Module1
'The following tutorial is by Jesse Liberty.
'It deal with polymorphism and overriding
'It's important to note that VB.net differs from Java or C++ in that an "overridable function is always considered to the be the root of dispatch"
'This tutorial also looks at object arrays

Sub Main()
Dim win As New window(1, 2)
Dim lb As New listBox(3, 4, "Stand Alone list box")
Dim b As New Button(5, 6)
win.drawWindow()
lb.drawWindow()
b.drawWindow()
Dim winArray(3) As window
winArray(0) = New window(1, 2)
winArray(1) = New listBox(3, 4, "List box in array")
winArray(2) = New Button(5, 6)

Dim i As Integer
For i = 0 To 2
winArray(i).drawWindow()
Next i

Console.ReadLine()
End Sub 'Main

End Module


Here is a little mini form builder class I have been working on





Public Class formBuilder

Dim formNew As Form

Dim buttonButton(10) As Button
Dim buttonIndex As Integer = 0
Dim buttonArray() As Button


Sub New()
formNew = New Form
MessageBox.Show("We created a new form")
End Sub

Sub showForm()
formNew.Show()
End Sub

Sub addButton(ByVal x, ByVal y)

If buttonIndex <= 10 Then
buttonButton(buttonIndex) = New Button
buttonButton(buttonIndex).Top = y
10: buttonButton(buttonIndex).Left = x
buttonButton(buttonIndex).Name = "NewButton" & buttonIndex
MessageBox.Show("We created a new button named " & buttonIndex)
buttonButton(buttonIndex).Text = "Button" & buttonIndex

formNew.Controls.Add(buttonButton(buttonIndex))
buttonIndex = buttonIndex + 1
Else
MessageBox.Show("You created enough buttons already")
Exit Sub
End If
End Sub

Sub bringForward()
formNew.TopMost = True
End Sub
End Class

Public Class Form1
Dim formRoll As formBuilder
Dim x As Integer = 0
Dim y As Integer = 0


Private Sub btnCreateForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateForm.Click
formRoll = New formBuilder
btnShowForm.Enabled = True
btnBringFormForward.Enabled = True
btnAddButton.Enabled = True
btnCreateForm.Enabled = False
End Sub

Private Sub btnShowForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowForm.Click
formRoll.showForm()
End Sub

Private Sub btnAddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddButton.Click

y = InputBox("Enter the top coordinate")
x = InputBox("enter the left coordinate")
formRoll.addButton(x, y)

End Sub

Private Sub btnBringFormForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBringFormForward.Click
formRoll.bringForward()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnShowForm.Enabled = False
btnBringFormForward.Enabled = False
btnAddButton.Enabled = False
End Sub
End Class

Monday, December 6, 2010

Monday, December 6th 2010

Here is a tutorial by Jesse Liberty on inheritance


Imports System
Public Class Window
'constructor takes to integers to fix location
'on the console

Public Sub New(ByVal top As Integer, ByVal left As Integer)
Me.top = top
Me.left = left

End Sub 'new

'simulaes drawing the window
Public Sub DrawWindow()
Console.WriteLine("Drawing window at {0}, {1}", top, Left)
End Sub 'draw window

'these members are private and thus invisible to
'derived class methods

Private top As Integer
Private left As Integer

End Class 'window


'Liar boz sweicwa deom Qinsoq
Public Class ListBox
Inherits Window

'constructor adds a parameter
Public Sub New(ByVal top As Integer, ByVal left As Integer, ByVal theContents As String)
MyBase.new(top, left) 'call base constructor
mListBoxContents = theContents
End Sub 'new

Public Shadows Sub DrawWindow()
MyBase.DrawWindow()
Console.WriteLine("Writing string to the ListBox: {0}", mListBoxContents)
End Sub 'DrawWindow

Private mListBoxContents As String 'new member variable

End Class 'listbox


Module module1
Sub main()
'create a base instance
Dim w As New Window(5, 10)
w.DrawWindow()

'create a derived instance
Dim lb As New ListBox(20, 30, "Hello World")
lb.DrawWindow()

Console.ReadLine()

End Sub
End Module


Here is something that I wrote about reading comma dilemeted text files.



Imports System
Imports System.IO


Public Class getFile


Dim fileAddress As String
Dim parsedArray(1, 30) As String

Dim stringLoad As String

Public Sub New()
Dim parentDirectory As String = "C:\Users\Alfred\Documents\VBPractice"
For Each fn In My.Computer.FileSystem.GetFiles(parentDirectory)
Dim fi As FileInfo = New FileInfo(fn)
Console.WriteLine("{0}: {1}", fi.Name, fi.Attributes.ToString())
Next


Console.WriteLine("Please enter the address of the file")
fileAddress = parentDirectory & "\" & Console.ReadLine()
Console.WriteLine("You have entered the address: " & fileAddress)
End Sub

Public Sub retrieve()
Console.WriteLine(fileAddress)

Dim objReader As New System.IO.StreamReader(fileAddress)
Console.WriteLine()

stringLoad = objReader.ReadToEnd
objReader.Close()
End Sub

Public Sub parse()

Dim strArray() As String = stringLoad.Split(",")

For i = 0 To strArray.GetUpperBound(0)
Console.WriteLine(strArray(i))
Next

Console.WriteLine(strArray(0))
Console.ReadLine()

parsedArray(0, 0) = strArray(0)
parsedArray(1, 0) = strArray(1)
Console.WriteLine(parsedArray(0, 0) & " " & parsedArray(1, 0))

Console.ReadLine()

For i = 0 To strArray.GetUpperBound(0) - 1
If (i + 1) Mod 2 = 0 Then
parsedArray(1, i) = strArray(i)
Else
parsedArray(0, i) = strArray(i)
End If
Next
End Sub

Public Sub print()
Dim stringHolder As String
For x = 0 To parsedArray.GetUpperBound(0)
Console.WriteLine("X = " & x)
Next

Console.ReadLine()

For y = 0 To parsedArray.GetUpperBound(1)
Console.WriteLine("Y = " & y)

Next

Console.ReadLine()

For x = 0 To parsedArray.GetUpperBound(0)
Console.WriteLine(parsedArray(x, 0))
Next
Console.ReadLine()

For y = 0 To parsedArray.GetUpperBound(1)

Console.WriteLine(parsedArray(0, y) & " " & parsedArray(1, y))

Next


End Sub

End Class


Module module1
Sub main()

Console.WriteLine("Getting file address...")
Dim fileRead As New getFile
continueContinue()

Console.WriteLine("Loading file...")
fileRead.retrieve()
continueContinue()

Console.WriteLine("Parsing file...")
fileRead.parse()
continueContinue()

Console.WriteLine("Displaying file...")
fileRead.print()
continueContinue()




End Sub

Sub continueContinue()
Console.WriteLine("Hit enter to continue")
Console.ReadLine()
End Sub


The final tutorial for today is one on returning an array from a procedure.


Imports System
Module module1

Sub main()
Dim numEntry As Integer
Dim rates() As Decimal = RateArray(9)
Dim indexInteger As Integer = 5
Do While indexInteger > 0
numEntry = Console.ReadLine()
Console.WriteLine(rates(numEntry))
indexInteger -= 1
Loop


End Sub


Private Function RateArray(ByVal elementCount As Integer) As Decimal()
Dim rates(elementCount - 1) As Decimal
For i As Integer = 0 To rates.Length - 1
rates(i) = (i + 1) / 100D
Next
Return rates
End Function
End Module


Pretty soon I am going to start studying JavaScript, among other things it would be good to take my mind off of VB for a while.

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

Thursday, December 2, 2010

Thursday, 12/2/2010


Imports System
Public Class time
'public member variables
Private mYear As Integer
Private mMonth As Integer
Private mDate As Integer
Private mHour As Integer
Private mMinute As Integer
Private mSecond As Integer

'this is my first time to use properties
Property Hour() As Integer
Get
Return mHour
End Get
Set(ByVal value As Integer)
mHour = value
End Set
End Property

'public accessor methods

Public Sub DisplayCurrentTime()
Console.WriteLine( _
"{0}/{1}/{2} {3}:{4}:{5}", _
mMonth, mDate, mYear, mHour, mMinute, mSecond)
End Sub 'DisplayCurrentTime

'constructors
Public Sub New(ByVal dt As System.DateTime)
mYear = dt.Year
mMonth = dt.Month
mDate = dt.Date
mHour = dt.Hour
mMinute = dt.Minute
mSecond = dt.Second
End Sub 'new

Public Sub New( _
ByVal mYear As Integer, _
ByVal mMonth As Integer, _
ByVal mDate As Integer, _
ByVal mHour As Integer, _
ByVal mMinute As Integer, _
ByVal mSecond As Integer)
Me.mYear = mYear
Me.mMonth = mMonth
Me.mDate = mDate
Me.mHour = mHour
Me.mHour = mMinute
Me.mSecond = mSecond
End Sub 'new

End Class 'Time

Module Module1

Sub main()
Dim currentTime As System.DateTime = System.DateTime.Now
Dim time1 As New time(currentTime)
time1.DisplayCurrentTime()

'extract the hour to a local variable
Dim theHour As Integer = time1.Hour

'display the local variable
Console.WriteLine("Retrieved the hour: {0}", theHour)

'add one to the local variable
theHour += 1
'write teh time back to the object
time1.Hour = theHour

'dispaly the result
Console.WriteLine("Update the hour: {0} ", _
time1.Hour)

Console.ReadLine()
End Sub
End Module

Wednesday, December 1, 2010

Tuesday 12/1/10

Here is another tutorial by Jesse Liberty


Imports System
'Fields hold the state of the object, and methods define the object's behavior.

Class cat
Private Shared instances As Integer = 0
Private weight As Integer
Private name As String

Public Sub New(ByVal name As String, ByVal weight As Integer)
instances += 1
Me.name = name
Me.weight = weight
End Sub

Public Shared Sub HowManyCats()
Console.WriteLine("{0} cats adopted ", instances)
End Sub

Public Sub TellWeight()
Console.WriteLine("{0} is {1} pounds", name, weight)
End Sub

End Class 'cat




Module Module1

Sub Main()
cat.HowManyCats()
Dim frisky As New cat("Frisky", 5)
frisky.tellWeight()
cat.HowManyCats()
Dim whiskers As New cat("Whiskers", 7)
whiskers.TellWeight()
whiskers.HowManyCats()

cat.HowManyCats()


Console.ReadLine()
End Sub

End Module


Here is another tutorial I worked on myself


Imports System

Public Class Employee
Private empID As Integer
Public Sub New(ByVal empID As Integer)
Me.empID = empID
End Sub
Public Overrides Function ToString() As String
Return empID.ToString()
End Function
End Class




Module Module1

Sub Main()
Dim empArray(5) As Employee
Dim counter As Integer = 0
Dim idNumber As Integer

While counter < 6
idNumber = Console.ReadLine()
empArray(counter) = New Employee(idNumber)
counter += 1
End While


Dim employeeInstance As Employee
For Each employeeInstance In empArray
Console.WriteLine("Employee ID# " & employeeInstance.ToString)
Next

Console.ReadLine()

End Sub

End Module


Here is another tutorial from the book by Jesse Liberty


Imports System

'The signature of a method is composed of its name and its parameter list

Public Class Time
'private member variables
Private yearYear As Integer
Private monthMonth As Integer
Private dateDate As Integer
Private hourHour As Integer
Private minuteMinute As Integer
Private secondSecond As Integer

'public accessor methods
Public Sub DisplayCurrentTime()
System.Console.WriteLine( _
"{0}/{1}/{2} {3}:{4}:{5}", _
monthMonth, dateDate, yearYear, hourHour, minuteMinute, secondSecond)
End Sub 'displayCurrentTime

'constructors
Public Sub New(ByVal dt As System.DateTime)
yearYear = dt.Year
monthMonth = dt.Month
dateDate = dt.Day
hourHour = dt.Hour
minuteMinute = dt.Minute
secondSecond = dt.Second
End Sub ' new

Public Sub New( _
ByVal aYear As Integer, _
ByVal aMonth As Integer, _
ByVal aDate As Integer, _
ByVal anHour As Integer, _
ByVal aMinute As Integer, _
ByVal aSecond As Integer)
Me.yearYear = aYear
Me.monthMonth = aMonth
Me.dateDate = aDate
Me.hourHour = anHour
Me.minuteMinute = aMinute
Me.secondSecond = aSecond
End Sub 'new
End Class 'time

Module Module1
Sub Main()
Dim currentTime As System.DateTime = System.DateTime.Now
Dim time1 As New Time(currentTime)
time1.DisplayCurrentTime()
Dim time2 As New Time(2005, 11, 18, 11, 3, 30)
time2.DisplayCurrentTime()
Console.ReadLine()
End Sub
End Module


Here is a tutorial about passing values on to subs using classes



Imports System
Public Class Tester
Public Sub run()
'declare a variable and initialize to 5
Dim theVariable As Integer = 5

'dipslay its value
Console.WriteLine("In Run. theVariable: {0}", theVariable)

'call a mthod & pass in the variable
Doubler(theVariable)

'return & display the value again
Console.WriteLine("Back in Run, theVariable: {0}", _
theVariable)
End Sub

Public Sub Doubler(ByVal param As Integer)
'display the value that was passed in
Console.WriteLine("In method1. received param: {0}", _
param)
'dobule the value
param *= 2

'display the dobuled 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