tags:

views:

132

answers:

4

I just came across an interesting scenario. I have a class in C#:

public class Test
{
  public int A;
  public int a;
}

As C# is case sensitive, this will treat the two variables A and a as distinct. I want to inherit the above class in my VB code, which is not case sensitive. How will the VB code access the two distinct variables A and a?

Any help is appreciated.

+6  A: 

The common language specification (CLS) which ensures cross language compatibility tells you not to declare two public members that are only different in case. Such a code won't be CLS compliant.

If you can't change the code of the library, you can use reflection API to manually select the field you want:

obj.GetType().GetField("a").GetValue(obj)
obj.GetType().GetField("A").GetValue(obj)
Mehrdad Afshari
A: 

If you intend for your code to be cross functional between languages, you will need to follow a naming standard that will work for both languages.

I personally find it very complicated to have two public members that have the same name, just different capitalization.

Mitchel Sellers
+1  A: 

for that you need to use reflection functionality

I saw how somewhere, I will try to find it

here is the link to how

in this solution above, it's about function, for a variable I think this would would be ok

at least you get both now :-)

Fredou
+5  A: 

Like Mehrdad said it is not CLS compliant to declare two public members that are only different in case

And if you want visual studio to help you write CLS complaint code that can be used from any other .NET languages , just write

[assembly: System.CLSCompliant(true)]

in your AssemblyInfo.cs file, if you did something wrong after writing this line, visual studio will not be happy :)
EDIT: or AssemblyInfo.vb if you are using VB.NET, thanks Lucas

Ahmed Mozaly
actually it's AssemblyInfo.cs for C# files, AssemblyInfo.vb for VB files, etc
Lucas