[VB.NET]For Eachで繰り返し処理|Dictionary・Exit For・Continue
![[VB.NET]For Eachで繰り返し処理|Dictionary・Exit For・Continue](https://www.fenet.jp/dotnet/column/wp-content/uploads/2020/09/pixta_69574121_M-1024x717.jpg)
[VB.NET]For Eachで繰り返し処理してみよう!
今回は、VB.NETでのFor Eachを使った繰り返し処理について説明します。For Eachを使えば、配列やListおよびDictionaryなどのコレクションについて、繰り返し処理ができます。また、繰り返し処理を中断する方法やスキップする方法を紹介します。
VB.NETでの繰り返し処理に興味のある方はぜひご覧ください。
配列
VB.NETでの配列の繰り返し処理方法を紹介します。実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 |
public class compiler shared function Main as integer ' 配列の宣言 Dim myArray() As String = {"red", "green", "blue", "yellow", "white"} ' For Eachで配列の繰り返し処理 For Each item As String In myArray Console.WriteLine("item:" + item + ", length:" + item.Length.ToString()) Next return 0 End function end class |
実行結果は以下のようになります。
1 2 3 4 5 |
item:red, length:3 item:green, length:5 item:blue, length:4 item:yellow, length:6 item:white, length:5 |
List
VB.NETでのListの繰り返し処理方法を紹介します。実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Imports System Imports System.Collections.Generic public class compiler shared function Main as integer ' Listの初期化 Dim myList As New List(Of String)(New String() {"red", "green", "blue", "yellow", "white"}) ' For EachでListの繰り返し処理 For Each item As String In myList Console.WriteLine("item:" + item + ", length:" + item.Length.ToString()) Next return 0 End function end class |
実行結果は以下のようになります。
1 2 3 4 5 |
item:red, length:3 item:green, length:5 item:blue, length:4 item:yellow, length:6 item:white, length:5 |
Dictionary
VB.NETでのDictionaryの繰り返し処理方法を紹介します。実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Imports System Imports System.Collections.Generic public class compiler shared function Main as integer ' Dictionaryの宣言 Dim myDict As New Dictionary(Of String, String)() ' Dictionaryの値追加 myDict.Add("red", "あか") myDict.Add("green", "みどり") myDict.Add("blue", "あお") myDict.Add("yellow", "きいろ") myDict.Add("white", "しろ") ' For EachでDictionaryの繰り返し処理 For Each kvp As KeyValuePair(Of String, String) In myDict Console.WriteLine("{0} : {1}", kvp.Key, kvp.Value) Next return 0 End function end class |
実行結果は以下のようになります。
1 2 3 4 5 |
red : あか green : みどり blue : あお yellow : きいろ white : しろ |
Exit For
VB.NETでFor Eachの繰り返し処理を抜ける方法を紹介します。実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class compiler shared function Main as integer ' 配列の宣言 Dim myArray() As String = {"red", "green", "blue", "yellow", "white"} ' For Eachで配列の繰り返し処理 For Each item As String In myArray ' itemがblueの場合、繰り返し処理を抜ける If item = "blue" Then Exit For End If Console.WriteLine("item:" + item + ", length:" + item.Length.ToString()) Next return 0 End function end class |
実行結果は以下のようになります。
1 2 |
item:red, length:3 item:green, length:5 |
Continue
VB.NETでFor Eachの繰り返し処理をスキップする方法を紹介します。実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class compiler shared function Main as integer ' 配列の宣言 Dim myArray() As String = {"red", "green", "blue", "yellow", "white"} ' For Eachで配列の繰り返し処理 For Each item As String In myArray ' itemがblueの場合、繰り返し処理をスキップする If item = "blue" Then Continue For End If Console.WriteLine("item:" + item + ", length:" + item.Length.ToString()) Next return 0 End function end class |
実行結果は以下のようになります。
1 2 3 4 |
item:red, length:3 item:green, length:5 item:yellow, length:6 item:white, length:5 |
逆順
VB.NETでFor Eachを逆順に処理する方法を紹介します。逆順ソートしてから繰り返し処理します。実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Imports System Imports System.Collections.Generic public class compiler shared function Main as integer ' Listの初期化 Dim myList As New List(Of String)(New String() {"red", "green", "blue", "yellow", "white"}) ' 逆順ソート myList.Reverse() ' For EachでListの繰り返し処理 For Each item As String In myList Console.WriteLine("item:" + item + ", length:" + item.Length.ToString()) Next return 0 End function end class |
実行結果は以下のようになります。
1 2 3 4 5 |
item:white, length:5 item:yellow, length:6 item:blue, length:4 item:green, length:5 item:red, length:3 |
まとめ
いかがでしたでしょうか。今回は、VB.NETでのFor Eachを使った繰り返し処理について説明しました。For Eachを使えば、配列やListおよびDictionaryなどのコレクションについて、繰り返し処理ができます。また、繰り返し処理を中断する方法やスキップする方法を紹介しました。
ぜひご自身でVB.NETのソースコードを書いて、理解を深めてください。
FEnet.NETナビ・.NETコラムは株式会社オープンアップシステムが運営しています。
株式会社オープンアップシステムはこんな会社です
秋葉原オフィスには株式会社オープンアップシステムをはじめグループのIT企業が集結!
数多くのエンジニアが集まります。

-
スマホアプリから業務系システムまで
スマホアプリから業務系システムまで開発案件多数。システムエンジニア・プログラマーとしての多彩なキャリアパスがあります。
-
充実した研修制度
毎年、IT技術のトレンドや社員の要望に合わせて、カリキュラムを刷新し展開しています。社内講師の丁寧なサポートを受けながら、自分のペースで学ぶことができます。
-
資格取得を応援
スキルアップしたい社員を応援するために資格取得一時金制度を設けています。受験料(実費)と合わせて資格レベルに合わせた最大10万円の一時金も支給しています。
-
東証プライム上場企業グループ
オープンアップシステムは東証プライム上場「株式会社オープンアップグループ」のグループ企業です。
安定した経営基盤とグループ間のスムーズな連携でコロナ禍でも安定した雇用を実現させています。
株式会社オープンアップシステムに興味を持った方へ
株式会社オープンアップシステムでは、開発系エンジニア・プログラマを募集しています。
年収をアップしたい!スキルアップしたい!大手の上流案件にチャレンジしたい!
まずは話だけでも聞いてみたい場合もOK。お気軽にご登録ください。


VB.NET新着案件New Job
生産管理システムの単体テスト/東京都千代田区/【WEB面談可】/在宅勤務
月給25万~25万円東京都千代田区(東京駅)鉄鋼関連・グループ会社システム支援/Oracle/東京都新宿区/【WEB面談可】/在宅勤務
月給26万~26万円東京都新宿区(新宿駅)資産運用会社向け残高管理システム運用保守/SQLServer/東京都中央区/【WEB面談可】
月給50万~60万円東京都中央区(銀座駅)資産運用会社向け残高管理システム開発のテスター/SQLServer/東京都中央区/【WEB面談可】
月給25万~35万円東京都中央区(銀座駅)Web受注システム運用保守/VB.NET/東京都港区/【WEB面談可】
月給50万~60万円東京都港区(品川駅)Web受注システム開発のテスター/VB.NET/東京都港区/【WEB面談可】
月給25万~35万円東京都港区(品川駅)