Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Missing Class

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Missing Class

Represents a missing Object. This class cannot be inherited.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class Missing _
    Implements ISerializable
Visual Basic (Usage)
Dim instance As Missing
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class Missing : ISerializable
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class Missing sealed : ISerializable
JScript
public final class Missing implements ISerializable

Missing is used to invoke a method with a default argument.

Only one instance of Missing ever exists.

The following example shows how to use Missing to invoke a method with a default argument. To compile and run this code example, you must first compile the following Visual Basic code as a DLL assembly named Target.dll. Save the code as Target.vb. From the command line, use vbc /t:library Target.vb to compile the assembly.

Imports System

Public Class MissingSample
    
    Public Shared Sub MyMethod(Optional k As Integer = 33)
        Console.WriteLine("k = " & k.ToString())
    End Sub

End Class

If you compile Target.dll in Visual Studio, by default the project name is used as the name of a namespace that contains the MissingSample class. Either remove the namespace from the Target project, or add a using Target; statement to the C# code (Imports Target in Visual Basic, using namespace Target; in Visual C++).

NoteNote:

Visual Basic code is used for Target.dll because C# and Visual C++ do not support optional parameters in managed code. Optional parameters are not part of the Common Language Specification. Therefore, code that uses optional parameters is not CLS-compliant. For more information, see Writing CLS-Compliant Code.

When you compile the Visual Basic and C# versions of this code example, add references to Target.dll. For example, if you are compiling from the command line, use the /r:Target.dll option. The Visual C++ version of the code example already includes a #using statement for Target.dll.

Visual Basic
Imports System
Imports System.Reflection

Public Module Example

    Sub Main()
        ' To invoke MyMethod with the default argument value, pass 
        ' Missing.Value for the optional parameter. First, get the 
        ' method.
        Dim mi As MethodInfo = _
            GetType(MissingSample).GetMethod("MyMethod")

        ' Second, create an array of parameters to pass to the method.
        ' In this case, the array contains just one element.
        Dim arguments() As Object =  { Missing.Value }

        ' Finally, invoke the method. Specify Nothing for the target
        ' object, because the method is Shared.
        mi.Invoke(Nothing, arguments)
    End Sub

End Module

' This code example produces the following output:
'
'k = 33

C#
using System;
using System.Reflection;

class Example
{
    static void Main()
    {
        // To invoke MyMethod with the default argument value, pass 
        // Missing.Value for the optional parameter.
        MethodInfo mi = typeof(MissingSample).GetMethod("MyMethod");
        mi.Invoke(null, new Object[] { Missing.Value });
    }
} 

/* This code example produces the following output:

k = 33
 */

Visual C++
#using "target.dll"

using namespace System;
using namespace System::Reflection;

void main()
{
    // To invoke MyMethod with the default argument value, pass 
    // Missing::Value for the optional parameter.
    MethodInfo^ mi = MissingSample::typeid->GetMethod("MyMethod");
    mi->Invoke(nullptr, gcnew array<Object^> { Missing::Value });
};    

/* This code example produces the following output:

k = 33
 */

System..::.Object
  System.Reflection..::.Missing
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker