注册 登录
编程论坛 VB.NET论坛

新手求助list(of)

zzzbtr 发布于 2018-05-27 13:31, 1977 次点击
编译没报错,但是执行到Pr.CityList.Add(Ci)就直接跳出SUB了,是怎么回事啊?
Sub aaa()是在主界面loaded事件里执行的,主界面上就三个TextBox

找到问题了,,,类里的list(of)类没有实例化 Private _CityList As New List(Of City)
现在的问题是 binding 关联第一个textbox是对的,关联第二个就跳出了。是怎么回事 = =????

好吧用笨办法实现了,每个binding都重用个实例。

现在问题是好像和书上c#例子 有些不一样 = =!? 我写的VB和书上的C#例子是一个东西么。。?
程序代码:
//相关类型
            class City
            {
                public string Name{get;set;}
            }
            class Province
            {
                public string Name{get;set;}
                public List<City>CityList{get;set;}
            }
            class Country
            {
                public string Name{get;set;}
                public List<Province>Province{get;set;}
            }
            //Binding
            List <Country>CountryList=new List<Country>{/*初始化*/};
            this.TextBox6.SetBinding(TextBox.TextProperty,new Binding("/Name"),{Source = CountryList});
            this.TextBox7.SetBinding(TextBox.TextProperty,new Binding("/ProvinceList.Name"),{Source = CountryList});
            this.TextBox8.SetBinding(TextBox.TextProperty,new Binding("/Province/CityList.Name"),{Source = CountryList});


程序代码:
Sub aaa()
        Dim Co As New Country
        Dim CountryList As New List(Of Country)
        Dim arr1 As Array = {"云南", "四川"}
        Dim arr2 As Array = {"昆明", "曲靖", "宣威"}
        Dim arr3 As Array = {"重庆", "成都", "绵阳"}
        Co.Name = "中国"
        For j As Integer = 0 To 1
            Dim Pr As New Province
            If j = 0 Then
                Pr.Name = arr1(0)
                For i As Integer = 0 To 2
                    Dim Ci As New City
                    Ci.Name = arr2(i)
                    Pr.CityList.Add(Ci)
                Next
            Else
                Pr.Name = arr1(j)
                For i As Integer = 0 To 2
                    Dim Ci As New City
                    Ci.Name = arr3(i)
                    Pr.CityList.Add(Ci)
                Next
            End If
            Co.ProvinceList.Add(Pr)
        Next
        CountryList.Add(Co)
        Erase arr1, arr2, arr3
        Dim binding As New Binding
        binding.Source = CountryList
        binding.Path = New PropertyPath("/Name")
        BindingOperations.SetBinding(TextBox6, TextBox.TextProperty, binding)

        Dim binding1 As New Binding
        binding1.Source = CountryList
        binding1.Path = New PropertyPath("/ProvinceList/Name")
        BindingOperations.SetBinding(TextBox7, TextBox.TextProperty, binding1)

        Dim binding2 As New Binding
        binding2.Source = CountryList
        binding2.Path = New PropertyPath("/ProvinceList/CityList/Name")
        BindingOperations.SetBinding(TextBox8, TextBox.TextProperty, binding2)

    End Sub

Public Class Country
    Private _Name As String
    Private _ProvinceList As New List(Of Province)
    Public Property Name As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property
    Public Property ProvinceList As List(Of Province)
        Get
            Return _ProvinceList
        End Get
        Set(ByVal value As List(Of Province))
            _ProvinceList = value
        End Set
    End Property
End Class

Public Class Province
    Private _Name As String
    Private _CityList As New List(Of City)
    Public Property Name As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property
    Public Property CityList As List(Of City)
        Get
            Return _CityList
        End Get
        Set(ByVal value As List(Of City))
            _CityList = value
        End Set
    End Property
End Class

Public Class City
    Private _Name As String
    Public Property Name As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property
End Class



[此贴子已经被作者于2018-5-27 14:30编辑过]

1 回复
#2
xyxcc1772018-06-05 20:15
给你个例子吧
程序代码:


 Private Structure myTity
        Dim Name As String
        Dim Height As Integer

    End Structure
    Private Sub myMain()
        Dim xList As List(Of myTity)
        xList = FirstSub()
        MsgBox(xList(0).Name & xList(0).Height)
        MsgBox(xList(1).Name & xList(1).Height)
    End Sub
    Private Function FirstSub() As List(Of myTity)
        Dim mList As New List(Of myTity)
        Dim tmpTity As New myTity
        With tmpTity
            .Name = "xxxx"
            .Height = 33
        End With
        mList.Add(tmpTity)
        tmpTity = New myTity
        With tmpTity
            .Name = "yyyyy"
            .Height = 55
        End With
        mList.Add(tmpTity)
        Return mList
    End Function

1