Solved

Error with array conversions

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

820 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