0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VBのブロック切り分けはこれでいける(のか?)

Posted at

VBで処理をブロックで切り分けたくなった

お仕事で1万光年ぶりにがっつりVBを読み書きすることになりました。1
Web界にいることが多くお仕事でここまでVB使うのはほぼほぼ初めてで正直やりたくないまあやってみるかと引き継いだのはいいんですけど2
4,000行あるサブルーチンやいろいろな値をとっかえひっかえお盛んなファジーな名前のグローバル変数をかきわけて保守しているうちに、変数の生存範囲を狭めるためにブロックに切り分けたくなりました。
ちなみにVisual Basic 2010です。

C#だとこんな感じのやつですね。

{
    var hoge = something();
}

{
    var hoge = something();
}

でもVBではこの書き方は不可能です。
なのでこんな書き方をする?

If True Then
    Dim hoge = something()
End If

If True Then
    Dim hoge = something()
End If

必ずTrueになるIf文を使用して切り分ける。
確かに動きます。
でもIf文って条件の分岐に使うためのものでちょっとこの使い方はきもちわるいなって感じですよね。
というわけでIf以外の方法で書くことにしました。

Call _
Sub()
    Dim hoge = something()
End Sub

Call _
Sub()
    Dim hoge = something()
End Sub

動く。 OK。

動くけど

もしかしたらVB的にはすごいBADな書き方なのかもしれないのでVBに強い方いらっしゃいましたらご指摘ください。

  1. 光年は時間の単位ではない

  2. 作成者も前任者も既に居ないのを引継ぎとはいわない

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?