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.

4 comments:

Brad Jensen said...

I didn't understand the inheritance example at all on first reading. So I googled shadows sub and vb.net and came to this page:

http://stackoverflow.com/questions/463209/shadows-vs-overrides-in-vb-net

Which has a lively dicussion of the difference between shadows and overrides.

The basic difference is that shadows completely replaces the mthod of the base class, the one your current class is dervied from. In the tutorial, the class window is defined with methods new and drawwindow. Then the class listbox is defined with the class window as its base class, the class it is built on top of.

Calling these classes 'window' and 'listbox' completely misled me with my vb6 background, because I thought these actually meant something in the language. They are just names.

I assume from the context that MyBase is a keyword to refer to the base class, which for 'listbox' in this example, would be 'window'.

So in the Listbox.new method the reference to mybase.new means window.new. Similarly, in the listbox.drawwindow method, the reference to mybase.drawwindow means window.drawwindow.

In the good old days of windows programming, we used to say every object on a window is another window. So a listbox on a window is really another window.

I don't know enough about vb.net object oriented syntax yet to really understand why you woulld use 'overrides' or 'shadows' in the method definitions for the derived listbox class. I would think just defining a method with the same name would replace the method in the mybase class, but apparently not.

Brad Jensen said...

When I think of derived classes, I think of thoe russian dolls that are one inside the other, with the derived class the outermost doll.

Brad Jensen said...

In the csv (comma separted values) reading example,t he one change I would make is to create a second array, and use it to split the stringload varaibel which contains the entire text of the file, by either vbcrlf (a normal text editor file, or one created with vb 'print' commands) or vblf (a unix-created file generally only has line feeds, denoting the austere or lazy character of unix prrogrammers, depending on your point of view). then make your a for/next on the individual lines, and split them by the comma character.

Of course, this doesn't work if you have values inside quotes with embedded commas like "Tulsa, OK" , which is one reason that many programs output such file as tab delimited instead of comma delimited , and outlaw tabs inside string values. tab is ascii character value 9.

Brad Jensen said...

The example of returnning an array shows the importance of coments in code.

the line:

Dim rates() As Decimal = RateArray(9)

is easy to miss at first. how can an array be defined as one array member of another array? Well, it isn't. ratearrya(9) is actually a function that returns an array, which is obvious as you think about it. A coment on this line or a separate comment line above would be very helpful, like 'set and array of rates'

Besides programming the computer, you are also programming the programmer who reads your code in the future. How do you get that person's attention, and help them avoid error in understanding your code?