Interfaces and workarounds for differences in C# and VB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    Interfaces and workarounds for differences in C# and VB

    I've got a hierarchy of interfaces that I need passed into a method to return a value given a value path. For instance:

    Class.Property. SubProperty.Val ue

    I'm using reflection to iterate through the path grabbing the sub-properties down to the "leaf" property at which point the value is returned. The basic concept is this:

    Code:
    Function GetValue(ByVal FreightBill As IFreightBill, ByVal ValuePath As String) As Double
    
      Dim AssemblyName As String = Reflection.Assembly.GetExecutingAssembly().GetName.Name
    
      Dim PathNodes As List(Of String) = ValuePath.Split(".").ToList
      If PathNodes(0) <> AssemblyName Then PathNodes.Insert(0, AssemblyName)
    
      Dim CrntInstance As Object = FreightBill
      For i As Integer = 2 To PathNodes.Count - 1
        Dim CrntType As Type = CrntInstance.GetType()
        Dim CrntProperty As String = PathNodes(i)
        Dim dynProperty As Reflection.PropertyInfo = CrntType.GetProperty(CrntProperty)
        Dim CrntValue = dynProperty.GetValue(CrntInstance, _
                                       System.Reflection.BindingFlags.GetProperty, _
                                       Nothing, _
                                       Nothing, _
                                       Nothing)
        CrntInstance = CrntValue
    
      Next
      Return CrntInstance
    
    End Function
    Now...even if I replace some of my properties from static classes to interfaces, in C# this will still work as when you implement an interface in C# it must have the same name as the interface, so everything works as you would expect. But if you implement an interface in VB, you can change the name of a property because each property uses the Implements keyword to implement the correct property of the interface. For example:

    [Code=vb]Public Property MyIncorrectlyNa medProperty() As String Implements IMyInterface.Ac tualPropertyNam e[/Code]

    Whereas in C#, you must use the correct name...

    So my question I guess is in order to prevent possible problems with VB programmers calling instance properties by incorrect names, what can I do about this? Other than rapping them on the knuckles with a ruler and telling them not to do that...
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    I wondered if creating a dynamic instance of an object that implements the interface that the property represents would help...

    So if I am handling a property with some declaration to:

    Code:
    Interface IPropertyInterface
      Property MyValue() As String
    End Interface
    
    Interface IInstanceInterface
      Property MyProperty() As IPropertyInterface
    End Interface
    
    Public Class MyProperty
      Implements IMyProperty
    
      Private _MyValue As String
      Public Property MyValue() As String
        Get
          Return _MyValue
        End Get
        Set (ByVal value As String)
          _MyValue = value
        End Set
      End Property
    End Class
    
    Public Class MyInstance
      Implements IInstanceInterface
    
      Private _MyProperty As IPropertyInterface
      Public Property MyProperty() As IPropertyInterface Implements IInstanceInterface.MyProperty
        Get
          Return _MyProperty
        End Get
        Set (ByVal value As IPropertyInterface)
          _MyProperty = value
        End Set
      End Property
    End Class
    So I can use the following:

    Code:
    Dim oInst As New MyInstance
    Dim oProp As New MyProperty
    oProp.MyValue = "Hello World"
    oInst.MyProperty = oProp
    
    Dim OutStr As String = GetValue(oInst, "ObjectInstance.MyProperty.MyValue")
    Console.WriteLine(OutStr)
    So my GetValue method (previously posted) would iterate over the instance without any issues...but if the class that implements IInstanceInterf ace had it's MyProperty property changed to MyIncorrectlyNa med property... I'm wondering if I could grab the property rather than by name, but by the interface it implements...

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      Actually, I think I might just like the "rap them on the knuckles" approach...

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Originally posted by balabaster
        Actually, I think I might just like the "rap them on the knuckles" approach...
        I was going to suggest you use a mallet or something larger then a ruler.

        Comment

        • balabaster
          Recognized Expert Contributor
          • Mar 2007
          • 798

          #5
          Originally posted by Plater
          I was going to suggest you use a mallet or something larger then a ruler.
          That's not in the stationary budget :oP

          Comment

          Working...