Custom object array inside of another custom object

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of GoogleUser11 GoogleUser11 10 months, 1 week ago.

  • Author
    Posts
  • #34252
    Profile photo of GoogleUser11
    GoogleUser11
    Participant

    Hi,

    What I would like to do is create a custom object called "_my_sub_object" and then define my main object called "_my_main_object" where one of the fields is an array of objects using my sub object definition.

    What I need is an array of objects within an object. Any assistance would be appreciated.

    add-type @"
    public struct _my_sub_object {
    public string MySubField01;
    public string MySubField02;
    public string MySubField03;
    }

    "@

    add-type @"
    public struct _my_main_object {
    public string MyObjField01;
    public string MyObjField02;
    public string MyObjField03;
    }

    "@

  • #34253
    Profile photo of random commandline
    random commandline
    Participant
    # Create two PSCustomObject
    $my_main_object = [pscustomobject]@{
    public1 = 'MyObjField01'
    public2 = 'MyObjField02'
    public3 = 'MyObjField03'
    }
    $my_sub_object = [pscustomobject]@{
    public1 = 'MySubField01'
    public2 = 'MySubField02'
    public3 = 'MySubField03'
    }
    
    # Add objects to hash table
    $my_object = @{}
    $my_object.Add('my_main_object',$my_main_object)
    $my_object.Add('my_sub_object',$my_sub_object)
    $my_object; $my_object.Values

    Or you can create two hash tables then add to a $my_object
    Ex: $main=@{};$sub=@{}

  • #34271
    Profile photo of GoogleUser11
    GoogleUser11
    Participant

    Sorry about not being clear about I was looking for.

    I want to create a person object and an address object.
    In the person object I want to have an array of address objects.

    The address object must have 3 elements namely 1) Address type, 2) Street name and 3) State.

    The person object must have 3 elements namely 1) Name, 2) Last name and 3) array of address objects.

    Person (type/object)
    Name
    LastName
    Address (type/object)
    AddressType
    StreetName
    State

    Record # | Name | Last name | Address type | Street name | State |
    1 John Doe Physical 100 Main str NY
    Mailing P.O Box 123 NJ

    2 Jane Doe Physical 500 Main str NY

  • #34273
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    It would be exceedingly helpful to see your input data.

  • #34302
    Profile photo of GoogleUser11
    GoogleUser11
    Participant

    I do not have any input data yet. I am trying to see if this structure is even possible. Think of the case of a server and it's hard disks. You have a server object and a hard disk object. I want to create a function that collects information for a server and another function that collects information of a hard drive.

    As a server can have multiple hard disks so the server object must contain an array of hard disk information.

    Hope that helps.

You must be logged in to reply to this topic.