求编程指教!!!!!
题目为:创建一个窗体,接受员工信息,冰封装在Employee类的对象中,在调用employee类的Display方法输出。
下面是我写的程序,为什么输出来的都是空的。我是新手,求指教。
谢谢您
Public Class Form1
Public Class employee
Private namevalue As String
Private agevalue As Integer
Private Sexvalue As String
Private salvalue As Integer
Public Property name() As String
Get
Return namevalue
End Get
Set(ByVal value As String)
namevalue = value
End Set
End Property
Public Property age() As String
Get
Return agevalue
End Get
Set(ByVal value As String)
agevalue = value
End Set
End Property
Public Property Sex() As String
Get
Return Sexvalue
End Get
Set(ByVal value As String)
Sexvalue = value
End Set
End Property
Public Property Sal() As String
Get
Return salvalue
End Get
Set(ByVal value As String)
salvalue = value
End Set
End Property
Public Sub display()
Dim msg As String = "姓名:" & name & ControlChars.CrLf _
& "年龄:" & age & ControlChars.CrLf _
& "性别:" & Sex & ControlChars.CrLf _
& "薪资:" & Sal
MsgBox(msg)
End Sub
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strname As String = TextBox1.Text
Dim strAge As Integer = Convert.ToInt32(TextBox2.Text)
Dim strSex As String = "男"
Dim strSal As Integer = Convert.ToInt32(TextBox3.Text)
If RadioButton2.Checked Then
strSex = "女"
End If
Dim emp As employee = New employee(strname, strAge, strSex, strSal)
emp.display()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
RadioButton1.Checked = True
End Sub
End Class 编程
[解决办法]
Dim emp As employee = New employee(strname, strAge, strSex, strSal)
=>
age = strAge
sex = strSex
...
即可。
[解决办法]
Public Sub New(ByVal name As String, ByVal age As String, ByVal sex As String, ByVal sal As String)
Me.name = name
Me.age = age
Me.Sex = sex
Me.Sal = sal
End Sub
第一种,加构造
Public Sub New(ByVal name As String, ByVal age As Integer, ByVal sex As String, ByVal sal As Integer)
Me.namevalue = name
Me.agevalue = age
Me.Sexvalue = sex
Me.salvalue = sal
End Sub
Dim emp As employee = New employee(strname, strAge, strSex, strSal)改为
Dim emp As employee = New employee()
emp.name = strname
emp.age = strAge
emp.Sex = strSex
emp.Sal = strSal
emp.display()