-1

是否可以创建一个类,在其中我提供一个字符串以公开属性和方法,类似于:

Dim PayCheck as Double = 0
With Employee("Bob")
    .Age =  30
    .Position = Engineer
    .Department = Operations
    .Company = SPP
    .Wage = 30
    .HoursWorked = 45
    PayCheck = .CalculatePayCheck
End With
4

2 回答 2

2

您可以将 Dictionary(Of Employee) 包装到一个类中,并在CurrentEmployee向其提供字符串时从字典中设置一个属性。

然后可以检索关于CurrentEmployee. 但是您必须复制员工类型中的每个属性并从 包装适当的属性CurrentEmployee,或者可能使用反射从 中按名称获取正确的属性CurrentEmployee

这有帮助吗?

于 2013-02-27T02:37:13.927 回答
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 循环或 do/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
于 2013-02-27T18:01:19.483 回答