VB.NET入门 - 数组、枚举和结构体

时间:2022-10-20 14:01:23

  VB.NET入门 - 数组、枚举和结构体

   写复杂点的程序,就会觉得单个变量不好使用,用保存的数据太多了。还好VB.NET给我们提供了几种高级一点的用来存储数据的结构:数组,枚举与结构体。

  1.数组,多个数据类型相同的数据的集合,顺序排放。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
'5-1.vb
Class SimpleCnl
    Public Shared Sub Main()
    Dim i As Integer
        Dim allowedExtensions() As String = {".jpg", "bmp", "gif"}
        Dim fileExtension As String
        Dim isAllowed As Boolean = False
 
        fileExtension = System.Console.ReadLine()
        For i = 0 To allowedExtensions.Length-1
            If allowedExtensions(i) = fileExtension.ToLower Then
                isAllowed = True
                Exit For
            End If
        Next
        If isAllowed Then
            System.Console.WriteLine("允许上传")
        Else
            System.Console.WriteLine("文件类型不允许")
        End If
 
    End Sub
End Class

 

二维数组要复杂,但我们要搞明白的还是怎样定义,怎样初始化,怎样访问。有个问题要记住的Dim a(9),数组a的长度是10。

2.枚举

枚举规定了数据的取值范围,用更有意义的名称来代表一个值。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'5-2.vb
Class SimpleCnl
    Public Enum CRUD
        Create=100
        Retrive
        Update
        Delete
    End Enum
    Public Shared Sub Main()
        Dim commandName As CRUD
        System.Console.WriteLine("选择要执行的操作CRUB:Create,Retrive,Update,Delete")
        commandName = System.Enum.Parse(GetType(CRUD), System.Console.ReadLine())
        Select Case commandName
            Case CRUD.Create
                System.Console.WriteLine("增加记录")
            Case CRUD.Retrive
                System.Console.WriteLine("检索记录")
            Case CRUD.Update
                System.Console.WriteLine("更新记录")
            Case CRUD.Delete
                System.Console.WriteLine("删除记录")
        End Select
    End Sub
End Class

 

3.结构体,多个眼睛肿了怎么办变量的组合体,用来共同描述某一事物整体

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'5-3.vb 多项式相加
Class SimpleCnl
    Public Structure Term
        Dim coef As Integer '系数
        Dim exp As Integer  '指数
        Sub New(ByVal c As Integer, ByVal e As Integer)
            coef = c
            exp = e
        End Sub
    End Structure
    Public Shared Sub Main()
        Dim polynomialA() As Term = {New Term(1, 0), New Term(1, 1), New Term(1, 2)}
        Dim polynomialB() As Term = {New Term(1, 1),New Term(1,4)}
        Dim polynomialC(polynomialA.Length + polynomialB.Length) As Term
        Dim i, j, k As Integer
 
        i = 0 : j = 0 : k = 0
 
 
        Do While i < polynomialA.Length And j < polynomialB.Length
            If polynomialA(i).exp < polynomialB(j).exp Then
                polynomialC(k).exp = polynomialA(i).exp
                polynomialC(k).coef = polynomialA(i).coef
                i += 1
            ElseIf polynomialA(i).exp > polynomialB(j).exp Then
                polynomialC(k).exp = polynomialB(j).exp
                polynomialC(k).coef = polynomialB(j).coef
                j += 1
            Else
                polynomialC(k).exp = polynomialA(i).exp
                polynomialC(k).coef = polynomialA(i).coef + polynomialB(j).coef
                i += 1
                j += 1
            End If
            k += 1
        Loop
        If i = polynomialA.Length Then
            Do While j < polynomialB.Length
                polynomialC(k).exp = polynomialB(j).exp
                polynomialC(k).coef = polynomialB(j).coef
                j += 1
                k += 1
            Loop
        Else
            Do While i < polynomialA.Length
                polynomialC(k).exp = polynomialA(i).exp
                polynomialC(k).coef = polynomialA(i).coef
                i += 1
                k += 1
            Loop
        End If
        ReDim Preserve polynomialC(k - 1)
    '输出相加后得到的多项式
        k = 0
        Do
            System.Console.Write("{0}x^{1} + ", polynomialC(k).coef, polynomialC(k).exp)
            k += 1
        Loop Until k = polynomialC.Length - 1
        System.Console.Write("{0}x^{1} ", polynomialC(k).coef, polynomialC(k).exp)
    End Sub
End Class

 

这三种结构,实际使用的都很多,要想用的好,就得多练。