Windows Vista Forums

Referencing Variables in a For Loop
  1. #1


    Kuma Guest

    Referencing Variables in a For Loop

    I am creating a [system.windows.forms.form] object with the following.
    [Void]
    [System.Reflection.Assembly]::Loadwithpartialname("System.Windows.Forms")
    [Void]
    [System.Reflection.Assembly]::Loadwithpartialname("System.Drawing")
    $form = new-object system.windows.forms.form
    $form.size = new-object system.drawing.size(1280,1024)

    Then I need to add 10 labels all with the same properties except for
    text and location. I know I can just type them all out but I would
    rather use a for loop like so.

    for($i=0;$i -lt 10;$i++)
    {
    set-variable -name "label$i" -value (new-object
    system.windows.forms.label)
    }



    I can create the labels with this, I have checked the variables and
    they are indeed labels with all the appropriate properties, methods,
    etc. What I can't figure out is how to reference them in the same for
    loop like so.

    for($i=0;$i -lt 10;$i++)
    {
    set-variable -name "label$i" -value (new-object
    system.windows.forms.label)
    $label$i.size = new-object system.drawing.size(140,30)
    $label$i.set_backcolor('0')
    }
    I have tried every possible combination of "",' ', (), {}, and + that
    i can think of, but i can't get it to accept $label$i in any form as a
    variable. Any thoughts or am I doomed to typing them all out one at a
    time?

    Thanks,

    Kuma


      My System SpecsSystem Spec

  2. #2


    Jeff Guest

    Re: Referencing Variables in a For Loop

    On Oct 11, 9:18 pm, Kuma <kumasa...@xxxxxx> wrote:

    > I am creating a [system.windows.forms.form] object with the following.
    > [Void]
    > [System.Reflection.Assembly]::Loadwithpartialname("System.Windows.Forms")
    > [Void]
    > [System.Reflection.Assembly]::Loadwithpartialname("System.Drawing")
    > $form = new-object system.windows.forms.form
    > $form.size = new-object system.drawing.size(1280,1024)
    >
    > Then I need to add 10 labels all with the same properties except for
    > text and location. I know I can just type them all out but I would
    > rather use a for loop like so.
    >
    > for($i=0;$i -lt 10;$i++)
    > {
    > set-variable -name "label$i" -value (new-object
    > system.windows.forms.label)
    > }
    >
    > I can create the labels with this, I have checked the variables and
    > they are indeed labels with all the appropriate properties, methods,
    > etc. What I can't figure out is how to reference them in the same for
    > loop like so.
    >
    > for($i=0;$i -lt 10;$i++)
    > {
    > set-variable -name "label$i" -value (new-object
    > system.windows.forms.label)
    > $label$i.size = new-object system.drawing.size(140,30)
    > $label$i.set_backcolor('0')
    > }
    > I have tried every possible combination of "",' ', (), {}, and + that
    > i can think of, but i can't get it to accept $label$i in any form as a
    > variable. Any thoughts or am I doomed to typing them all out one at a
    > time?
    >
    > Thanks,
    >
    > Kuma
    Kuma,

    I would recommend using an array:

    $labels = @()

    for ( $i = 0; $i -lt 10; $i++ )
    {
    $label = New-Object System.Windows.Forms.Label

    $label.Size = New-Object System.Drawing.Size( 140, 30 )
    $label.BackColor = [System.Drawing.Color]::Orange

    $labels += $label
    }

    After the loop, you can just subscript into the array to get the label
    you want:

    $labels[ 0 ].Text = "I'm a label!"

    Good luck.
    Jeff


      My System SpecsSystem Spec

  3. #3


    Kuma Guest

    Re: Referencing Variables in a For Loop

    On Oct 12, 12:09 am, Jeff <jeff.hill...@xxxxxx> wrote:

    > On Oct 11, 9:18 pm, Kuma <kumasa...@xxxxxx> wrote:
    >
    >
    >
    >
    >

    > > I am creating a [system.windows.forms.form] object with the following.
    > > [Void]
    > > [System.Reflection.Assembly]::Loadwithpartialname("System.Windows.Forms")
    > > [Void]
    > > [System.Reflection.Assembly]::Loadwithpartialname("System.Drawing")
    > > $form = new-object system.windows.forms.form
    > > $form.size = new-object system.drawing.size(1280,1024)
    >

    > > Then I need to add 10 labels all with the same properties except for
    > > text and location. I know I can just type them all out but I would
    > > rather use a for loop like so.
    >

    > > for($i=0;$i -lt 10;$i++)
    > > {
    > > set-variable -name "label$i" -value (new-object
    > > system.windows.forms.label)
    > > }
    >

    > > I can create the labels with this, I have checked the variables and
    > > they are indeed labels with all the appropriate properties, methods,
    > > etc. What I can't figure out is how to reference them in the same for
    > > loop like so.
    >

    > > for($i=0;$i -lt 10;$i++)
    > > {
    > > set-variable -name "label$i" -value (new-object
    > > system.windows.forms.label)
    > > $label$i.size = new-object system.drawing.size(140,30)
    > > $label$i.set_backcolor('0')
    > > }
    > > I have tried every possible combination of "",' ', (), {}, and + that
    > > i can think of, but i can't get it to accept $label$i in any form as a
    > > variable. Any thoughts or am I doomed to typing them all out one at a
    > > time?
    >

    > > Thanks,
    >

    > > Kuma
    >
    > Kuma,
    >
    > I would recommend using an array:
    >
    > $labels = @()
    >
    > for ( $i = 0; $i -lt 10; $i++ )
    > {
    > $label = New-Object System.Windows.Forms.Label
    >
    > $label.Size = New-Object System.Drawing.Size( 140, 30 )
    > $label.BackColor = [System.Drawing.Color]::Orange
    >
    > $labels += $label
    >
    > }
    >
    > After the loop, you can just subscript into the array to get the label
    > you want:
    >
    > $labels[ 0 ].Text = "I'm a label!"
    >
    > Good luck.
    > Jeff- Hide quoted text -
    >
    > - Show quoted text -
    Never thought of that, thanks for the help.


      My System SpecsSystem Spec

  4. #4


    Keith Hill [MVP] Guest

    Re: Referencing Variables in a For Loop

    "Kuma" <kumasan76@xxxxxx> wrote in message
    news:1192112295.771857.304690@xxxxxx

    > for($i=0;$i -lt 10;$i++)
    > {
    > set-variable -name "label$i" -value (new-object
    > system.windows.forms.label)
    > $label$i.size = new-object system.drawing.size(140,30)
    > $label$i.set_backcolor('0')
    > }
    > I have tried every possible combination of "",' ', (), {}, and + that
    > i can think of, but i can't get it to accept $label$i in any form as a
    > variable. Any thoughts or am I doomed to typing them all out one at a
    > time?
    Get-Variable is your friend e.g.:

    Get-Variable -name "label$i"

    --
    Keith


      My System SpecsSystem Spec

  5. #5


    Kiron Guest

    Re: Referencing Variables in a For Loop

    You can use invoke-expression:

    for($i=0;$i -lt 10;$i++)
    {
    set-variable -name "label$i" -value (new-object system.windows.forms.label)
    invoke-expression "$(""`$label$i"").size = new-object system.drawing.size(140,30)"
    invoke-expression "$(""`$label$i"").set_backcolor('0')"
    }

    --
    Kiron

      My System SpecsSystem Spec

Referencing Variables in a For Loop problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Referencing .NET 2.0 but end result uses .NET 3.5 Israel .NET General 5 12 Mar 2010
Re: Referencing enumeration Shay Levy [MVP] PowerShell 0 07 Feb 2010
Referencing Posts ? Dwarf Vista General 7 30 Oct 2007
Sundeep Raina:) variables being handled in Workflow and How to declare New Variables raisundeep@gmail.com WinFX General 1 09 Aug 2006
Sundeep Raina(Iopsis): Is variables being handled in Workflow and How to declare New Variables sunny Avalon 0 30 Jun 2006