Using translate on variables in MEL

Hi,
Before anything else I would like to say that this is my 4th day of having a crack at MEL-script and probably need a lot more help than I'm asking for right now. But here we go...

I want to define the selected object in my scene as a variable. As far as I understand it's done like this:

string $object = ls -sl;

This works so far.My next step is to create a UI with a slider to control the translation of the object. This is what I've found so far.

window -title "sliderTest";columnLayout; 
attrFieldSliderGrp -label "Translate X"
-min -10.0
-max 10.0
-fieldMinValue -100.0
-fieldMaxValue 100.0
-at "pSphere1.tx";
showWindow;

This works great when the object name is specified but not when I use the variable $object instead. This is the error I get.

// Error: Invalid redeclaration of variable "$object" as a different type. //

I'm sure I'm doing it completely wrong and would appreciate absolutely any help I can get.
Thanks.

1)
well first off restart maya ^^
you have most likely defined $object before so you can not redifine it.
Maya remembers all variables you define each session. And the only way to clear it is to reboot maya. (as far as i know)

2)
secondly the "ls" command returns a string array.
so if you want to define it correctly you would need to write

string $object[] = 'ls -sl';

3)
now if you would want to use it in your window code you would need to tell maya which object in the string array you would want to use.
In order to do that write a number in the brackets like this.

$object[0]

(0 is the first object in the array)

so your window code will look like this.

window -title "sliderTest";columnLayout; 
attrFieldSliderGrp -label "Translate X"
-min -10.0
-max 10.0
-fieldMinValue -100.0
-fieldMaxValue 100.0
-at ($object[0]+".tx");
showWindow;

( you need to use brackets and the + in order to make a correct syntax, read up on the mell syntax it will help you a lot.)

4)
here is a little bit extra for you to play with. Select several objects in your scene and run the following code.

// get your objects selection
string $myObjects[] = ls -sl;

//build window and layout

window -title "sliderTest";

columnLayout;

// for each object that is in the $myObjects array do the following code.

for ($thisObject in $myObjects){

    attrFieldSliderGrp -label ($thisObject + " Translate X")

    -min -10.0

    -max 10.0

    -fieldMinValue -100.0

    -fieldMaxValue 100.0

    -at ($thisObject+".tx");

}

showWindow;

have fun coding in mel ^^

edit: replaced # with // (python coding :P)

Jan:

1) Well there is a alternative to that. and thats to put a scoping variable around your code. So you do:

{
  ...your code goes here..
}

thanka a lot. this was great. You have single handedly trippled my mel scripting knowledge.
really, thanks! 

Ah so thats what that trick does !
Thanks joojaa!