Color as Param

  • Thread starter Thread starter Daniel Groh
  • Start date Start date
D

Daniel Groh

public LabelMsg(System.Web.UI.WebControls.Label lblMsg, string strMensagem,
Color.???)
{
lblMsg.Text = strMensagem;
lblMsg.ForeColor = clrLabel.???
}

I need to give the color as a param, wich type should i use ?
 
Daniel said:
public LabelMsg(System.Web.UI.WebControls.Label lblMsg, string strMensagem,
Color.???)
{
lblMsg.Text = strMensagem;
lblMsg.ForeColor = clrLabel.???
}

I need to give the color as a param, wich type should i use ?
system.color ?
JB
 
No...there is System.Drawing.Color but it's not working as a reference!

if i do

System.Drawing.Color myColor

myColor.(point)...

it's showing to me properties that dont solve my probleb of textcolor!
 
Hello,
You can use this:

public LabelMsg(System.Web.UI.WebControls.Label lblMsg, string
strMensagem,System.Drawing.Color color);
{
lblMsg.Text = strMensagem;
lblMsg.ForeColor = color;
}

and later use it like this:
LabelMsg("Hello World","Hi World", Color.Red)

HTH. Cheers:)
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
Daniel said:
No...there is System.Drawing.Color but it's not working as a reference!

if i do

System.Drawing.Color myColor

myColor.(point)...

it's showing to me properties that dont solve my probleb of textcolor!
Doh!
Knew I should have checked that first :)
It is still the correct type to use though.
What is it not doing that you want it to?

What is MyColor.(point)?

Color is static.

lblMyLabel.ForeColor = System.Drawing.Color.SteelBlue;

JB
 
That's tha way...I found it!

private void Page_Load(object sender, System.EventArgs e)
{
lblMsg.Text = "Testando parâmetro de cores";
SetColor(lblMsg,"Red");
}

private void SetColor(Label lblTexto, string strColor)
{
lblTexto.ForeColor = System.Drawing.Color.FromName(strColor); //This
method is the way --FromName--
}
 
Paperback said:
That's tha way...I found it!

private void Page_Load(object sender, System.EventArgs e)
{
lblMsg.Text = "Testando parâmetro de cores";
SetColor(lblMsg,"Red");
}

private void SetColor(Label lblTexto, string strColor)
{
lblTexto.ForeColor = System.Drawing.Color.FromName(strColor); //This
method is the way --FromName--
}
UGLYYYYYY :)


Seriously, doing this is less intuitive, and way more fragile than
passing color as a color type.

I (or any future developers) can now break your code by doing
SetColor(lblmsg, "BlaBlaBla")

How about

private void SetColor(Label label, Color color)
{
label.ForeColor = color;
}


....
SetColor(myLabel, Color.Red);

Then, the programmer will get strong typing, (cannot pass an invalid
color), intellisense support etc..

Color.FromName will also incur a performance hit as well because it will
have to parse your string and map it to a valid colour.

HTH

JB
 
Back
Top