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.
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.