Solved

Error with array conversions

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

773 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