Writing C# .net code in PowerShell

Tagged: 

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Paul Smith Paul Smith 7 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #35999
    Profile photo of Paul Smith
    Paul Smith
    Participant

    Hi

    I am currently working through some webservice calls wrote in C# .net and converting these to PowerShell. This has been going ok until I got to the following:

    test.systemFields = new systemFieldsType[2]

    int j = 0;
    metadata.systemFields[j].name = "name";
    metadata.systemFields[j++].value = "test name";
    metadata.systemFields[j].name = "type";
    metadata.systemFields[j++].value = "test";

    In PowerShell I am trying:

    $test.systemFields = New-Object systemFieldsType[2]

    I can see systemFieldsType as an option but it doesn't like me adding [2].

    Any ideas how I deal with this in PowerShell?

    Many thanks
    Paul

    #36001
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You're creating an array with two elements. That's shorthand for calling the array constructor with an argument of two, which looks like this in PowerShell:

    $test.systemFields = New-Object -TypeName systemFieldsType[] -ArgumentList 2
    
    # Or as I usually write it, out of habit:
    
    $test.systemFields = New-Object systemFieldsType[](2)
    

    I know that second syntax is sort of me wishing that New-Object was just like the new statement in C#, but I can't break the habit. That's what New should look like. 😛

    #36015
    Profile photo of Paul Smith
    Paul Smith
    Participant

    Hi Dave

    Thanks for the reply.

    When I run :
    $test.systemFields = New-Object -TypeName systemFieldsType[]

    and then:
    $test.systemFields

    I get the 'name' and 'value' headers. However, when I run:
    $test.systemFields = New-Object -TypeName systemFieldsType[] -ArgumentList 2

    and then:
    $test.systemFields

    I don't get the headers and cannot set the following:

    int j = 0;
    metadata.systemFields[j].name = "name";
    metadata.systemFields[j++].value = "test name";
    metadata.systemFields[j].name = "type";
    metadata.systemFields[j++].value = "test";

    Any ideas?

    Many thanks
    Paul

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.