Vb.net 后期绑定和选项严格
我有一个关于后期绑定的问题:我正在创建一个杂货清单应用程序。我有一个名为Vb.net 后期绑定和选项严格,vb.net,binding,collections,early-binding,Vb.net,Binding,Collections,Early Binding,我有一个关于后期绑定的问题:我正在创建一个杂货清单应用程序。我有一个名为Item的类,它存储杂货清单上某个项目的名称,价格,数量,以及说明 我有一个名为ListCollection的模块,它定义了Item对象的Collection。我创建了一个Edit表单,该表单将自动显示当前选择的ListCollection项目属性,但每当我尝试填充文本框时,它都会告诉我选项Strict不允许后期绑定 我可以采取简单的方法禁用选项Strict,但我更愿意找出问题所在,以便我知道问题所在,以供将来参考 我将在这
Item
的类,它存储杂货清单上某个项目的名称
,价格
,数量
,以及说明
我有一个名为ListCollection
的模块,它定义了Item
对象的Collection
。我创建了一个Edit
表单,该表单将自动显示当前选择的ListCollection
项目属性,但每当我尝试填充文本框时,它都会告诉我选项Strict
不允许后期绑定
我可以采取简单的方法禁用选项Strict
,但我更愿意找出问题所在,以便我知道问题所在,以供将来参考
我将在这里粘贴相关代码。(后期绑定错误在EditItem.vb
中)
Item.vb代码:
' Member variables:
Private strName As String
' Constructor
Public Sub New()
strName = ""
' Name property procedure
Public Property Name() As String
Get
Return strName
End Get
Set(ByVal value As String)
strName = value
End Set
End Property
Private Sub EditItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Set the fields to the values of the currently selected ListCollection item.
txtName.Text = ListCollection.listCollection(g_selectedItem).Name.Get ' THIS LINE HAS THE ERROR!
ListCollection.vb代码:
' Create public variables.
Public g_selectedItem As Integer ' Stores the currently selected collection item.
' Create a collection to hold the information for each entry.
Public listCollection As New Collection
' Create a function to simplify adding an item to the collection.
Public Sub AddName(ByVal name As Item)
Try
listCollection.Add(name, name.Name)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
EditItem.vb代码:
' Member variables:
Private strName As String
' Constructor
Public Sub New()
strName = ""
' Name property procedure
Public Property Name() As String
Get
Return strName
End Get
Set(ByVal value As String)
strName = value
End Set
End Property
Private Sub EditItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Set the fields to the values of the currently selected ListCollection item.
txtName.Text = ListCollection.listCollection(g_selectedItem).Name.Get ' THIS LINE HAS THE ERROR!
我曾尝试声明一个字符串
变量,并将项
属性赋给该变量,我还尝试直接从列表
项中获取值(不使用Get
函数),但这两种方法都没有效果
我如何解决这个问题 必须将项目从“对象”强制转换为您的类型(“EditItem”) 编辑:
多年来,我没有在VB.NET中编写任何代码,我希望一切正常。我不完全确定我将在哪里(或如何)这样做。你能帮我澄清一下吗?我读了你的链接,但对我来说是希腊语。是的!这确实奏效了事实上,经过一番推敲,它变得更简单了——我没有将列表创建为对象并将其转换为项目,而是将其创建为项目并完全绕过转换。瞧!在这个过程中我学到了一些东西。非常感谢。我很乐意帮助你:)。这是OOP的一个基本方面。这叫做“装箱和拆箱”。我建议你研究一下。对于可能有同样问题的其他人来说,这更简化了:“txtName.Text=CType(ListCollection.ListCollection(g_selectedItem),Item).Name”现在我知道问题出在哪里了,我完全明白了。。。虽然我希望我能找到它在集合中作为对象声明所选项目的位置!