Export (0) Print
Expand All

ArrayTypeMismatchException.ArrayTypeMismatchException() Constructor

Initializes a new instance of the ArrayTypeMismatchException class.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public ArrayTypeMismatchException ()
public ArrayTypeMismatchException ()
public function ArrayTypeMismatchException ()
Not applicable.

This constructor initializes the Message property of the new instance to a system-supplied message that describes the error, such as "Source array type cannot be assigned to destination array type." This message takes into account the current system culture.

The following table shows the initial property values for an instance of ArrayTypeMismatchException.

Property

Value

InnerException

A null reference (Nothing in Visual Basic).

Message

The localized error message string.

The following example demonstrates the ArrayTypeMismatchException() constructor of the ArrayTypeMismatchException class. It contains a function which takes two arrays as arguments and checks whether the two arrays are of the same type. If the arrays are of different types, a new ArrayTypeMismatchException is thrown and then caught in the calling method.

using System;

public class ArrayTypeMisMatchConst
{
   public void CopyArray(Array myArray,Array myArray1)
   {
      string typeArray1 = myArray.GetType().ToString();
      string typeArray2 = myArray1.GetType().ToString();
      // Check whether the two arrays are of same type or not.
      if(typeArray1==typeArray2)
      {
         // Copy the values from one array to another.
         myArray.SetValue("Name: "+myArray1.GetValue(0),0);
         myArray.SetValue("Name: "+myArray1.GetValue(1),1);
      }
      else
      {
         // Throw an exception of type 'ArrayTypeMismatchException'.
         throw new ArrayTypeMismatchException();
      }
   }
   static void Main()
   {
      try
      {
         string[] myStringArray = new string[2];
         myStringArray.SetValue("Jones",0);
         myStringArray.SetValue("John",1);

         int[] myIntArray = new int[2];
         ArrayTypeMisMatchConst myArrayType = new ArrayTypeMisMatchConst();
         myArrayType.CopyArray(myStringArray,myIntArray);
      }
      catch(ArrayTypeMismatchException e)
      {
         Console.WriteLine("The Exception is :"+e);
      }
   }
}


import System.*;

public class ArrayTypeMisMatchConst
{
    public void CopyArray(Array myArray, Array myArray1)
    {
        String typeArray1 = myArray.GetType().ToString();
        String typeArray2 = myArray1.GetType().ToString();
        // Check whether the two arrays are of same type or not.
        if (typeArray1.Equals(typeArray2)) {
            // Copy the values from one array to another.
            myArray.SetValue("Name: " + myArray1.GetValue(0), 0);
            myArray.SetValue("Name: " + myArray1.GetValue(1), 1);
        }
        else {
            // Throw an exception of type 'ArrayTypeMismatchException'.
            throw new ArrayTypeMismatchException();
        }
    } //CopyArray

    public static void main(String[] args)
    {
        try {
            String myStringArray[] = new String[2];
            myStringArray.SetValue("Jones", 0);
            myStringArray.SetValue("John", 1);
            int myIntArray[] = new int[2];
            ArrayTypeMisMatchConst myArrayType = new ArrayTypeMisMatchConst();
            myArrayType.CopyArray(myStringArray, myIntArray);
        }
        catch (ArrayTypeMismatchException e) {
            Console.WriteLine("The Exception is :" + e);
        }
    } //main
} //ArrayTypeMisMatchConst

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft