是否可以创建一个类,我在其中提供一个字符串以显示属性和方法,类似于:
Dim PayCheck as Double = 0
With Employee("Bob")
.Age = 30
.Position = Engineer
.Department = Operations
.Company = SPP
.Wage = 30
.HoursWorked = 45
PayCheck = .CalculatePayCheck
End With
答案 0 :(得分:2)
您可以将Dictionary(Of Employee)包装到一个类中,并具有CurrentEmployee
属性,当您将字符串提供给字典时,该属性将从字典中设置。
然后可以检索与CurrentEmployee
相关的所有属性。但是您必须从雇员类型复制每个属性并从CurrentEmployee
包装适当的属性,或者使用反射从CurrentEmployee
按名称获取正确的属性。
这有帮助吗?
答案 1 :(得分:1)
是的你应该使用一类课程, 这被称为一个集合 所以你有一些东西......
Class Employee
Property Id (a unique numeric identifier)
Property Name
Property Age
Property Position
Property Department
Property Company
Property Wage
Property HoursWorked
Property PayCheck
Method CalculatePayCheck()
End Class
然后是您的员工集合
Class Employees
Property AllEmployees as generic.list(of Employee)
Property ThisEmployee(Id as Long) as Employee
Property ThisEmployee(Name as string) as Employee
Method AddEmployee(Name, Age, Position, Department, Company, Wage, HoursWorked, PayCheck)
Method RemoveEmployee(Name)
End Class
Id字段的想法是名称可能重复, 例如,可以使用两个“Steven Smith”
要访问员工,您可以按照
进行操作With ThisEmployee("Bob")
或
With ThisEmployee(6)
它将提取第七名员工,因为大多数集合都是零(从零开始)
参考上面的示例代码.... 是JoeB,因为它是一个通用的集合,你可以使用 for / next循环 或者一个for / each循环 或者做/ while循环
类似......
For Counter = 0 to Employees.Count -1
'print name using the EMPLOYEES collection object
Print AllEmployees(Counter).Name
'print name using the EMPLOYEE object (single employee using the Id property)
Print ThisEmployee(Counter).Name
Next
在某些情况下,您可能需要在使用之前安装对象... 像这样...
Dim TmpEmployee as Employee
For Counter = 0 to Employees.Count -1
'get employee from collection
TmpEmployee = AllEmployees(Counter)
'print name using the EMPLOYEE object
Print TmpEmployee.Name
TmpEmployee = nothing
Next