標準モジュールのPropertyとは
標準モジュールのPropertyとは
標準モジュールにもPropertyを生やすことができる。
では、標準モジュールのPropertyとFunctionは何が違うのだろうか。
Fucntionならば、同名のPublic
なものが他のモジュールにない限りはメソッド名だけで呼び出せる。Propertyというぐらいだから、[モジュール名].Property名
の形でしか呼び出せない、とかなら便利なんだが。
比較
標準モジュールを用意し、オブジェクト名をProvoke
にする。
ユーザーをいらだたせる機能をあつめたモジュールにするのだ。
リスト1 標準モジュール Provoke
Option Explicit Public Enum ProvokeMessageType pmtAtype pmtBtype pmtCtype End Enum Public Property Get PropMessage( _ ByVal msgType As ProvokeMessageType) As String Dim ret As String ret = getMessage(msgType) PropMessage = ret End Property Public Function FuncMessage( _ ByVal msgType As ProvokeMessageType) As String Dim ret As String ret = getMessage(msgType) FuncMessage = ret End Function Private Function getMessage( _ ByVal msgType As ProvokeMessageType) As String Dim ret As String Select Case msgType Case pmtAtype: ret = "( ´_ゝ`)フーン" Case pmtBtype: ret = "( ´,_ゝ`)プッ" Case pmtCtype: ret = "(゚Д゚)ハァ?" Case Else: ret = "ち~んw" End Select getMessage = ret End Function
まったく同じ処理内容の「PropMessage
」プロパティと、「FuncMessage
」メソッドを置いた。
本来、このようなハンガリアン的な命名は嫌いなんだが、今回はProperty
とFunction
の比較のためなのでガマン。
使ってみる
新たに標準モジュールを挿入し、別モジュールから使用を試みる。
リスト2 標準モジュール
Option Explicit Private Sub testProvokeModule() Call MsgBox(PropMessage(pmtAtype)) Call MsgBox(FuncMessage(pmtBtype)) End Sub
ちなみに、入力中の様子は
このとおり。
イヤーな予感……。
これ、もしかしてuniqueなProperty名なら、モジュールを指定しなくても呼び出せちゃうのか……?
実行
リスト2を実行してみると……。
フツーに呼び出せてんじゃん……。
おわりに
ますますPropertyとFunctionの違いがわからなくなってしまったじゃねえか。
全VBAerのみなさんへ。
思わせぶりなタイトルで書いてしまってすみません。
真摯に反省してま~す。