Solved

Error with array conversions

Posted on 2009-05-07
7
174 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

746 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

13 Experts available now in Live!

Get 1:1 Help Now