Thursday, August 4, 2011

Thursday 8.4.11


Option Strict On
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 'new

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

'these members are private and thus invisible
'to derived class methods; we'll examine this later in the chapter
Private top As Integer
Private left As Integer

End Class 'Window

'ListBox derives from Window
Public Class ListBox
Inherits Window
Dim mListBoxContents As String
'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

'a shadow version (note keyword) because in the derived method we chagne the behavior
Public Shadows Sub DrawWindow()
Console.WriteLine("Using myBase")
MyBase.DrawWindow() 'invoke the base method
Console.WriteLine("Writing string to the listbox: {0}", mListBoxContents)
End Sub 'drawWindow
End Class

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()
End Sub
End Module




Option Strict On
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 'new

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

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

Protected top As Integer
Protected left As Integer

End Class 'window

'ListBox derives from Window
Public Class ListBox
Inherits Window
'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 ' new

'an overridden version(note keyword)
'because in the derived method we change the behavior
Public Overrides Sub DrawWindow()
MyBase.DrawWindow() 'invoke 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 overriden version (note keyword) because in the derived method
'we change the behavior
Public Overrides Sub DrawWindow()
Console.WriteLine("-------------------------------")
Console.WriteLine("Drawing a button at {0}, {1}" + ControlChars.Lf, top, left)
Console.WriteLine("-------------------------------")
End Sub 'DrawWindow
End Class 'Button

Module Module1
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, "ListBox in array")
winArray(2) = New Button(5, 6)

Dim i As Integer
For i = 0 To 2
winArray(i).DrawWindow()
Next i
End Sub
End Module




Option Strict On
Imports System
Public Class Dog
Private weight As Integer
'constructor
Public Sub New(ByVal weight As Integer)
Me.weight = weight
End Sub 'new

'override Object.ToString
Public Overrides Function ToString() As String
Return weight.ToString()
End Function 'ToString
End Class 'Dog

Module Module1
Sub Main()
Dim i As Integer = 5
Console.WriteLine("The value of i is: {0}", i.ToString())

Dim milo As New Dog(52)
Console.WriteLine("My dog Milo weighs {0} pounds", milo.ToString())
End Sub 'main
End Module 'tester




Option Strict On
Imports System

Public Class UnboxingTest
Public Shared Sub Main()
Dim myIntegerVariable As Integer = 123
'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)
End Sub
End Class





using System;

namespace MainProgram
{

static class CharStrExtMethods
{
public static bool IsCharEqual(this char firstChar, char secondChar)
{
//calls the seocnd IsCharEqual method with two parameters
return (IsCharEqual(firstChar, secondChar, false));
}

public static bool IsCharEqual(this char firstChar, char secondChar, bool caseSensitiveCompare)
{
if (caseSensitiveCompare)
{
return (firstChar.Equals(secondChar));
}
else
{
return (char.ToUpperInvariant(firstChar).Equals(char.ToUpperInvariant(secondChar)));
}
}


}//end class CharStrExtMethods


class MainClass
{
static void Main(string[] args)
{
Console.WriteLine("hello world");
char char1 = 'c';
char char2 = 'X';
char char3 = 'f';
char char4 = 'F';

Console.WriteLine(CharStrExtMethods.IsCharEqual(char1, char2));
Console.WriteLine(CharStrExtMethods.IsCharEqual(char3, char4));
Console.WriteLine(CharStrExtMethods.IsCharEqual(char3, char4, true));
}
}
}

No comments: