Collaboration between C# and VB.Net

G

Guest

Hi community,

Based on the promise that classes developed in C# and compiled into a *.dll
can be used from a VB.Net project I tried the following:

I need to develop a class PeriodDate in which I hold an integer representing
a PeriodDate in the format 200410 (where 2004 is the year and 10 is the
month). It happens that I have to make comparisons and would like to use
following VB code

Dim x as PeriodDate = new PeriodDate(200410)
Dim y as PeriodDate = new PeriodDate(200409)
If y < x then …

As far as I know operators cannot be implemented in VB.Net classes, however,
it can be done in C#.

Logically I created a C# project (class library) created a class PeriodDate
and implemented two operators

public static bool operator >(PeriodDate a, PeriodDate b) {
if ( a.PeriodNumber > b.PeriodNumber ) return true;
return false;
}

public static bool operator <(PeriodDate a, PeriodDate b) {
if ( a.PeriodNumber < b.PeriodNumber ) return true;
return false;
}

I compiled the dll, made the appropriate references in the VB.Net project
and used it in VB.Net as described above. This causes the IDE / compiler to
through the following error message:
“Operator ‘<’ not defined for types …â€

Am I overseeing something or is this an illegal design concept?

Any help is appreciated.

Regards,
Christian
 
G

Guest

Hi,

From what I understand, .NET has a thing called the Common Language
Specification, which is the "bare" minimum any language needs to implement
when working with the .NET framework. Operator overloading is NOT part of
this specification, and VB.NET cannot handle it..

The common approach is to have your class implement IComparable. This has a
method CompareTo which returns an int.

Basically, make sure that you stick within the CLS and you will be fine
operating between different languages.

I hope this helps even a little.
 
D

David Anton

In VB.NET 2003, you must access the < operator via "op_LessThan"
for all but 'core' data type cases.

(ditto for op_GreaterThan, op_Equality, etc.)

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top