Type conversion problem

I'm using the .NET framework v1.1 and dominantly C#, but am having a problem with some VB code.

First of all, I have a type:

public class Field
{
object _fieldValue;

private object FieldValue
{
get { return _fieldValue; }
set { _fieldValue = value; }
}

public static implicit operator Field(string fromRawString)
{
Field untypedField = new Field();
untypedField.FieldValue = fromRawString;
return untypedField;
}
}

(Compiled using "csc /target:library MyType.cs")


I then write a program to use this type:

public class Klass
{
public static void Main()
{
string s = "Hello World";
Field f = s;
}
}

(Compiled using "csc /r:MyType.dll MyProg.cs")


I then write a similar program to use this type, but this time in VB.NET

Module Klass
Sub Main()
Dim s as String = "Hello World"
Dim f as Field = s
End Sub
End Module

(Compiled using "vbc /r:MyType.dll MyProg.vb")


But the vb compiler emits an error message that the C# one doesn't:
error BC30311: Value of type 'String' cannot be converted to 'Field'.


Now here's the thing... If I use the vbc from .NET 2.0, it works, so it appears to be a bug that has been fixed.

I'm interested in a work-around though. I would like to release an assembly built on the 1.1 Framework that contains a type that allows for implicit type conversion. I would like customers who use VB.NET to be able to do said type conversion. Is there a way for me to get this working

Thanks,
--Nick



Answer this question

Type conversion problem

  • Paul Gerald

    Thanks for the suggestion, but this doesn't solve the problem. I'd actually already tried this but just tried again and yah, it still doesn't work...


  • windows_mss

    To call the implicit operators from VB, you need to call the static op_Implicit methods..

    Module Klass
    Sub Main()
    Dim s as String = "Hello World"
    Dim f as Field = Field.op_Implicit(s)
    End Sub
    End Module


  • Zakamon

    Thanks for your input, but that's not the problem either.  Somebody really should try my sample code.

    In fact, here is a new copy that proves that your solution doesn't fix the issue...


    MyType.cs:  (csc /target:library MyType.cs)
    (Note the explicit type conversion this time.

    public class Field
    {
     object _fieldValue;

     private object FieldValue
     {
      get { return _fieldValue; }
      set { _fieldValue = value; }
     }

     public static explicit operator Field(string fromRawString)
     {
      Field untypedField = new Field();
      untypedField.FieldValue = fromRawString;
      return untypedField;
     }
    }


    Here's the vb code:

    NoStrict.vb: (c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\vbc.exe /r:MyType.dll NoStrict.vb)
    (strict off, doesn't work)

    Option Strict Off

    Module Klass
     Sub Main()
      Dim s as String = "Hello World"
      Dim f as Field = Ctype(s, Field)
     End Sub
    End Module

    NoStrict.vb: (c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\vbc.exe /r:MyType.dll NoStrict.vb)
    (strict on, doesn't work)

    Option Strict On

    Module Klass
     Sub Main()
      Dim s as String = "Hello World"
      Dim f as Field = Ctype(s, Field)
     End Sub
    End Module


    NoStrict.vb: (c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\vbc.exe /r:MyType.dll NoStrict.vb)
    (strict off, but that doesn't even matter... it works either way...)

    Option Strict Off

    Module Klass
     Sub Main()
      Dim s as String = "Hello World"
      Dim f as Field = Ctype(s, Field)
     End Sub
    End Module

     


    It would be great if somebody could actually copy this code and try compiling it before trying to tell me what the problem is.
    It looks like neither imlicit nor explicit type convesion of custom types works in v1.1 of VB.NET.
    Could this really be true   Show me an simple example of either implicit or explicit type convesion to custom types and I'll be a happy man.


    Oh, and as for single or int instead of string, yah, I have type conversions for all the types that I care about, but I was just showing the one for simplicity of the example.


    Thanks,
    --Nick


  • Martimus

    Actually, this doesn't solve the problem either.

    DirectCast requires an inheritance or implementation relationship. Since the Field type neither inherits from nor implements the string type, the DirectCast type conversion fails with an InvalidCastException.


  • twospoons

    Thanks DMan,

    That definitely works. Is there a way to set up my Field class to allow for this direct type casting to occur implicitly

    Thanks,

    --Nick


  • Syed Faraz Mahmood

    I'm thinking from looking at this thing that its possible - one of two things

    1. Default Property
    2. No conversion defined from string to field

    As it looks to me that you ultimately doing a conversion of a string to an Object which although is possible - you should probably better define you intentions than let implied conversion occur.

    What would you intention be if you tried using an integer or single instead of a string

    You could of course using a Ctype on the string and convert it to the object. If you put option strict on then you should see the problem with this implied conversion.


  • DearME

    VB added support for operator overloading in VS2005. Previous versions won't allow operator overloading, so that's why your code compiles with the .Net 2.0 VB compiler, but not with the .Net 1.1 compiler.

    You can work around the issue by declaring and calling the conversion function explicitly:

    Public Function FieldFromString(ByVal fromRawString As String) As Field
      untypedField As new Field()
      untypedField.FieldValue = fromRawString;
      return untypedField
    End Function

     Module Klass
     Sub Main()
      Dim s as String = "Hello World"
      Dim f as Field = FieldFromString(s)
     End Sub
    End Module

    Hope this helps,

    Abel.



  • Amit Bansal

    Although Ctype did not work in the 1.1 environment DirectCast did....

    Module Module1

    Sub Main()

    Dim s As String = "Hello World"

    Dim f As Field = DirectCast(s, Field)

    End Sub

    End Module



  • Blipwort

    I believe you will need to turn option strict Off, if you want implicit type conversion (not recommended, but will be what you need). You can do this by putting:

    Option Strict Off

    at the top of the file. There may be also a command line parameter to do this.



  • Type conversion problem