Using C# written Class in VB.NET project - Case Problem

  • Thread starter Thread starter Joerg Krause
  • Start date Start date
J

Joerg Krause

Hi,

I've a component written in C#, and I use it in VB.NET project.
The class comes with these members:

MyClass.onClick ==> This is a property of type string
MyClass.OnClick ==> This is an event

In C# I can use this as expected.

Now I have a VB.NET project and Intellisense cannot recognize the
OnClick event because it sees the onClick property instead with
2 overloads.

I want to do:

AddHandler MyClass.OnClick, AddressOf MyHandler
^^^^^^^

This fails, because VB.NET reads a property instead of the event
and the editor makes the OnClick (upper case O) into onClick
(lower case o).

Has anybody any solution in VB.NET?

Thank you for any idea...

Jorge
 
G'Day,

The best way is to change the property name or the event name. It is not a
good practice anyway IMHO.
 
If you have the C# code, rename the Property and Event to something a bit
more descriptive. AFAIK, Identifiers Beginning with "On" are used to
identify protected methods used to raise the event of the same name minus
the "On" (e.g. Protected "OnClick" method raises the "Click" event).

If you are stuck with the names, maybe you could inherit from the class and
provide a new event with a different name that is raised when the base class
raises the OnClick event. Just remember that this way is a Fudge IMHO.

Hth,

Trev.
 
Joerg Krause said:
Hi,

I've a component written in C#, and I use it in VB.NET project.
The class comes with these members:

MyClass.onClick ==> This is a property of type string
MyClass.OnClick ==> This is an event

In C# I can use this as expected.

Don't use C#. It is case sensitive.
 
* (e-mail address removed) (Joerg Krause) scripsit:
I've a component written in C#, and I use it in VB.NET project.
The class comes with these members:

MyClass.onClick ==> This is a property of type string
MyClass.OnClick ==> This is an event

That's very bad design in the C# class. If you have the source code,
change it. If you don't have it, ask the author of the component to
change it.
 
Think VB.NET is case insensitive so it can't distinguish onClick from
OnClick or indeed ONCLICK or onclick.
You probably have to change the name of the property to something else?
There might be a workaround I don't know of.
 
Just to add: if you've written the c# component and plan on using it from
other languages, you should try adding the attribute
[assembly:CLSCompliant(true)]

If this is added, the compiler will verify CLS Compliance - it would yell at
you for having multiple public exports from a type with names that differ
only by case. You may find other issues this way as well.

Note that not all assemblies can use this, as it is quite restrictive - but
it does help for multiple-language projects.

-Philip Rieck
 
Thank you for the answers.

ClsCompliant is not possible, the project contains a lot of COM Interop
stuff which is in fact not CLS compliant.

Changing the sources is obviously the best way. My questions was "what
can I do if I have the object form only?".

-Joerg
www.joergkrause.de
ASP.NET/.NET-WinForm Softwaredevelopment
 
* Joerg Krause said:
Changing the sources is obviously the best way. My questions was "what
can I do if I have the object form only?".

You can use reflection to call the method/..., but that's not very
comfortable.

\\\
Imports System.Reflection

Public Class Main
Public Shared Sub Main()
Dim sc As New SampleClass()
Dim obj As Object = sc ' ;-)))
Dim mi As MethodInfo = obj.GetType.GetMethod("SampleMethod")
mi.Invoke(obj, Nothing)
End Sub

Public Class SampleClass
Public Sub SampleMethod()
MsgBox("Hello World!")
End Sub
End Class
End Class
///

Keywords: invoke, call, method, reflection.
 
Back
Top