Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript Class and Class...End

Status
Not open for further replies.

mcnoob

IS-IT--Management
Nov 15, 2010
5
US
Hi all,

I'm currently tinkering around with some exercises I have in my VBScript for Beginners book and I'm stuck on creating Class objects. What I'm trying to do (and failing at) is move the calculations in the code I'm attaching into a class:

Option Explicit

Const Title = "Temperature Conversion"

Dim Conversion
Dim Celsius
Dim Fahrenheit
Dim Moar
Dim WshShell
Dim WshObject

Set WshShell = WScript.CreateObject("WScript.Shell")

Function Fheit(Celsius)
Fheit = (Celsius * 9 / 5) + 32
End Function

Function CCalc(Fahrenheit)
CCalc = (Fahrenheit - 32) * 5 / 9
End Function

On Error Resume Next

Do
Conversion = WshShell.RegRead("HKCU\Software\WSH\Temperature Conversion\Type")
Conversion = InputBox("Enter: " & _
vbCrLf & "F to convert Fahrenheit to Celsius" & _
vbCrLf & "C to convert Celsius to Fahrentheit", Title, Conversion)
Select Case Conversion
Case ""
WScript.Quit
Case "F", "f"
Fahrenheit = WshShell.RegRead("HKCU\Software\WSH\Temperature Conversion\Temp\Fahrenheit")
Fahrenheit = InputBox("Enter a Fahrenheit" & _
" Temperature", Title, Fahrenheit)
If Fahrenheit = "" Then
WScript.Quit
Else
MsgBox Fahrenheit & " degrees Fahrenheit is " & CCalc(Fahrenheit) & _
" degrees Celsius"
End If
Case "C", "c"
Celsius = WshShell.RegRead("HKCU\Software\WSH\Temperature Conversion\Temp\Celsius")
Celsius = InputBox("Enter a Celsius" & _
" Temperature", Title, Celsius)
If Celsius = "" Then
WScript.Quit
Else
MsgBox Celsius & " degrees Celsius is " & Fheit(Celsius) & _
" degrees Fahrenheit"
End If
End Select
Moar = MsgBox("Would you like convert more temperatures?", vbOKCancel, Title)
WshShell.RegWrite "HKCU\Software\WSH\Temperature Conversion\Type", Conversion
WshShell.RegWrite "HKCU\Software\WSH\Temperature Conversion\Temp\Celsius", Celsius
WshShell.RegWrite "HKCU\Software\WSH\Temperature Conversion\Temp\Fahrenheit", Fahrenheit
Loop Until Moar <> vbOK

What I have thus far which I *think* is correct is:

Option Explicit

Class TemperatureConverter

Function Fheit(Celsius)
Fheit = (Celsius * 9 / 5) + 32
End Function

Function CCalc(Fahrenheit)
CCalc = (Fahrenheit - 32) * 5 / 9
End Function

End Class

Const Title = "Temperature Conversion"

Dim Conversion
Dim Celsius
Dim Fahrenheit
Dim Moar
Dim WshShell
Dim WshObject

Set WshShell = WScript.CreateObject("WScript.Shell")

On Error Resume Next

Do
Conversion = WshShell.RegRead("HKCU\Software\WSH\Temperature Conversion\Type")
Conversion = InputBox("Enter: " & _
vbCrLf & "F to convert Fahrenheit to Celsius" & _
vbCrLf & "C to convert Celsius to Fahrentheit", Title, Conversion)
Select Case Conversion
Case ""
WScript.Quit
Case "F", "f"
Fahrenheit = WshShell.RegRead("HKCU\Software\WSH\Temperature Conversion\Temp\Fahrenheit")
Fahrenheit = InputBox("Enter a Fahrenheit" & _
" Temperature", Title, Fahrenheit)
If Fahrenheit = "" Then
WScript.Quit
Else
MsgBox Fahrenheit & " degrees Fahrenheit is " & CCalc(Fahrenheit) & _
" degrees Celsius"
End If
Case "C", "c"
Celsius = WshShell.RegRead("HKCU\Software\WSH\Temperature Conversion\Temp\Celsius")
Celsius = InputBox("Enter a Celsius" & _
" Temperature", Title, Celsius)
If Celsius = "" Then
WScript.Quit
Else
MsgBox Celsius & " degrees Celsius is " & Fheit(Celsius) & _
" degrees Fahrenheit"
End If
End Select
Moar = MsgBox("Would you like convert more temperatures?", vbOKCancel, Title)
WshShell.RegWrite "HKCU\Software\WSH\Temperature Conversion\Type", Conversion
WshShell.RegWrite "HKCU\Software\WSH\Temperature Conversion\Temp\Celsius", Celsius
WshShell.RegWrite "HKCU\Software\WSH\Temperature Conversion\Temp\Fahrenheit", Fahrenheit
Loop Until Moar <> vbOK

Any input anyone could provide is greatly appreciated.
 
[0] Take out all the wshshell and related registry read write: that is terrible thing to do. Besides, most are utterly non-sense as you read some data from registry put it in a variable and then you read some input and put it to the same variable. So what is the purpose of reading the registry?

[1] To make good use of the class, in the else part of the if-then-else conditional, do this for case "F" or "f".
[tt]
If Fahrenheit = "" Then
WScript.Quit
Else
[red]set obj=new TemperatureConverter[/red]
MsgBox Fahrenheit & " degrees Fahrenheit is " & [red]obj.[/red]CCalc(Fahrenheit) & _
" degrees Celsius"
End If
[/tt]
[1.1] Same for case "C" or "c".
 
Very good thank you. It was not working but then I Dim'd Obj and it ran perfectly.
 
>So what is the purpose of reading the registry

Looks like it is - basically - to default to the last used set of parameters. But I agree that the way it is implemented is ... um ... unneccessary.
 
Trust me I know. These exercises have no real world use, they are just aggravating learning procedures...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top