我正在使用Visual Basic 2008.我正在尝试使用button
将2列文本输入数据网格。
当我按下按钮时,变量和与变量相关的数据应该输入到2列datagrid中。
我使用的代码是:
Public Class Entering_the_rates
Public Class Datagrid
Private category As String
Public Property getcategory() As String
Get
Return category
End Get
Set(ByVal value As String)
category = value
End Set
End Property
Private price As Decimal
Public Property getprice() As Decimal
Get
Return price
End Get
Set(ByVal value As Decimal)
price = value
End Set
End Property
End Class
Private Sub Entering_the_rates_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
datagridadult() as new Datagrid (category, price)
End Sub
End Class
这种语法是对还是错。 我还需要一个关于如何调用我声明的数据网格的示例代码。请帮帮我
答案 0 :(得分:0)
你班上需要一个构造函数:
Public Class Datagrid
Private _category As String
Public Property getcategory() As String
Get
Return category
End Get
Set(ByVal value As String)
category = value
End Set
End Property
Private _price As Decimal
Public Property getprice() As Decimal
Get
Return price
End Get
Set(ByVal value As Decimal)
price = value
End Set
End Property
Public Sub New(byval category as string, byval price as decimal)
_category = category
_price = price
End Sub
End Class
属性后面的私有变量也应该以下划线开头,请参见此处:http://10rem.net/articles/net-naming-conventions-and-programming-standards---best-practices这使得在构造函数中更容易分配它们。您还可以使用自动实现的属性,这些属性会自动为您创建_后面的变量(尽管它们不会在intellisense中显示)。