[VB.NET]For Eachで繰り返し処理|Dictionary・Exit For・Continue
![[VB.NET]For Eachで繰り返し処理|Dictionary・Exit For・Continue](/dotnet/column/wp-content/uploads/2020/09/pixta_69574121_M-960x320.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のソースコードを書いて、理解を深めてください。
Search キーワード検索
Popular 人気の記事
-
【VB.NET入門】DataGridViewの使い方まとめ
公開: 更新:
reccomended おすすめ記事
-
【.NETが統合】.NET 5の概要と今後のリリース予定
公開: 更新:
Categories 連載一覧
Tags タグ一覧
Jobs 新着案件
-
生産管理システムの単体テスト/東京都千代田区/【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万円東京都港区(品川駅)