Tips - Visual Basic

【TOP】

インクリメントとデクリメント
プログラム中でループを構成するとき等に、カウンタをインクリメント、またはデクリメントすることが
しばしばあります。 C# や Java 言語では「++」、「--」演算子がありますが、残念ながらこの演算子は
Visual Basic には存在しません。
ここでは、「+=」、「-=」演算子と「System.Decimal.op_Increment(dec As System.Decimal)」演算子
「System.Decimal.op_Decrement(dec As System.Decimal)」演算子を使ってカウンタの増減をしてみます。
Private decIdx As Decimal = 0 'インクリメント① decIdx += 1 'デクリメント① decIdx -= 1 'インクリメント② decIdx = System.Decimal.op_Increment(decIdx) 'デクリメント② decIdx = System.Decimal.op_Decrement(decIdx) 'カウンタのデータ型がDecimalではなく '「Option Strict On」を記述して暗黙的な型変換を禁止しているとき '②の方法を使うには型変換が必要。 Private intIdx As Integer = 0 'インクリメント③ intIdx = CType(System.Decimal.op_Increment(CType(decIdx,Decimal)),Integer) 'デクリメント③ intIdx = CType(System.Decimal.op_Decrement(CType(decIdx,Decimal)),Integer)
【戻る】