Top Index
|
![]() ![]() ![]() |
| 01 | Public Class Form1 | |
| 02 | ||
| 03 | Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click | |
| 04 | Dim aryA As New List(Of Integer) | |
| 05 | For i As Integer = 0 To 9 | |
| 06 | aryA.Add(i) | |
| 07 | Next | |
| 08 | Dim aryB As New List(Of String) | |
| 09 | '以下のように書くと 型変換できません とエラーになってしまう | |
| 10 | 'aryB = aryA | |
| 11 | aryB = Me.ListTypeConvert(Of String)(aryA) | |
| 12 | End Sub | |
| 13 | ||
| 14 | Public Function ListTypeConvert(Of T)(ByVal arys As ICollection) As List(Of T) | |
| 15 | Dim lst As New List(Of T) | |
| 16 | For Each obj As Object In arys | |
| 17 | lst.Add(CType(obj, T)) | |
| 18 | Next | |
| 19 | Return lst | |
| 20 | End Function | |
| 21 | ||
| 22 | End Class |