Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Error with array conversions

Posted on 2009-05-07
7
Medium Priority
?
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
7 Comments
 
LVL 39

Accepted Solution

by:
abel earned 500 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 500 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

618 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