你可能要考虑作出专门类。
比方说,你有你的基本LoginResponse
Public Class LoginResponse
Public Property TerminalID As String
Public Property ReaderID As String
Public Property TransRef As String
Public Property TransDateTime As String
Public Property Timeout As Integer
Public Property ResponseCode As String
' Note: no ResponseMsg here
Public Property Cryptogram As String
End Class
那么你就会有一个扩展响应等级继承你的基本LoginResponse
:
Public Class LoginResponseEx : Inherits LoginResponse
Public Property ResponseMsg As String
End Class
然后在任何你创建那些LoginResponse
对象,你只需创建一个合适的对象。
比方说,你有一个GetResponse()
程序,如:
Public Function GetResponse() As LoginResponse
Dim result As LoginResponse = Nothing
Dim code As Integer = GetSomeCode()
' ... get the other properties
' Say you have a const or something with the appropriate code: SPECIAL_CODE
If code = SPECIAL_CODE Then
Dim msg As String = GetSomeMessage()
result = New LoginResponseEx(..., code, msg, ...) ' have a special Response
Else
result = New LoginResponse(..., code, ...) ' have a normal Response
End If
Return result
End Function
检查你只是检查是否有ResponseCode
一个特殊值,并把对象respectivly响应最后,当。
'...
Dim resp as LoginResponse = GetResponse()
If resp.ResponseCode = SPECIAL_CODE Then
Dim respx as LoginResponseEx = CType(resp, LoginResponseEx)
Console.WriteLine("ResponseMessage was: " & respx.ResponseMsg
Else
Console.WriteLine("No ResponseMessage")
End If
'...
这样,你有你的基本LoginResponse
与特殊类ResponseMsg
隐藏ResponseLoginEx
注意,当你这样做,你应该想想如何实现虚拟课堂。例如这些字段可能必须声明为Protected
而不是Private
,但我相信你会做得很好。
这也适用于Serializable类,当然。
为什么不直接返回空字符串,当Responsecode!= 99并在文档中声明该事实? –
或者更改你的软件结构,这样你就可以拥有一个名为LoginResponseXYZ的类,它继承了LoginResponse类并公开了ResponseMsg成员。 – Mino
我知道这通过返回空字符串,但是这可能隐藏ResponseMsg当ResponseCode!= 99?谢谢 – user3051461