Microsoft Knowledge Base Email Alertz

KBAlertz.com: (896579) - Describes possible array covariance issues that may affect a Visual Basic .NET application.

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 896579 - Last Review: December 3, 2007 - Revision: 2.6

Your Visual Basic .NET or Visual Basic 2005 application may be vulnerable to array covariance issues

For a Microsoft Visual C# .NET version of this article, see 897545  (http://kbalertz.com/Feedback.aspx?kbNumber=897545/ ) .

SUMMARY

This article describes array covariance issues to which a Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 application is vulnerable.

MORE INFORMATION

An array covariance occurs when the following conditions are true:
  • You have two reference types. For example, you have class A and class B.
  • The two reference types have an implicit or explicit reference conversion from a class of B to a class of A.
The same reference conversion issue exists from an array of B[R] to an array of A[R]. In this example, R represents an element of an array.

Array covariance means that an element of an array that has an element type of A is actually an element of an array that has an element type of B if the following conditions are true:
  • Both A and B are a reference type.
  • A is a base type of B.
In the second invocation of the F method in the following code example, the actual element of the array variable b is B, not A. Therefore, the element is treated as a different class and a System.ArrayTypeMismatchException exception is thrown.

Array covariance does not apply to an array of values.

The following Visual Basic .NET or Visual Basic 2005 code example demonstrates how the System.ArrayTypeMismatchException exception is thrown when you run a console application. Notice that function F takes a class A object as a parameter. However, function F actually receives a class B object. This behavior causes the System.ArrayTypeMismatchException exception to be thrown at run time.
Module Module1
    Class A
    End Class

    Class B
        Inherits A
    End Class

    Sub Main()
        Dim a() As A
        ReDim a(10)
        Dim b() As A = New B(10) {} ' An implicit reference conversion is performed.
        F(a(0)) ' This works correctly.
        F(b(0)) ' Because the element type of an array b is class B, an ArrayTypeMismatchException exception is thrown.
    End Sub

    Sub F(ByRef x As A)
    End Sub

End Module

REFERENCES

For more information about array covariance from the Visual Basic .NET language specification, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/aa711607(VS.71).aspx (http://msdn2.microsoft.com/en-us/library/aa711607(VS.71).aspx)

APPLIES TO
  • Microsoft .NET Framework 2.0
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbhowto kbinfo arraytypemismatchexception kbvs2005applies kbvs2005swept KB896579
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please