今回は Decimal 型を扱う演算子をすべて VB のメソッドとして備えて ActiveX Component として実装します。それを VBScript から呼び出しますので、結局は単純なラッパとしての位置づけにすぎません。
数値型を扱うのに必要となる演算には、加減乗除だけではなく、比較演算子や負号などもありますので、これらも備えています。ちなみに負号は減算でも代用 (0 - x) できるのでなくてもよいでしょうが、比較演算子は加減乗除でやりくりしたとしても、少なくともひとつの比較演算子は必要でしょう。
実際にこのサンプルどおり動いている例は こちら です(新しいウィンドウが開きます。なお都合によりサーバが動いていない時もあります)。
ActiveX Component のインストールが必要なので、実質的にサーバサイドでのみ使用でき、クライアントサイドでの使用は難しいかもしれません。
プロジェクトファイルおよびビルド済みの ActiveX Component のダウンロードは こちら です。
なお、コンパイルは Visual Basic Version 5.0 Control Creation Edition SP2 日本語版 (VB5CCE) を使いました。
VB5CCE は、コントロールしかビルドできないようなので、コントロール(.ctl)にメソッドを実装していますが、本来は通常の VB を用いて ActiveX DLL などとしてプロジェクトを作成したほうが望ましいでしょう。ただ、今回は状態を持たないし、処理が小さなコンポーネントなので、とくに問題はないようです。
コンパイルすると Deci.ocx というファイルができますので、これを C:\WINNT\System32 のフォルダにコピーしてから、
regsvr32 C:\WINNT\System32\Deci.ocx
のように登録します。
動作には VB バージョン 5 のランタイムライブラリが必要ですが、最近の動作環境ならば標準で備えていることがほとんどでしょう。
ちなみにこの ActiveX Component が返す Decimal 型の値を Application 変数や Session 変数に格納することもできると思います。もし何か問題がある場合は、VBScript 側で受け取った返り値を VBScript の CStr 関数などにより文字列型に変換してそれを Application や Session 変数に格納するという手で回避できるでしょう。
Option Explicit
Public Function pl(ByVal x As Variant) As Variant
pl = CDec(x)
End Function
Public Function mi(ByVal x As Variant) As Variant
mi = -CDec(x)
End Function
Public Function ad(ByVal a As Variant, ByVal b As Variant) As Variant
ad = CDec(a) + CDec(b)
End Function
Public Function su(ByVal a As Variant, ByVal b As Variant) As Variant
su = CDec(a) - CDec(b)
End Function
Public Function mu(ByVal a As Variant, ByVal b As Variant) As Variant
mu = CDec(a) * CDec(b)
End Function
Public Function di(ByVal a As Variant, ByVal b As Variant) As Variant
di = CDec(a) / CDec(b)
End Function
Public Function lt(ByVal a As Variant, ByVal b As Variant) As Boolean
lt = (CDec(a) < CDec(b))
End Function
Public Function le(ByVal a As Variant, ByVal b As Variant) As Boolean
le = (CDec(a) <= CDec(b))
End Function
Public Function gt(ByVal a As Variant, ByVal b As Variant) As Boolean
gt = (CDec(a) > CDec(b))
End Function
Public Function ge(ByVal a As Variant, ByVal b As Variant) As Boolean
ge = (CDec(a) >= CDec(b))
End Function
Public Function eq(ByVal a As Variant, ByVal b As Variant) As Boolean
eq = (CDec(a) = CDec(b))
End Function
Public Function ne(ByVal a As Variant, ByVal b As Variant) As Boolean
ne = (CDec(a) <> CDec(b))
End Function
<%
Option Explicit
Dim a
a = Request.Form("a")
If IsEmpty(a) Then
a = 0
End If
Dim b
b = Request.Form("b")
If IsEmpty(b) Then
b = 0
End If
Dim op
op = Request.Form("op") ' Operator
If IsEmpty(op) Then
op = "+"
End If
Dim d
Set d = Server.CreateObject("Deci.Mal")
Dim c
On Error Resume Next
If op = "plus" Then
c = d.pl(a)
ELseIf op = "minus" Then
c = d.mi(a)
ELseIf op = "+" Then
c = d.ad(a, b)
ELseIf op = "-" Then
c = d.su(a, b)
ELseIf op = "*" Then
c = d.mu(a, b)
ELseIf op = "/" Then
c = d.di(a, b)
ELseIf op = "<" Then
c = d.lt(a, b)
ELseIf op = "<=" Then
c = d.le(a, b)
ELseIf op = ">" Then
c = d.gt(a, b)
ELseIf op = ">=" Then
c = d.ge(a, b)
ELseIf op = "=" Then
c = d.eq(a, b)
ELseIf op = "<>" Then
c = d.ne(a, b)
ELse
c = "no operator"
End If
If Err.Number <> 0 Then
c = "Number = " & Err.Number & ", Description = " & Err.Description
End If
On Error GoTo 0
Set d = Nothing
%><HTML>
<HEAD>
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=Shift_JIS">
<TITLE>Decimal 型を使った演算</TITLE>
</HEAD>
<BODY>
Decimal 型を使った演算のサンプルです。a と b に桁数の多い値を入れて、operator を選択してから、演算ボタンを押してください。
<BR>演算結果またはエラーメッセージが c に表示されます。
<BR>plus(正号) と minus(負号) は a のみに作用し、b は使いません。
<BR>比較演算子(<, <=, >, >=, =, <>)の結果は 0(False) または -1(True) のいずれかのみです。
<HR>
<FORM METHOD=post>
a: <INPUT TYPE=text NAME=a SIZE=60 VALUE="<%=a%>">
<BR>
operator: <SELECT NAME="op">
<OPTION VALUE="plus" <%If op = "plus" Then%>SELECTED<%End If%>>plus(正号)
<OPTION VALUE="minus" <%If op = "minus" Then%>SELECTED<%End If%>>minus(負号)
<OPTION VALUE="+" <%If op = "+" Then%>SELECTED<%End If%>>+(加算)
<OPTION VALUE="-" <%If op = "-" Then%>SELECTED<%End If%>>-(減算)
<OPTION VALUE="*" <%If op = "*" Then%>SELECTED<%End If%>>*
<OPTION VALUE="/" <%If op = "/" Then%>SELECTED<%End If%>>/
<OPTION VALUE="<" <%If op = "<" Then%>SELECTED<%End If%>><
<OPTION VALUE="<=" <%If op = "<=" Then%>SELECTED<%End If%>><=
<OPTION VALUE=">" <%If op = ">" Then%>SELECTED<%End If%>>>
<OPTION VALUE=">=" <%If op = ">=" Then%>SELECTED<%End If%>>>=
<OPTION VALUE="=" <%If op = "=" Then%>SELECTED<%End If%>>=
<OPTION VALUE="<>" <%If op = "<>" Then%>SELECTED<%End If%>><>
</SELECT>
<BR>b: <INPUT TYPE=text NAME=b SIZE=60 VALUE="<%=b%>">
<BR>=
<BR>c: <INPUT TYPE=text NAME=c SIZE=60 VALUE="<%=c%>">
<BR><INPUT TYPE=submit VALUE="演算">
</FORM>
</BODY>
</HTML>