Windows Vista Forums

Writing a cmdlet: using a variable named "object"
  1. #1


    Marco Shaw Guest

    Writing a cmdlet: using a variable named "object"

    C# novice...

    It would appear that "object" is a reserved word in C#. At least I
    cannot seem to easily declare a variable with the name "object".



    I'm writing a cmdlet, and I actually want one of the parameters to be
    named "object".

    Is there some way I can make it work?

    For now, I just do something like this:

    private string _myObject;
    [Parameter(Position = 2)]
    [ValidateNotNullOrEmpty]
    public string myObject
    {
    get { return _myObject; }
    set { _myObject = value; }
    }

    Marco

      My System SpecsSystem Spec

  2. #2


    Keith Hill [MVP] Guest

    Re: Writing a cmdlet: using a variable named "object"

    "Marco Shaw" <marco.shaw@_NO_SPAM_gmail.com> wrote in message
    news:O%23QcHh%23lHHA.1216@TK2MSFTNGP03.phx.gbl...
    > C# novice...
    >
    > It would appear that "object" is a reserved word in C#. At least I cannot
    > seem to easily declare a variable with the name "object".


    Yes it is language shorthand for System.Object. Just like string is short
    for System.String.

    > I'm writing a cmdlet, and I actually want one of the parameters to be
    > named "object".
    >
    > Is there some way I can make it work?
    >
    > For now, I just do something like this:
    >
    > private string _myObject;
    > [Parameter(Position = 2)]
    > [ValidateNotNullOrEmpty]
    > public string myObject
    > {
    > get { return _myObject; }
    > set { _myObject = value; }
    > }
    >


    You can use this syntax "@Object" to tell the compiler that Object is the
    property name and doesn't refer to System.Object. However I would consider
    renaming the paramter. Write-Host is the only cmdlet I see that uses a
    param named Object. Others use:

    InputObject, DifferenceObject, ReferenceObject, TargetObject, AclObject

    694# gcm -type cmdlet | select @{n='CmdletName';e={$_.Name}} -expand
    ParameterSets | select CmdletName -expand Parameters | ? {$_.Name -match
    'object'} | Sort Name | ft CmdletName, Name -a

    --
    Keith



      My System SpecsSystem Spec

  3. #3


    William Stacey [C# MVP] Guest

    Re: Writing a cmdlet: using a variable named "object"

    other cmdlets you will note, use "Value" for this reason. I stuck with same
    name to go with the flow.

    --
    William Stacey [C# MVP]


    "Marco Shaw" <marco.shaw@_NO_SPAM_gmail.com> wrote in message
    news:O%23QcHh%23lHHA.1216@TK2MSFTNGP03.phx.gbl...
    | C# novice...
    |
    | It would appear that "object" is a reserved word in C#. At least I
    | cannot seem to easily declare a variable with the name "object".
    |
    | I'm writing a cmdlet, and I actually want one of the parameters to be
    | named "object".
    |
    | Is there some way I can make it work?
    |
    | For now, I just do something like this:
    |
    | private string _myObject;
    | [Parameter(Position = 2)]
    | [ValidateNotNullOrEmpty]
    | public string myObject
    | {
    | get { return _myObject; }
    | set { _myObject = value; }
    | }
    |
    | Marco



      My System SpecsSystem Spec

  4. #4


    Marco Shaw Guest

    Re: Writing a cmdlet: using a variable named "object"

    Keith Hill [MVP] wrote:
    > "Marco Shaw" <marco.shaw@_NO_SPAM_gmail.com> wrote in message
    > news:O%23QcHh%23lHHA.1216@TK2MSFTNGP03.phx.gbl...
    >> C# novice...
    >>
    >> It would appear that "object" is a reserved word in C#. At least I cannot
    >> seem to easily declare a variable with the name "object".

    >
    > Yes it is language shorthand for System.Object. Just like string is short
    > for System.String.


    I shoulda thought of that...

    Marco

      My System SpecsSystem Spec

Writing a cmdlet: using a variable named "object" problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error: "Cannot convert "System.Object[]" to "System.Int32"." Shay Levi PowerShell 3 31 Jan 2008
Writing a cmdlet: "Does not exist in the current context" error Marco Shaw PowerShell 1 09 May 2007
Separate hashtable vs $alist | where-object { "key" = "value" } versus something else? ydroam PowerShell 2 12 Dec 2006
Writing a "good" error object? Alex K. Angelopoulos [MVP] PowerShell 0 09 Sep 2006
False IE doc body error - "Object reference not set to an instance of an object" Alex K. Angelopoulos [MVP] PowerShell 7 15 Jul 2006