可以在vb.net的不同类中使用相同的变量定义
问题描述:
是否有可能在两个不同的类中具有相同的变量定义。 我刚开始学习vb.net,我试图实现地址验证API,到目前为止,UPS类适用于跟踪详细信息,但UPS地址不。两个类别之间唯一不同的是路径变量和构造函数的参数。可以在vb.net的不同类中使用相同的变量定义
Public Class UPS
Private accessKey As String = "0D0F94260Dxxxxx"
Private userName As String = "xxxxxx"
Private passWord As String = "xxxxx"
Private path As String = "https://www.ups.com/ups.app/xml/Track"
Public xml As XmlDocument = New XmlDocument()
public Sub New(trackNo As String)
xml = getUPSXMLbyTrackingNumber(trackNo)
End Sub
Public Function getIdentificationNumber() As String
Dim idNo As String = getNodeValue(xml, "TrackResponse/Shipment/ShipmentIdentificationNumber")
Return idNo.Trim
End Function
End Class
这是另一类。
Public Class UPSAddress
Private accessKey As String = "0D0F94260Dxxxxx"
Private userName As String = "xxxxxxx"
Private passWord As String = "xxxxxxx"
Private path as String = "https://wwwcie.ups.com/ups.app/xml/XAV"
Public xml As XmlDocument = New XmlDocument()
public Sub New(Address As String,City as String,State as String,Zipcode as String)
xml = getUPSXMLAddressValidation(Address,City,State,Zipcode)
End Sub
end class
此方法是否正确?这就是我称之为VB编译器中的类的方法
Dim trackNo As String = upsTrackNo.Value
Dim Address as String = upsAddress.value
Dim City as String = upsCity.value
Dim State as String = upsState.value
Dim Zipcode as String = upsZipcode.value
'This works'
Dim ups As New UPS(trackNo){
..some code
}
'Im not sure if this will work'
Dim upsAddress as new UPSAddress(Address,City,State,Zipcode){
...some code
}
答
通常,您可以使用包含公共变量的基类来执行此操作。然后你的其他类继承这个类并为路径属性提供它们自己的实现:
Public MustInherit Class UPSBase
Protected accessKey As String = "0D0F94260Dxxxxx"
Protected userName As String = "xxxxxx"
Protected passWord As String = "xxxxx"
Protected MustOverride ReadOnly Property Path As String
End Class
Public Class UPSAddress
Inherits UPSBase
Protected Overrides ReadOnly Property Path As String
Get
Return "https://wwwcie.ups.com/ups.app/xml/XAV"
End Get
End Property
End Class
Public Class UPS
Inherits UPSBase
Protected Overrides ReadOnly Property Path As String
Get
Return "https://www.ups.com/ups.app/xml/Track"
End Get
End Property
End Class
那么最后一个不是vb,你在学什么? – BugFinder
它的vb编译器,对不起,我没有提到。 – Cesar
好吧,但最后的代码不是VB,其C#,如果你不想在C#中做它,那么你不想在这里的C#标记..也许这就是为什么你在最后一节挣扎,因为它不是VB – BugFinder