' Public members, accessible from throughout declaration region.
Friend firstName As String
Public middleName As String
Public lastName As String
' Friend members, accessible from anywhere within the same assembly.
Friend employeeNumber As Integer
Friend workPhone As Long
' Private members, accessible only from within the structure itself.
Private homePhone As Long
Private level As Integer
Private salary As Double
Private bonus As Double
' Procedure member, which can access structure's private members.
Friend Sub calculateBonus(ByVal rate As Single)
bonus = salary * CDbl(rate)
End Sub
' Property member to return employee's eligibility.
Friend ReadOnly Property eligible() As Boolean
Get
Return level >= 25
End Get
End Property
' Event member, raised when business phone number has changed.
Public Event changedWorkPhone(ByVal newPhone As Long)
End Structure
其中 Public Event changedWorkPhone(ByVal newPhone As Long)这个事件如何触发呢?
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim emplo1 As New employee
‘emplo1.无法检索changedWorkPhone
End Sub
还有结构可以声明零个或多个非共享变量或非共享、非自定义的结构事件。 在结构中不能只包含常数、属性和过程,即使某些成员是非共享的。
上面说的意思是结构可以类一样空的,是吗?
有些网上说,StrucTure结构至少有一个私有变量或自定义事件,但是我实际测试中结构中只有一个属性或函数能够
正常运行的,没有任何私有变量可以正常运行的,
大家这上面两个问题具体提出看法。
7 个解决方案
#1
如
Public Structure employee
Public ReadOnly Property NewProperty() As String
Get
Return "巴西世界杯"
End Get
End Property
End Structure
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim emplo1 As New employee
'MsgBox(emplo1.NewProperty)
‘照样能够显示 “巴西世界杯”
End Sub
End Class
Public Structure employee
Public ReadOnly Property NewProperty() As String
Get
Return "巴西世界杯"
End Get
End Property
End Structure
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim emplo1 As New employee
'MsgBox(emplo1.NewProperty)
‘照样能够显示 “巴西世界杯”
End Sub
End Class
#2
你的这段代码在VB.net2008下无法通过编译。
操作如下:
打开VisualStudio2008-》新建VB.net的窗口工程-》在窗体上增加一个按钮-》双加该按钮打开代码窗口,用拷贝你的代码来替换代码。
会在employee下出现波浪线,错误信息是
エラー 1 Structure 'employee' には、少なくとも 1 つのインスタンス メンバ変数宣言またはイベント宣言が含まれていなければなりません。
#3
你的代码不全,根本没有触发事件的代码。事件触发是结构体内部应该做的。
#4
我的是vb.net2010的
#5
在VB语言规范9.0里结构成员一节还有这样的描述:
在11.0里这句话被删除。由于10.0到11.0没有重大的语言更改,可以认为这个改变是10.0时做出的。
因此在VS2010前结构是不能空的。
在11.0里这句话被删除。由于10.0到11.0没有重大的语言更改,可以认为这个改变是10.0时做出的。
因此在VS2010前结构是不能空的。
#6
只剩下一个问题,结构体内的
Public Event changedWorkPhone(ByVal newPhone As Long)这个事件如何响应呢。
这个意思是什么
Public Event changedWorkPhone(ByVal newPhone As Long)这个事件如何响应呢。
这个意思是什么
#7
在结构体内部可以这样触发:
Private _phoneNumber As Long = 0
Public Property phoneNumber As Long
Get
Return _phoneNumber
End Get
Set(value As Long)
Dim oldValue = _phoneNumber
_phoneNumber = value
If oldValue <> _phoneNumber Then
RaiseEvent changedWorkPhone(oldValue) '参数由你决定
End If
End Set
End Property
在结构体内部可以这样响应:
Private Sub employee_changedWorkPhone(newPhone As Long) Handles Me.changedWorkPhone
End Sub
在结构体外部可以这样响应:
Private WithEvents emplo1 As employee
Private Sub emplo1_changedWorkPhone(newPhone As Long) Handles emplo1.changedWorkPhone
End Sub
Private _phoneNumber As Long = 0
Public Property phoneNumber As Long
Get
Return _phoneNumber
End Get
Set(value As Long)
Dim oldValue = _phoneNumber
_phoneNumber = value
If oldValue <> _phoneNumber Then
RaiseEvent changedWorkPhone(oldValue) '参数由你决定
End If
End Set
End Property
在结构体内部可以这样响应:
Private Sub employee_changedWorkPhone(newPhone As Long) Handles Me.changedWorkPhone
End Sub
在结构体外部可以这样响应:
Private WithEvents emplo1 As employee
Private Sub emplo1_changedWorkPhone(newPhone As Long) Handles emplo1.changedWorkPhone
End Sub
#1
如
Public Structure employee
Public ReadOnly Property NewProperty() As String
Get
Return "巴西世界杯"
End Get
End Property
End Structure
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim emplo1 As New employee
'MsgBox(emplo1.NewProperty)
‘照样能够显示 “巴西世界杯”
End Sub
End Class
Public Structure employee
Public ReadOnly Property NewProperty() As String
Get
Return "巴西世界杯"
End Get
End Property
End Structure
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim emplo1 As New employee
'MsgBox(emplo1.NewProperty)
‘照样能够显示 “巴西世界杯”
End Sub
End Class
#2
你的这段代码在VB.net2008下无法通过编译。
操作如下:
打开VisualStudio2008-》新建VB.net的窗口工程-》在窗体上增加一个按钮-》双加该按钮打开代码窗口,用拷贝你的代码来替换代码。
会在employee下出现波浪线,错误信息是
エラー 1 Structure 'employee' には、少なくとも 1 つのインスタンス メンバ変数宣言またはイベント宣言が含まれていなければなりません。
#3
你的代码不全,根本没有触发事件的代码。事件触发是结构体内部应该做的。
#4
我的是vb.net2010的
#5
在VB语言规范9.0里结构成员一节还有这样的描述:
在11.0里这句话被删除。由于10.0到11.0没有重大的语言更改,可以认为这个改变是10.0时做出的。
因此在VS2010前结构是不能空的。
在11.0里这句话被删除。由于10.0到11.0没有重大的语言更改,可以认为这个改变是10.0时做出的。
因此在VS2010前结构是不能空的。
#6
只剩下一个问题,结构体内的
Public Event changedWorkPhone(ByVal newPhone As Long)这个事件如何响应呢。
这个意思是什么
Public Event changedWorkPhone(ByVal newPhone As Long)这个事件如何响应呢。
这个意思是什么
#7
在结构体内部可以这样触发:
Private _phoneNumber As Long = 0
Public Property phoneNumber As Long
Get
Return _phoneNumber
End Get
Set(value As Long)
Dim oldValue = _phoneNumber
_phoneNumber = value
If oldValue <> _phoneNumber Then
RaiseEvent changedWorkPhone(oldValue) '参数由你决定
End If
End Set
End Property
在结构体内部可以这样响应:
Private Sub employee_changedWorkPhone(newPhone As Long) Handles Me.changedWorkPhone
End Sub
在结构体外部可以这样响应:
Private WithEvents emplo1 As employee
Private Sub emplo1_changedWorkPhone(newPhone As Long) Handles emplo1.changedWorkPhone
End Sub
Private _phoneNumber As Long = 0
Public Property phoneNumber As Long
Get
Return _phoneNumber
End Get
Set(value As Long)
Dim oldValue = _phoneNumber
_phoneNumber = value
If oldValue <> _phoneNumber Then
RaiseEvent changedWorkPhone(oldValue) '参数由你决定
End If
End Set
End Property
在结构体内部可以这样响应:
Private Sub employee_changedWorkPhone(newPhone As Long) Handles Me.changedWorkPhone
End Sub
在结构体外部可以这样响应:
Private WithEvents emplo1 As employee
Private Sub emplo1_changedWorkPhone(newPhone As Long) Handles emplo1.changedWorkPhone
End Sub