Advertisement
ASP_Volume3 String Manipulation #44415

Extract Numerical Values from Text Strings

The purpose of this routine is to take a string of text (such as with a textbox) and extract a numerical value from it. let's say that you have a textbox in which people enter dollar amounts. Many users are likely to enter something such as "$ 4,335.49" and expect calculations to be performed on it. The trouble is, the value of that string is 0 (zero), not 4335.49!

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
Function PurgeNumericInput (StringVal As Variant) As Variant
  On Local Error Resume Next
  Dim x As Integer
  Dim WorkString As String
  
  If Len(Trim(StringVal)) = 0 Then Exit Function ' this is an empty string
  For x = 1 To Len(StringVal)
    Select Case Mid(StringVal, x, 1)
      Case "0" To "9", "." 'Is this character a number or decimal?
        WorkString = WorkString + Mid(StringVal, x, 1) ' Add it to the string being built
    End Select
  Next x
  PurgeNumericInput = WorkString 'Return the purged string (containing only numbers and decimals
End Function


You then just need to call the function passing a string argument to it. An example is shown below. 

Sub Command1_Click
  Dim NewString as Variant
  NewString = PurgeNumericInput("$44Embedded letters and spaces 33 a few more pieces of garbage .9")
  If Val(NewString) 0 Then
    MsgBox "The Value is: " & NewString
  Else
    MsgBox "The Value is ZERO or non-numeric"
  End If
End Sub


Notice how much alphanumeric garbage was placed in the string argument. However, the returned value should be 4433.9! Two questions might arise when using this type of example. 
#1 - What if the string was "0"? This could be determined by checking the length of the string (variant) returned. If the user entered a "0" then the length of the string would be > 0. 
#2 - What if the string contains more than one decimal? You could use INSTR to test for the number of decimals. However, chances are, if the user entered more than one decimal you might better have them re-enter that field again anyway. <sly smile>
Upload
<?
/*
annotate.php3 
This is a module that can be placed on any php3 page to allow users to add
their comments. The comments are stored in a file in the current directory,
whose name is constructed by adding ".comment" to the calling page's name,
and merged into the calling page dynamically. (The calling page is not
modified.)
I wrote this because I wanted a simple way to add this functionality to my
pages without requiring that mySQL be available.
In the message input, blank lines are converted to paragraph tags. No other
conversions are applied. If you don't want your users to be able to input
html, uncomment the "strip_tags" line.
Note that the directory must be writable by the web server.
Put this module in some convenient location and then embed it in your pages
like so:
require("/some/full/path/annotate.php3");
or, relative to the docroot:
require($DOCUMENT_ROOT . "/relativepath/php3");
Steve Yelvington <steve@yelvington.com>
*/
if ($message)
	{
	/* uncomment the next two lines to strip out html from input */
	/* $name = strip_tags($name); */
	/* $message = strip_tags($message); */
	$message = ereg_replace("\r\n\r\n", "\n<P>", $message);
	$date = date("l, F j Y, h:i a");
	$message = "<B>$name </B> -- $date<P> $message <BR><HR>";
	$fp = fopen (basename($PHP_SELF) . ".comment", "a");
	fwrite ($fp, $message);
	fclose ($fp);
	}
@readfile(basename(($PHP_SELF . ".comment")));
?>
<FORM method="post">
<b>Your name:</b><BR><INPUT name="name" type="text" size="55"><BR>
<b>Your comment:</b><BR><TEXTAREA name="message" rows=10 cols=55 wrap=virtual>
</TEXTAREA><BR>
<INPUT name="submit" type="submit" value="Post your comments">
</FORM>


<P align=center>&nbsp;</P>
<P align=center><font size="6" color="#0000CC">Part I</font></P>
		<P align="center"><FONT size="5">Learn VB .NET fast!</FONT></P>
		<P align="left">So what's the difference between vb 6 and vb .net?
		</P>
		<P align="left">you may have asked yourself that but when you find the answer you will be 
			almost surprised.</P>
		<P align="left">The first difference and the most important&nbsp;one is that &nbsp;vb 
			.NET is a complete Object Oriented Programming language with all the options in 
			an OOP language like classes , Implementation , Inheritance etc.</P>
		<P align="left">Here is&nbsp;a brief description of each of these new features:</P>
		<P align="left">(Notice that I removed the Public statement from the 
    beginning of the class declarations because it is not almost needed when 
    working with only one module)</P>
		<P align="center"><FONT size=5>Class</FONT></P>
		<P align="left">A class is a set of properties , and methods in one 
    piece of code that can be used multiple times. a class 
			itself is not an object but objects can be created from classes.</P>
		<P align="left">The following code demonstrates a simple class:</P>
		<P align="left"><FONT color=#0000cc size=2>Class SimpleClass</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public Sub 
SimpleMethod()</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
System.Console.Write("Simple Class")</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Sub</FONT></P>
		<P align="left"><FONT color=#0000cc size=2>End 
Class</FONT></P>
<P align=left>The above code shows the declaration of the SimpleClass(note that 
this class have no use unless you make an Instance of it)</P>
<P align=left>The following code shows how to use the SimpleClass class:</P>
<P align=left><FONT color=#0000cc size=2>Class Prog1</FONT></P>
<P align=left><FONT color=#0000cc 
size=4>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<FONT color=#0000cc 
size=2>Public&nbsp; Shared Sub Main()</FONT></P>
<P align=left><FONT color=#0000cc 
size=4>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 
</FONT><FONT color=#0000cc 
size=2>Dim obj as New SimpleClass()</FONT></P>
<P align=left><FONT color=#0000cc 
size=4>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000cc 
size=4>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></FONT><FONT color=#0000cc 
size=2> obj.SimpleMethod()</FONT></P>
<P align=left><FONT color=#0000cc 
size=4>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<FONT color=#0000cc 
size=2>End Sub</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>End Class</FONT></P>
		<P align="left">Any program in vb .NET must have at least one 
class (or module) with a 'Main' method which is where the program starts. This 
    sub must be shared (explained later)</P>
<P align=left>in the above example the program's class is called "Prog1" and it 
has a main method and an instance of the SimpleClass class is created using the 
'Dim' statement , Dim statement is used to declare Variables and Objects. 'New' 
statement makes a new instance and gives it the memory space it needs , 
remember that if you don't use the 'New' statement you will only get a 
reference no an object unless you set it to an object again using the 'New' 
statement.</P>
<P align=left>To separate a method or property from its owner object you must 
use dot ('.')&nbsp; like obj.SimpleMethod()</P>
<P align=left>&nbsp;</P>
<P align=center><FONT size=5>Class members</FONT></P>
<P align=left>Class members can be Public&nbsp;, Protected and Private.</P>
<P align=left>Members that are public are visible to objects derived from the 
class, classes that inherit this class and the class itself.</P>
<P align=left>Members that are private are only visible to the class itself.</P>
    <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
     <tr>
      <td width="25%">
      <p align="center">Member Type/What can access it</td>
      <td width="25%">
      <p align="center">The class itself</td>
      <td width="25%">
      <p align="center">Derived Classes</td>
      <td width="25%">
      <p align="center">Objects derived from the class</td>
     </tr>
     <tr>
      <td width="25%">
      <p align="center">Public</td>
      <td width="25%">
      <p align="center">Yes</td>
      <td width="25%">
      <p align="center">Yes</td>
      <td width="25%">
      <p align="center">Yes</td>
     </tr>
     <tr>
      <td width="25%">
      <p align="center">Protected</td>
      <td width="25%">
      <p align="center">Yes</td>
      <td width="25%">
      <p align="center">Yes</td>
      <td width="25%">
      <p align="center">No</td>
     </tr>
     <tr>
      <td width="25%">
      <p align="center">Private</td>
      <td width="25%">
      <p align="center">Yes</td>
      <td width="25%">
      <p align="center">No</td>
      <td width="25%">
      <p align="center">No</td>
     </tr>
    </table>
<P align=left>Members that are protected can be used in the class itself and 
classes that inherit the class.</P>
<P align=left>The following example shows how to use different member kinds</P>
<P align=left><FONT color=#0000cc size=2>Class SimpleClass2</FONT></P>
<P align=left><FONT color=#0000cc size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Private 
MemberVar1 as Long</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Protected MemberVal2 as 
Long</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public MemberVal3 as 
Long</FONT></P>
<P align=left><FONT color=#0000cc size=2>End Class</FONT></P>
<P align=left><FONT color=#0000cc size=2>Class Prog2 </FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public&nbsp; Shared Sub Main()</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim obj as New 
Simpleclass2()</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'obj.MemberVal1 = 
1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'wrong because 
it's a private member</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'obj.MemberVal2 = 
2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'wrong because 
it's a&nbsp;protected member</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj.MemberVal3 = 
3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'correct because 
it's a&nbsp;public member</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Sub&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></P>
<P align=left><FONT color=#0000cc size=2>End Class</FONT></P>
<P align=left><FONT color=#0000cc size=4></FONT>&nbsp;</P>
<P align=center><FONT color=#000000 size=5>Constructors </FONT></P>
<P align=left>Sometimes you want to give some members of a class some 
initialization value when the object is created . This can be done by using the 
'New'&nbsp;as sub.</P>
<P align=left>The following code shows how to use them:</P>
<P align=left><FONT color=#0000cc size=2>Class SimpleClass3</FONT></P>
<P align=left><FONT color=#0000cc><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public Var as 
Long&nbsp;</font></P>
<P align=left><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public Sub New()</font></P>
<P 
align=left><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Var = 10&nbsp; 'Initialization value</font></P></FONT>
<P align=left><FONT color=#0000cc size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End 
Sub</FONT></P>
    <FONT color=#0000cc 
size=2>
<P align=left>End Class</FONT></P>
<P align=left><FONT color=#0000cc size=2>Class Prog4</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public Shared&nbsp; Sub Main()</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim obj as New 
Simpleclass3()</FONT></P>
<P align=left><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</font><font size="2" color="#0000CC">System.Console.Write(obj.Var)&nbsp;</font></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Sub&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></P>
<P align=left><FONT color=#0000cc size=2>End Class</FONT></P>
<P align=left>When the object is created , the value of Var is set to 10.</P>
<P align=left>You also send parameters to the constructor like this:</P>
<P align=left><FONT color=#0000cc size=2>Class SimpleClass4</FONT></P>
<P align=left><FONT color=#0000cc><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public Var 
as Long&nbsp;</font></P>
<P align=left><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public Sub New(InitValue 
as Long)</font></P>
<P 
align=left><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Var = InitValue&nbsp; 'Initialization value</font></P>
<P align=left><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End 
Sub</font></P>
<P align=left><font size="2">End Class</font></FONT></P>
<P align=left><FONT color=#0000cc size=2>Class Prog5</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public&nbsp; Shared Sub Main()</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim obj as New 
Simpleclass4(1000)&nbsp;&nbsp; 'The initialization value is 1000</FONT></P>
<P align=left><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</font><font size="2" color="#0000CC">System.Console.Write(obj.Var)&nbsp;</font></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Sub&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></P>
<P align=left><FONT color=#0000cc size=2>End Class</FONT></P>
    <p align="center"><font size="5">Shared Methods</font></p>
<P align=left>Sometimes you want to write a class that has methods which are not 
related to a specific object e.g. a class that has math methods. So you want to 
use the methods without creating an object first. In this case you can declare 
it using the 'Shared' statement the following code is an example of shared 
methods:</P>
<P align=left>NOTE: shared methods and properties can not access methods, 
properties or variables that are not shared.</P>
<P align=left><FONT color=#0000cc size=2>Class SimpleClass5</FONT></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Public Shared Function Multiply (ByVal Num1 as Long , ByVal Num2 as Long) as 
Long</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Return Num1*Num2</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
End Sub</font></P>
    <FONT color=#0000cc size=2>
<P align=left>End Class</P>
    </FONT>
<P align=left>&nbsp;</P>
<P align=left><FONT color=#0000cc size=2>Class Prog6</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public&nbsp; Shared Sub Main()</FONT></P>
<P align=left><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</font><font size="2" color="#0000CC">System.Console.Write(SimpleClass5.Multiply(10,10))&nbsp;</font></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Sub&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></P>
<P align=left><FONT color=#0000cc size=2>End Class</FONT></P>
<P align=center><font size="6" color="#0000CC">Part II</font></P>
    <p align="center"><font size="5">Inheritance</font></p>
<P align=left>This subject is almost the most important in OOP, it makes writing 
codes a lot faster because you don't have to write code for each of the classes 
and instead you write a general class and other classes will inherit it. for 
example in a game you write a general character class then you can write more 
specific classes that inherit this class for example enemy class and friend 
class and again more detailed classes which inherit these items.</P>
<P align=left>And the great thing is that inheritance is available in VB .NET. 
the following example shows how to use it:</P>
<P align=left><FONT color=#0000cc size=2>Class A&nbsp;&nbsp;&nbsp;&nbsp; ' 
parent class</FONT></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Public Sub A()</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
System.Console.Write(&quot;A&quot;)</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
End Sub</font></P>
    <FONT color=#0000cc size=2>
<P align=left>End Class</P>
    </FONT>
<P align=left><FONT color=#0000cc size=2>&nbsp;Class B&nbsp;&nbsp;&nbsp;&nbsp; ' 
fist child class</FONT></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Inherits A&nbsp;&nbsp;&nbsp; ' this line tells VB that this class inherits A</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Public Sub B()</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
System.Console.Write(&quot;B&quot;)</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
End Sub</font></P>
    <FONT color=#0000cc size=2>
<P align=left>End Class</P>
    </FONT>
<P align=left><FONT color=#0000cc size=2>Class C&nbsp;&nbsp;&nbsp;&nbsp; 'second 
child class</FONT></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Inherits B&nbsp;&nbsp;&nbsp; ' this line tells VB that this class inherits A</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Public Sub C()</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
System.Console.Write(&quot;C&quot;)</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
End Sub</font></P>
    <FONT color=#0000cc size=2>
<P align=left>End Class</P>
    </FONT>
<P align=left><FONT color=#0000cc size=2>Class Prog6</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public&nbsp; Shared Sub Main()</FONT></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Dim objC as New C()&nbsp; ' always remember to use parentheses when using the 
New statement</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
objC.A()&nbsp; 'because C inherits B and B inherits A so it has A() too</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
objC.B()&nbsp; 'inherits B() directly from B</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
objC.C()&nbsp; 'classes own sub</font></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Sub&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></P>
<P align=left><FONT color=#0000cc size=2>End Class</FONT></P>
    <p align="center"><font size="5">Abstract classes (MustInherit)</font></p>
<P align=left>Some classes are not used to create object directly from them but 
are used to write classes that inherit them. Following code shows this matter:</P>
<P align=left><FONT color=#0000cc size=2>MustInherit Class A&nbsp;&nbsp;&nbsp;&nbsp; 
' parent class</FONT></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Public Sub A()</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
System.Console.Write(&quot;A&quot;)</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
End Sub</font></P>
    <FONT color=#0000cc size=2>
<P align=left>End Class</P>
    </FONT>
<P align=left><FONT color=#0000cc size=2>&nbsp;Class B&nbsp;&nbsp;&nbsp;&nbsp; ' 
fist child class</FONT></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Inherits A&nbsp;&nbsp;&nbsp; ' this line tells VB that this class inherits A</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Public Sub B()</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
System.Console.Write(&quot;B&quot;)</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
End Sub</font></P>
    <FONT color=#0000cc size=2>
<P align=left>End Class</P>
    </FONT>
<P align=left><FONT color=#0000cc size=2>Class Prog7</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public Shared&nbsp; Sub Main()</FONT></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
'Dim objA as New A()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'this is wrong because 
you can not create an instance from a mustinherit class</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Dim objB as New B()&nbsp; ' always remember to use parentheses when using the New 
statement</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
objB.A()&nbsp; </font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
objB.B()&nbsp; </font></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Sub&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></P>
<P align=left><FONT color=#0000cc size=2>End Class</FONT></P>
    <p align="center"><font size="5">Overriding</font></p>
<P align=left>When writing a class that inherits another class you may want to 
change what a sub or function in the base class does&nbsp; in this case you can 
use overriding:</P>
<P align=left><FONT color=#0000cc size=2>Class A&nbsp;&nbsp;&nbsp;&nbsp; ' 
parent class</FONT></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Public Overridable Sub A()</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
System.Console.Write(&quot;A.A&quot;)</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
End Sub</font></P>
    <FONT color=#0000cc size=2>
<P align=left>End Class</P>
    </FONT>
<P align=left><FONT color=#0000cc size=2>&nbsp;Class B&nbsp;&nbsp;&nbsp;&nbsp; ' 
fist child class</FONT></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Inherits A&nbsp;&nbsp;&nbsp; ' this line tells VB that this class inherits A</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Public Overrides Sub A()</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
System.Console.Write(&quot;B.A&quot;)</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
End Sub</font></P>
    <FONT color=#0000cc size=2>
<P align=left>End Class</P>
    </FONT>
<P align=left><FONT color=#0000cc size=2>Class Prog8</FONT></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public Shared Sub Main()</FONT></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Dim objB as New B()&nbsp; </font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
objB.A()&nbsp; ' the output will be &quot;B.A&quot; not &quot;A.A&quot;</font></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Sub&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></P>
<P align=left><FONT color=#0000cc 
size=2>End Class</FONT></P>
    <p align="center"><font size="5">Events</font></p>
    <p align="left">If you have used VB 6 or any other windows programming 
    language then you must be already familiar with events.</p>
    <p align="left">An event is a piece of code (a procedure) which executes 
    when something especial happens such as the users clicks a button , or 
    text of a textbox changes.</p>
    <p align="left">classes can have events. Following code shows how to use 
    events:</p>
<P align=left><FONT color=#0000cc size=2>Class A</FONT></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Public Event OnInitialize&nbsp; ' declares the event as public</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Public Sub New()</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
RaiseEvent OnInitialize() ' causes the event to be executed</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
End Sub</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Public Overridable Sub A()</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
System.Console.Write(&quot;A.A&quot;)</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
End Sub</font></P>
    <FONT color=#0000cc size=2>
<P align=left>End Class</P>
    </FONT>
<P align=left><FONT color=#0000cc size=2>Class Prog9</FONT></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Private WithEvents objA as New A()&nbsp; ' notice 'WithEvents' it tells vb that 
this class has events</font></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public Shared Sub Main()</FONT></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Dim objB as New B()&nbsp; </font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
objB.A()&nbsp; ' the output will be &quot;B.A&quot; not &quot;A.A&quot;</font></P>
<P align=left><FONT color=#0000cc 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Sub&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Private sub objA_OnInitialize() Handles objA.OnInitialize&nbsp;&nbsp;&nbsp; ' 
&quot;Handles&quot; tells vb that this sub is the event handler for the &quot;objA.onInitialize&quot; 
event</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
System.Console.WriteLine(&quot;objA initialized&quot;)</font></P>
<P align=left><font size="2" color="#0000CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
End Sub</font></P>
<P align=left><FONT color=#0000cc 
size=2>End Class</FONT></P>
<p align="left"><font size="5">This time it has the sample files!</font></p>
<p align="left"><font size="5">More coming soon!</font></p>
<P align=left>&nbsp;</P>
<P align=center>&nbsp;</P>
<P align=left>&nbsp;</P>
<P align=left>&nbsp;</P>
Original Comments (3)
Recovered from Wayback Machine