Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Error with array conversions

Avatar of Frankliga
FrankligaFlag for United States of America asked on
Visual Basic Classic.NET Programming
4 Comments1 Solution208 ViewsLast Modified:
I am receiving an error with converting arrays of two different types even though I have widening and narrowing CTYPE operators in MYClass.

Value of type '1-dimensional array of Integer' cannot be converted to '1-dimensional array of MyClass' because 'Integer' is not derived from 'MyClass'.

According to MSDN help this should work if there is a Widening / Narrowing Conversion.

Is there anything I can do?


Module Test
    Public Sub TestOK()
        'This works
        Dim i as integer
        Dim mc as MyClass
        i = New MyClass(1)
        mc = 7
        i = mc
    End Sub
 
    Public Sub TestFailed()
        'This doesn't work
        Dim intSet() as Integer
        Dim mcSet() as MyClass
 
        intSet = New Integer() {1,2,3,4,5}
        mcSet = intSet  'Compiler Error
 
       ' however this conversion works, because it's converting one element at a time.
        For each mc as MyClass In intSet 
            Debug.WriteLine(mc.ToString())
        Next
    End Sub
    
End Module
Pubilc Class MyClass
    Protected m_intID as Integer
    Protected m_strValue as String
    Public Sub New(ByVal intVal As Integer)
        m_intID = intVal
        m_strValue = ""
    End Sub
 
    Public Overrides Function ToString() As String
        Return m_intID.ToString() & "|" & m_strValue
    End Function
 
        Public Shared Narrowing Operator CType(ByVal mc As MyClass) As Integer
            Return mc.m_intID
        End Operator
        Public Shared Widening Operator CType(ByVal int As Integer) As MyClass
            Dim mcTemp As MyClass
            mcTemp = New MyClass(int)
            Return mcTemp
        End Operator
End Class
ASKER CERTIFIED SOLUTION
Avatar of abel
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answers