Click here to Skip to main content
15,917,859 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
string labelName = "lblCity";


lblAmtInWords is label name which i given in label properties..

C#
lblAmtInWords.Location = new Point(lblAmtInWords.Location.X, 238);
lblAmtInWords.Location = new Point(lblAmtInWords.Location.Y, 684);

i want to pass the lblCity instead of lblAmtInWords

C#
lblCity.Location = new Point(Convert.ToInt32(split1[i + 1]), Convert.ToInt32(split1[i + 2]));


getting error like this...

error:'string' does not contain a definition for 'Location' and no extension method 'Location' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
Posted
Updated 8-Jan-16 5:20am
v2
Comments
Sergey Alexandrovich Kryukov 8-Jan-16 12:41pm    
The question makes no sense. You got a message
'string' does not contain a definition for 'Location'

It does not mean you should ask how to declare a string with 'Location'. This is wrong thinking. All explanations are useless until you learn how to use some logic.

—SA

I would use a Dictionary<String, Label> for the purpose, e.g.
C#
Dictionary<String, Label> labelByName = new Dictionary<string, Label>();
labelByName.Add("price", lblPrice);
labelByName["price"].Text = "Please specify the price";
 
Share this answer
 
Comments
Maciej Los 8-Jan-16 15:34pm    
Sounds reasonable. 5!
CPallini 8-Jan-16 16:06pm    
Thank you!.
If you want to pass a label, then pass a label: a string does not have a Location property because it's not a displayable item - only Control based classes are.

You can pass a label to a method very easily:
C#
public void MyMethod(Label lab, string str, Point p)
   {
   lab.Location = p;
   lab.Text = str;
   }
...
   MyMethod(myLabelToTamperWith, "Some text", new Point(100,100));
But if you are trying to pass the name of a variable which refers to a label then it gets much more complex, and you have to faff about with Reflection or similar depending on your environment. Normally, it's a sign you have made something far more complicated than it needs to be! :laugh:
 
Share this answer
 
Comments
Sathish km 8-Jan-16 8:36am    
for (int i = 0; i < split1.Count(); i += 3)
{
Label LabelName = new Label();
LabelName.Name = split1[i].ToString();

//lblVoucherNo.Location.
lblVoucherNo.Location = new Point(Convert.ToInt32(split1[i + 1]), Convert.ToInt32(split1[i + 2]));

MyMethod(LabelName, new Point(Convert.ToInt32(split1[i + 1]), Convert.ToInt32(split1[i + 2])));
}


public void MyMethod(Label lab, Point p)
{
lab.Location = p;
//lab.Text = str;
}

i want to pass LabelName.Name to lab.Location instead of lab...
OriginalGriff 8-Jan-16 8:48am    
Why?
As I said, it's normally an indication that you have done something wrong in your design!
Why do you think you need to do this?
Sathish km 8-Jan-16 8:54am    
want to pass assign name of all control to lab.Location from LabelName.Name = split1[i].ToString();

if LabelName.Name is "a" or "b" or "c" pass it to lab.location (name)
OriginalGriff 8-Jan-16 9:08am    
Yes, I kinda guessed that...but why?
The name of a variable is not the Control itself, it's a memory location that holds a reference to the control, is all.
It looks like you've painted yourself into a corner, and think this is the best way out - when frequently all you are doing is making life a lot harder for yourself. Why do you need this, particularly since you don't seem to have been coding very long?
Sathish km 8-Jan-16 9:14am    
i want to assign location to many controls so i don't want it assign as multiple line code. i need to assign in single line code....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900