標準モジュールの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」メソッドを置いた。

本来、このようなハンガリアン的な命名は嫌いなんだが、今回はPropertyFunctionの比較のためなのでガマン。

使ってみる

新たに標準モジュールを挿入し、別モジュールから使用を試みる。

スト2 標準モジュール
Option Explicit

Private Sub testProvokeModule()
  Call MsgBox(PropMessage(pmtAtype))
  Call MsgBox(FuncMessage(pmtBtype))
End Sub

ちなみに、入力中の様子は

f:id:akashi_keirin:20190718074327g:plain

このとおり。

イヤーな予感……。

これ、もしかしてuniqueなProperty名なら、モジュールを指定しなくても呼び出せちゃうのか……?

実行

スト2を実行してみると……。

f:id:akashi_keirin:20190718074346g:plain

ああ播磨灘

フツーに呼び出せてんじゃん……。

おわりに

ますますPropertyとFunctionの違いがわからなくなってしまったじゃねえか。

全VBAerのみなさんへ。

思わせぶりなタイトルで書いてしまってすみません。

真摯に反省してま~す。