473,414 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Interfaces and workarounds for differences in C# and VB

balabaster
797 Expert 512MB
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.Value

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:

Expand|Select|Wrap|Line Numbers
  1. Function GetValue(ByVal FreightBill As IFreightBill, ByVal ValuePath As String) As Double
  2.  
  3.   Dim AssemblyName As String = Reflection.Assembly.GetExecutingAssembly().GetName.Name
  4.  
  5.   Dim PathNodes As List(Of String) = ValuePath.Split(".").ToList
  6.   If PathNodes(0) <> AssemblyName Then PathNodes.Insert(0, AssemblyName)
  7.  
  8.   Dim CrntInstance As Object = FreightBill
  9.   For i As Integer = 2 To PathNodes.Count - 1
  10.     Dim CrntType As Type = CrntInstance.GetType()
  11.     Dim CrntProperty As String = PathNodes(i)
  12.     Dim dynProperty As Reflection.PropertyInfo = CrntType.GetProperty(CrntProperty)
  13.     Dim CrntValue = dynProperty.GetValue(CrntInstance, _
  14.                                    System.Reflection.BindingFlags.GetProperty, _
  15.                                    Nothing, _
  16.                                    Nothing, _
  17.                                    Nothing)
  18.     CrntInstance = CrntValue
  19.  
  20.   Next
  21.   Return CrntInstance
  22.  
  23. 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:

Expand|Select|Wrap|Line Numbers
  1. Public Property MyIncorrectlyNamedProperty() As String Implements IMyInterface.ActualPropertyName
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...
Sep 24 '08 #1
4 1236
balabaster
797 Expert 512MB
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:

Expand|Select|Wrap|Line Numbers
  1. Interface IPropertyInterface
  2.   Property MyValue() As String
  3. End Interface
  4.  
  5. Interface IInstanceInterface
  6.   Property MyProperty() As IPropertyInterface
  7. End Interface
  8.  
  9. Public Class MyProperty
  10.   Implements IMyProperty
  11.  
  12.   Private _MyValue As String
  13.   Public Property MyValue() As String
  14.     Get
  15.       Return _MyValue
  16.     End Get
  17.     Set (ByVal value As String)
  18.       _MyValue = value
  19.     End Set
  20.   End Property
  21. End Class
  22.  
  23. Public Class MyInstance
  24.   Implements IInstanceInterface
  25.  
  26.   Private _MyProperty As IPropertyInterface
  27.   Public Property MyProperty() As IPropertyInterface Implements IInstanceInterface.MyProperty
  28.     Get
  29.       Return _MyProperty
  30.     End Get
  31.     Set (ByVal value As IPropertyInterface)
  32.       _MyProperty = value
  33.     End Set
  34.   End Property
  35. End Class
So I can use the following:

Expand|Select|Wrap|Line Numbers
  1. Dim oInst As New MyInstance
  2. Dim oProp As New MyProperty
  3. oProp.MyValue = "Hello World"
  4. oInst.MyProperty = oProp
  5.  
  6. Dim OutStr As String = GetValue(oInst, "ObjectInstance.MyProperty.MyValue")
  7. Console.WriteLine(OutStr)
So my GetValue method (previously posted) would iterate over the instance without any issues...but if the class that implements IInstanceInterface had it's MyProperty property changed to MyIncorrectlyNamed property... I'm wondering if I could grab the property rather than by name, but by the interface it implements...
Sep 24 '08 #2
balabaster
797 Expert 512MB
Actually, I think I might just like the "rap them on the knuckles" approach...
Sep 24 '08 #3
Plater
7,872 Expert 4TB
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.
Sep 24 '08 #4
balabaster
797 Expert 512MB
I was going to suggest you use a mallet or something larger then a ruler.
That's not in the stationary budget :oP
Sep 24 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Philip Herlihy | last post by:
I spent the whole day yesterday trying to get a page to layout correctly in IE. I wanted two columns to maintain a given proportion (25%, 75%) on a page regardless of the browser width. Easy to...
1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
11
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
1
by: Fuzzy | last post by:
The thread on 'why NOT to use interface?' has provoked a question - but I'll start a new thread.... Is there any performance difference that anyone is aware of between using interfaces for...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
3
by: Steven Nagy | last post by:
XML serialising this class: public class Table { public List<IActionActions; public List<ColumnColumns; } I think the interface is the problem.
18
by: abcd | last post by:
Can someone tell me whats the exact advantage of Interfaces in C#. To me abstract classes serves the purpose and have all good things then what is special about interfaces...can someone tell me...
28
by: jmDesktop | last post by:
Studying OOP and noticed that Python does not have Interfaces. Is that correct? Is my schooling for nought on these OOP concepts if I use Python. Am I losing something if I don't use the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.