Solved

Error with array conversions

Posted on 2009-05-07
7
175 Views
Last Modified: 2013-11-26
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

Open in new window

0
Comment
Question by:Frankliga
  • 3
7 Comments
 
LVL 39

Accepted Solution

by:
abel earned 125 total points
ID: 24327666
This doesn't work because you are trying to converse a whole array at once and that is considered a different type. You'll have to loop through the array, regardless, there's little you can do about that. This is even still true in VB.NET, btw.
0
 

Author Comment

by:Frankliga
ID: 24328023
I don't understand why it's not allowed if an explicit conversion for each element exists.

Is this a limitation of only VB.NET or is this a problem C# also?  
0
 
LVL 39

Assisted Solution

by:abel
abel earned 125 total points
ID: 24328533
It is a problem in any language I know of, with the positive exception of scripting languages like perl, ruby, perhaps python and rebol. C# certainly has this same problem. You can't even cast an array of int32 to an array of int64...

the point here is, the array on itself is considered a type on its own, and combined with the type you store in it, it becomes a new type. Hence, even if it contained only one element, you cannot cast an array of one type to another, even if an explicit cast exist.

The reason behind this goes a bit deeper and has everything to do with the way that arrays are allocated and stored. Each element takes a certain amount of memory and this is stacked on op of one another. Though it is possible to write a castable array, it would yield the same exercise: create a new array of the size and type of the new element and cast each object one by one.
0
 
LVL 39

Expert Comment

by:abel
ID: 25237066
The question was: "how is cast-conversion with arrays with different element types possible". The answer was: "this cannot be done, you will have to loop". In the follow up question, more information was requested, which I provided. If something cannot be done, that's also a fair answer.

Suggestion to accept http:#24327666 as answer and http:#24328533 as assist.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now