Re: GURUS: VB .NET alternatives to C# type conversion operators and operator overloading?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Tom (answerinthenewsgroup_at_anywhere.com)
Date: 04/06/04


Date: Tue, 6 Apr 2004 13:06:23 -0700

Ah, welcome to my world. On this one feature many o' VB developers switched
to C#. The unfortunate answer if that there is no *neat* way for VB
developers to use conversion (or operator) overloads. For conversion
overloads, they can, of course, explicitedly cast their objects to the
appropriate type. For operator overloads they would need to Operator+
syntax.

You have come across one of the top reasons that C# is significantly better
than current version of VB.NET and why I personally see no reason to use the
current version of VB.NET.

Tom

"Krunoslav" <ktrs66@hotmail.com> wrote in message
news:c4v1ed$uf6$1@ls219.htnet.hr...
> Hi ,
>
> I have implemented a struct in C# which acts as a sort of a variant type,
> i.e
> its constructors accept numeric types, dates and strings and the struct
> itself
> is convertible to those types but no other types and can perform
arithmetic
> operations with those types too.
>
> But arithmetic operations are not as important to me as the seamless
> conversion to and fro.
>
> For the conversion purpose I had to implement static conversion operators,
> implicit and explicit.
> I also implemented IConvertible interface in the struct.
>
> NOW THE PROBLEM.
> After testing the class in VB .NET I realized that those static converters
> as well as any static operators are unavailable. In C# everything works
> fine, but
> in VB I could not say something like
>
> MyStruct = 100
> or MyStruct = CType(100, MyStruct)
> even though MyStruct has a constructor with an int value (and others) as
> well as the conversion operators.
>
> Examination of my objects with ildasm proves that all of the conversion
> operators are there.
> As I say in C# everything works perfectly as it should.
>
> So I considered implementing a custom TypeConverter for my struct to see
if
> this will help.
>
> Then I performed the test in VB .NET to see what I can do with just the VB
> .NET itself not C#.
>
> I wrote the following in VB as a test, just the constructors and the
> TypeConverter:
> I implemented a type converter SmartTypeConverter for the structure too.
>
> namespace test
> <TypeConverter(GetType(SmartTypeConverter))> _
> Public Structure VBSmartVar
> Private m_object As Object
>
> Public Sub New(ByVal v As String)
> m_object = New Object()
> m_object = v
> End Sub
>
> Public Sub New(ByVal v As Integer)
> m_object = New Integer()
> m_object = v
> End Sub
>
> Public Sub New(ByVal v As Double)
> m_object = New Double()
> m_object = v
> End Sub
>
> Public Overrides Function ToString() As String
> Return CStr(m_object)
> End Function
>
> Public Overloads Function GetTypeCode() As TypeCode
> If TypeOf m_object Is String Then
> Return CStr(m_object).GetTypeCode()
> End If
> If TypeOf m_object Is Integer Then
> Return CInt(m_object).GetTypeCode()
> End If
> If TypeOf m_object Is Double Then
> Return CDbl(m_object).GetTypeCode()
> End If
> End Function
>
> Public Overloads Function GetHashCode() As Integer
> If TypeOf m_object Is String Then
> Return CStr(m_object).GetHashCode()
> End If
> If TypeOf m_object Is Integer Then
> Return CInt(m_object).GetHashCode()
> End If
> If TypeOf m_object Is Double Then
> Return CDbl(m_object).GetHashCode()
> End If
> End Function
>
> End Structure
> End Namespace
>
> The test proved unsuccessful, I still can not say either
> MyStruct = 100
> or CType(100, MyStruct).
>
> What I would like is something like this
> MyStruct1 = 100
> MyStruct2 = 100.45
>
> MyInteger = CInt(MyStruct)
> MyDouble = CDbl(MyStruct)
>
> etc
>
> I do not include here a type converter I used for brevity, but it
implements
> CanConvertTo, CanConvertFrom, ConvertTo, ConvertFrom and IsValid.
>
> Is it POSSIBLE to do something like this in VB like it is in C# and if so
> how please?
> This issue may severely affect my entire system design construct from
ground
> up.
>
> As you can see, I am more comfortable with C# due to a C++ background
though
> I used to program simple apps in VB 6. So if anyone knows a simple way to
do
> this in both C# and
> VB please let me know.
> Thanks to anyone that gives me any meaningful tips.
>
> my email: krtrs66@hotmail.com.
>
>
>
>



Relevant Pages

  • Re: Cast to a struct?
    ... Neither the source expression nor the target type of a cast is ... allowed to be a struct (except that any expression can be cast to ... A cast specifies a type conversion. ... unsigned int is exactly 32 bits. ...
    (comp.lang.c)
  • GURUS: VB .NET alternatives to C# type conversion operators and operator overloading?
    ... I have implemented a struct in C# which acts as a sort of a variant type, ... For the conversion purpose I had to implement static conversion operators, ... or MyStruct = CType ... I wrote the following in VB as a test, just the constructors and the ...
    (microsoft.public.dotnet.framework)
  • [PATCH v3] net/atm: move all compat_ioctl handling to atm/ioctl.c
    ... We have two implementations of the compat_ioctl handling for ATM, ... we do a conversion in fs/compat_ioctl.c. ... +static struct { ... unsigned int cmd, unsigned long arg) ...
    (Linux-Kernel)
  • Re: pointer to array of const objects
    ... > encapsulate an array inside a struct. ... "typedef" only creates an alias. ... anywhere in the conversion process. ...
    (comp.lang.c)
  • Re: Compiler chooses conv ctor - why?
    ... struct A { ... conversion function should be chosen, as according to 13.3.3.2of the standard, it is better function due to qualification conversion required by conversion constructor? ...
    (microsoft.public.vc.language)