बाहर स्थित कार्यान्वयन enumerated हैं वर्तमान में मैं अपने अंतर्निहित मॉडल के लिए इस कोड है:प्रकार के अंदर या क्लास कार्यान्वयन
Public Enum vehicleType
Car
Lorry
Bicycle
End Enum
Public Class TrafficSurveyA
' Declare the fields here.
Private fCars As Integer
Private fBicycles As Integer
Private fLorries As Integer
Public Sub New()
' An instance of TrafficSurveyA is created with all vehicle counts set to zero.
fCars = 0
fBicycles = 0
fLorries = 0
End Sub
Public Sub incrementCount(ByVal vehicle As vehicleType)
' Preconditions: none
' Postconditions: If vehicle is "Car", "Bicycle" or "Lorry" then 1 is added
' to the corresponding count. Otherwise nothing is done.
Select Case vehicle
Case vehicleType.Car : fCars = fCars + 1
Case vehicleType.Bicycle : fBicycles = fBicycles + 1
Case vehicleType.Lorry : fLorries = fLorries + 1
Case Else 'do nothing
End Select
End Sub
Public Function getCount(ByVal vehicle As vehicleType) As String
' Preconditions: none
' Postconditions: If vehicle is "Car", "Bicycle" or "Lorry", the string
' representation of the corresponding count is returned.
' Otherwise the empty string is returned.
Dim result As String
result = ""
Select Case vehicle
Case vehicleType.Car : result = Convert.ToString(fCars)
Case vehicleType.Bicycle : result = Convert.ToString(fBicycles)
Case vehicleType.Lorry : result = Convert.ToString(fLorries)
Case Else : result = ""
End Select
Return result
End Function
Public ReadOnly Property Vehicles() As String
' Preconditions: none
' Postconditions: The total number of vehicles recorded is returned.
Get
Return (fCars + fBicycles + fLorries).ToString()
End Get
End Property
End Class
ऐसा लगता है कि Enum
बस के रूप में आसानी से TrafficSurveyA
कक्षा इसलिए की तरह भीतर रखा जा सकता है .. ।
Public Class TrafficSurveyA
Enum vehicleType
Car
Lorry
Bicycle
End Enum
' Declare the fields here.
Private fCars As Integer
Private fBicycles As Integer
Private fLorries As Integer
Public Sub New()
' An instance of TrafficSurveyA is created with all vehicle counts set to zero.
fCars = 0
fBicycles = 0
fLorries = 0
End Sub
...
...
फर्क सिर्फ इतना है तो जीयूआई कोड मैं इस vehicleType.Lorry
के बजाय इस TrafficSurveyA.vehicleType.Lorry
उपयोग करने की आवश्यकता में हो रहा है।
दोनों ठीक चलते प्रतीत होते हैं लेकिन enum प्रकार के इन कार्यान्वयन में से एक गलत है?
+1 चीयर्स स्टीवन। मुझे एक और सरल 'एनम' प्रश्न मिला है [यहां] (http://stackoverflow.com/questions/14013227/converting-enum-type-to-a-sting-in-order-to-return-the-name) । – whytheq