ネタ元1→オノデラの研究日記 in わんくま:Excel.Workbooks.Open メソッドの引数の数が VB.NET と C# で違う?
ネタ元2→Mr.Tの場所:Optionalをつける時
火種はコレ↓
わたしはVisual Basic であっても Optional はなんかしっくりきません・・・。
Mr.Tさんがビクッと反応しちゃう感じらしいです。(´Д`;)ハァハァ
さて、私のコメントの真意ですが、78%くらいはただの慣れの問題で片付きます。ごめんなさい。Optional キーワードを指定した引数を持つメソッドを呼び出す側のコードが、自分にとって見慣れないんです。
Public Class Hoge
Public Sub New( _
Optional ByVal a As String = "", _
Optional ByVal b As String = "", _
Optional ByVal c As String = "", _
Optional ByVal d As String = "", _
Optional ByVal e As String = "")
End Sub
End Class
Public Class Program
Public Shared Sub Main()
Dim h1 As New Hoge()
Dim h2 As New Hoge(, )
Dim h3 As New Hoge(, , )
Dim h4 As New Hoge(, , , )
Dim h5 As New Hoge(, , , , )
End Sub
End Class
極端な話、こんな感じのコードが書かれていたときに
Dim h5 As New Hoge(, , , , )
を見て、即座にOptional キーワードだと気が付けない。一瞬思考がとまっちゃいますです。
Optional な引数というもの自体がイメージしにくい思考なんだろうと思います。Mr.Tさんの書かれている '絵を描く' メソッド↓
Public Sub 絵を描く(ByVal 筆 As 絵筆, Optional 上塗りフラグ As Boolean = False)
この '上塗りフラグ' がOptional である(にすべき?)理由が自分には見つけられません。
省略であること(できること)によるメリット、デメリットってなんなんでしょうね。