The String Class VB Help

The last example in this section is a very simple Class that implements a few useful string operations The String Class provides three methods which are implemented as Public functions and they are:

•Integer2Binary(number As.Long) Converts its numeric argument to a binary number arnd returns the binary value as a string .
• Number2String(number As Integer) Converts its numeric argument toa string and returns it. If the numbs .5 :3passed to this method as argument the return value will be the string “three hundred ninety-five.”
• Lower Caps(str As StrUtg) Converts its argument to lower caps and returns the new string. If the value “three hundred ninety-nine” is passed to this method as ~t the return value will be “Three Hundred Ninety-Nine”

The members of the String Class are quite simple but they can serve as your starting point for creating a custom Class with all the string and number manipulation functions you frequently use in your projects that are not readily available in Visual Basic.

The three methods of the String Class are implemented as Public functions and they are listed next

The Method Of The String Class

Public Function Number2String(Number)
Dim tenth As Integer
Dim leftover As Integer
Dim hundred As Integer
Dim thousand As Integer

If Number < 20 Then ‘Reads unique numbers
NumString – ReadSingle(Number)
ElseIf Number < 100 Then· ‘Reads numbers less than 100
tenth – Fix(Number I 10)
NumString – ReadTenths(tenth * 10}
leftover – Number – ~tenth * 10)
If leftover> 0 Then ..
NumString – NumString & “”&ReadSingle(leftover)
End If
ElseIf Number.< 1000 Then ‘values between 100 and 999
hundred – Fix(Nu~ber I 100)
NumString – ReadSingle(hundred) & “hundred”
leftover – Number – (hundred * 100)
If l~ftover > 0 Then
tenth – Fix(leftover / 10)
If tenth> 0 Then NumString – _
i NumString & “”& ReadTent s(tenth * 10)
leftover – Numher – (hundred * )00) – (tenth * 10)
If leftover> 0 rhen
NumString – NumString & “”& _

End If
End If
else ‘Reads number between 1000 and 9999
thousand – Fix(Number I 1000)
NumString – ReadSingle(thousand) & ‘thousand”
leftover – Number – ~thousand ~ 1000)
If leftover> 0 Then
hundred – Fix(leftover / 100)
If hundred > 0 Then
NumString • NumString &, , , &,
ReadSingle(hundred) & ‘hundred’
End If
leftover ft Number – (thousand ~ 1000) –
(hundred ~ 100)
If leftover> 0 Then
tenth – Fix(leftover / 10)
If tenth > 0 Then
NumString – NumString & . ,&
ReadTenths(tenth ~ 10)’
~,….,.•i l ‘.. …,Ii ‘
End If
leftover • Number – (thousand * 1000) –
(hundred * 100) – (tenth * 10)
,; ; !., If .leftover >. 0 Th”n
!, NurnString· NumString” • ” &, –
. r. il’.1I’/ 110/, II. ‘:1’
End If
End If
End If
End If
Number2String – NumString
End Function ~
Public Function Lo~erCaps(str As String) As String
Dim neWWord ~s String, newStr As String
Dim tempStr As String .~
Dim Wl)elimiter As Integer
tempStr  Trim(str)
WDelimiter – InStr(tempStr ‘)
Whil. WDelimiter > 0
newWord  Left(tempStr, WDelimiter)
tempStr _ Right(tempStr Len(tempStr) – WOelimiter)
newStr  newStr & UCase(Left(newWord 1)) & –
Mid(newWord 2 Len(newWord) )
WDelimiter = InStr(tempStr  ‘)
We”d .
newWord = tempStr
newStr =.newStr &. UCase(Left(nevNiord, 1)) & -‘
Mid(newWord 2 ~ (newWord) – 1)
LowerCaps – newStr
End Function
Public Function Integer2Binary(ByVal Numbe~As Long) As String
“HexNum = Hex(Number)
For i = 1 To Len(HexNum)
,BinNum= BjnNum & BinaryDigits(“&H’ & Mid(HexNum. _i. 1»
Integer2Binary BinNum
End Function

The Number2StringO function is the most complicated one’ It’s similar to a function used .by the READNUM project in Appendix C (on the CD) to read out numeric values. The Number 2 String O function can convert Integer values in the range from o to 9999 but you can easily add the code to make it work with larger values and
non-integer.values. The Number 2 String O function calls the functions Read and Read). which are private to the Class meaning they can be called from within the Class, but outside programs can’t contact them. If you view the me”members of the String Class Class with the Object Browser you will see the private.members of the Class. However they will be listed as Private which lets you know that you can’t access them from within your application.

the test Form of the String Class is shown in Figure 15.10. The user can enter a numeric value in the text box at the top of the Form and click the Convert to String or Convert to Binary buttons to convert the value to a string Or binary value respectively. Although the textbox under the Convert to String button contains a” string (the string that corresponds to the numeric value or a user-supplied value) the user can click the Convert to L Caps button to convert the string to lower caps.

The test Form of the
String Class

FIGURE 15.10:

FIGURE 15.10:

The code of the test Form “is quite simple. First, an object variable that will be used to access the String Class is declared

Dim NS As New Num Strings String Class

Num Strings is the name of the project and String Class the name of the Class Module. The code behind the three Command buttons is shown next:

Private Sub Commandl_Clitk()
Text2.Text – NS.Number2String(Textl.Text)
End Sub

Private Sub Command2_Click()
Dim NStr As String
NStr – NS.Number2String(Textl.Text)
Text3.Text – NS.lowerCaps(NStr)
End Sub
Private Sub Command3_Click()
Text4.Text – NS.lnteger2Binary(Textl.Text)
End Sub

Each button calls a different method of the NS variable. The code behind the Command 2 button calls two methods one after the other. First it converts the numeric value in the Text! control to a string then it calls the Lower Caps method to convert the string to lower caps. The two calls can be combined in a single statement as
shown next:

Text3.TextNStr – ~S.lowerCaps(NS.Number2String(Textl.Text))

In this chapter’s folder on the CO you’ll find the NumStr project. Open it with Visual Basic and examine its code or add new members to it. Now is a good time to register this Class with your system so you can use it in the examples of Chapter 22 where we’ll build an Active Server Page that contacts this component to
create HTML pages.

In the following chapter we are going to look at the second category of ActiveX components the ActiveX controls AcitiveX controls are far more common than ActiveX OLLs and quite a bit more interesting because they have a visible user interface. The programmatic interface of ActiveX controls is identical to that of ActiveX code components. The process of implementing properties and methods
for ActiveX controls is also identical to the one described in this chapter. In addition you must design the visible interface of the control which is quite similar to designing Forms.


Posted on November 5, 2015 in Building AdiveX Components

Share the Story

Back to Top