后期绑定和严格的选项 [英] Late binding and Option Strict

查看:71
本文介绍了后期绑定和严格的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在后期绑定时遇到了这个问题:我正在创建一个购物清单应用程序.我有一个名为Item的类,该类存储杂货清单上某个项目的namepricequantitydescription.

I have this problem with late binding: I am creating a grocery list application. I have a class named Item which stores the name, price, quantity, and description of an item on the grocery list.

我有一个名为ListCollection的模块,该模块定义了Item对象的Collection.我创建了一个Edit表单,该表单将自动显示当前选择的ListCollection项属性,但是每当我尝试填充文本框时,它都会告诉我Option Strict不允许后期绑定.

I have a module named ListCollection which defines a Collection of Item objects. I have created an Edit form which will automatically display the currently selected ListCollection item properties, but whenever I attempt to fill the text boxes, it tells me that Option Strict disallows late binding.

我可以采取简单的方法并禁用Option Strict,但是我更想弄清楚问题出在哪里,所以我知道以供将来参考.

I COULD take the easy route and disable Option Strict, but I'd prefer to figure out what the problem is so I know for future reference.

我将在此处粘贴相关代码. (最新的绑定错误在EditItem.vb中.)

I shall paste pertinent code here. (Late binding error is in EditItem.vb.)

Item.vb代码:

Item.vb code:

' 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

ListCollection.vb代码:

ListCollection.vb code:

' 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代码:

EditItem.vb code:

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!

我尝试声明一个String变量并为其分配Item属性,并且我还尝试直接从List项中获取值(不使用Get函数),而没有一个这些有所作为.

I have tried declaring a String variable and assigning the Item property to that, and I have also tried grabbing the value directly from the List item (not using the Get function), and neither of these made a difference.

如何解决此问题?

推荐答案

您必须将项目从对象"转换为您的类型("EditItem").

You must cast the item from "Object" to your type ("EditItem").

http://www.codeproject.com/KB/dotnet/CheatSheetCastingNET.aspx

Private Sub EditItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ' getting the selected item
    Dim selectedItem As Object = ListCollection.listCollection(g_selectedItem)

    ' casting the selected item to required type
    Dim editItem As EditItem = CType(selectedItem, EditItem)

    ' setting value to the textbox
    txtName.Text = editItem.Name

多年来我没有在VB.NET中编写任何代码,我希望一切都很好.

I didn't code anything in VB.NET for years, I hope it is all right.

这篇关于后期绑定和严格的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆