2016-07-26 143 views
1

是否有可能在兩個不同的類中具有相同的變量定義。 我剛開始學習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 
    } 
+0

那麼最後一個不是vb,你在學什麼? – BugFinder

+0

它的vb編譯器,對不起,我沒有提到。 – Cesar

+0

好吧,但最後的代碼不是VB,其C#,如果你不想在C#中做它,那麼你不想在這裏的C#標記..也許這就是爲什麼你在最後一節掙扎,因爲它不是VB – BugFinder

回答

1

通常,您可以使用包含公共變量的基類來執行此操作。然後你的其他類繼承這個類併爲路徑屬性提供它們自己的實現:

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 
+0

這使得很多道理,謝謝!! ....所以如果我想在vb.net編譯器中使用UPSAddress類? Dim upsAddress as new UPSAddress(Address,City,State,Zipcode)will work? – Cesar

+0

是的,應該工作正常 –

+0

感謝您的幫助:) – Cesar

相關問題