471,325 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Enums as parameters

I have the following:

void setColours()
{
Console.BackgroundColor = ConsoleColor .Black;
Console.ForegroundColor = ConsoleColor.Yellow;
}

thus setting the colours at compile time.

I would like to set the colours at run time and have something like:

void setColours( bgColour, fgColour)
{
Console.BackgroundColor = ConsoleColor .bgColour;
Console.ForegroundColor = ConsoleColor.fgColour;
}

however the compiler does not allow this.

Is it possible to set colours at run time and if so how would i do it?

Thanks
Aug 15 '08 #1
7 1206
Paolo <Pa***@discussions.microsoft.comwrote:
I have the following:

void setColours()
{
Console.BackgroundColor = ConsoleColor .Black;
Console.ForegroundColor = ConsoleColor.Yellow;
}

thus setting the colours at compile time.

I would like to set the colours at run time and have something like:

void setColours( bgColour, fgColour)
{
Console.BackgroundColor = ConsoleColor .bgColour;
Console.ForegroundColor = ConsoleColor.fgColour;
}

however the compiler does not allow this.

Is it possible to set colours at run time and if so how would i do it?
You just need to change your code to give types to the parameters:

void setColours(ConsoleColor bgColour, ConsoleColor fgColour)
{
Console.BackgroundColor = bgColour;
Console.ForegroundColor = fgColour;
}
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 15 '08 #2
When setting properties, only use the variable names. You have
ConsoleColor.bgColour. Get rid of the ConsoleColor reference. This is
probably what the compiler is complaining about.

"Paolo" <Pa***@discussions.microsoft.comwrote in message
news:73**********************************@microsof t.com...
>I have the following:

void setColours()
{
Console.BackgroundColor = ConsoleColor .Black;
Console.ForegroundColor = ConsoleColor.Yellow;
}

thus setting the colours at compile time.

I would like to set the colours at run time and have something like:

void setColours( bgColour, fgColour)
{
Console.BackgroundColor = ConsoleColor .bgColour;
Console.ForegroundColor = ConsoleColor.fgColour;
}

however the compiler does not allow this.

Is it possible to set colours at run time and if so how would i do it?

Thanks

Aug 15 '08 #3
Peter: thanks, that explains why I received the compiler error that I did.

"Peter Rilling" wrote:
When setting properties, only use the variable names. You have
ConsoleColor.bgColour. Get rid of the ConsoleColor reference. This is
probably what the compiler is complaining about.

"Paolo" <Pa***@discussions.microsoft.comwrote in message
news:73**********************************@microsof t.com...
I have the following:

void setColours()
{
Console.BackgroundColor = ConsoleColor .Black;
Console.ForegroundColor = ConsoleColor.Yellow;
}

thus setting the colours at compile time.

I would like to set the colours at run time and have something like:

void setColours( bgColour, fgColour)
{
Console.BackgroundColor = ConsoleColor .bgColour;
Console.ForegroundColor = ConsoleColor.fgColour;
}

however the compiler does not allow this.

Is it possible to set colours at run time and if so how would i do it?

Thanks


Aug 15 '08 #4
Jon: bingo, that's great. Thanks

"Jon Skeet [C# MVP]" wrote:
Paolo <Pa***@discussions.microsoft.comwrote:
I have the following:

void setColours()
{
Console.BackgroundColor = ConsoleColor .Black;
Console.ForegroundColor = ConsoleColor.Yellow;
}

thus setting the colours at compile time.

I would like to set the colours at run time and have something like:

void setColours( bgColour, fgColour)
{
Console.BackgroundColor = ConsoleColor .bgColour;
Console.ForegroundColor = ConsoleColor.fgColour;
}

however the compiler does not allow this.

Is it possible to set colours at run time and if so how would i do it?

You just need to change your code to give types to the parameters:

void setColours(ConsoleColor bgColour, ConsoleColor fgColour)
{
Console.BackgroundColor = bgColour;
Console.ForegroundColor = fgColour;
}
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 15 '08 #5
Jon: I've changed the code as you suggested. When I use the setColours method
in a class constructor I am inserting the ConsoleColor names i.e. Black,
Yellow as the actual parameters. Is this correct? I am getting an error 'The
name 'Black' does not exist in the current context' (same message for Yellow.)

I don't think this is a scope-related error since I've got similar methods
and parameters in the same Utilities class where the setColours is defined.

"Jon Skeet [C# MVP]" wrote:
Paolo <Pa***@discussions.microsoft.comwrote:
I have the following:

void setColours()
{
Console.BackgroundColor = ConsoleColor .Black;
Console.ForegroundColor = ConsoleColor.Yellow;
}

thus setting the colours at compile time.

I would like to set the colours at run time and have something like:

void setColours( bgColour, fgColour)
{
Console.BackgroundColor = ConsoleColor .bgColour;
Console.ForegroundColor = ConsoleColor.fgColour;
}

however the compiler does not allow this.

Is it possible to set colours at run time and if so how would i do it?

You just need to change your code to give types to the parameters:

void setColours(ConsoleColor bgColour, ConsoleColor fgColour)
{
Console.BackgroundColor = bgColour;
Console.ForegroundColor = fgColour;
}
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 16 '08 #6
Paolo <Pa***@discussions.microsoft.comwrote:
Jon: I've changed the code as you suggested. When I use the setColours method
in a class constructor I am inserting the ConsoleColor names i.e. Black,
Yellow as the actual parameters. Is this correct? I am getting an error 'The
name 'Black' does not exist in the current context' (same message for Yellow.)
You've got to specify the ConsoleColor, e.g.

setColours(ConsoleColor.Black, ConsoleColor.Yellow);

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 16 '08 #7
Jon: many thanks again.

"Jon Skeet [C# MVP]" wrote:
Paolo <Pa***@discussions.microsoft.comwrote:
Jon: I've changed the code as you suggested. When I use the setColours method
in a class constructor I am inserting the ConsoleColor names i.e. Black,
Yellow as the actual parameters. Is this correct? I am getting an error 'The
name 'Black' does not exist in the current context' (same message for Yellow.)

You've got to specify the ConsoleColor, e.g.

setColours(ConsoleColor.Black, ConsoleColor.Yellow);

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 16 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by SpaceCowboy | last post: by
2 posts views Thread by Faisal | last post: by
6 posts views Thread by Rasmus | last post: by
2 posts views Thread by Matt | last post: by
2 posts views Thread by Stan | last post: by
37 posts views Thread by mdh | last post: by
37 posts views Thread by Ian Semmel | last post: by
4 posts views Thread by zdravko.monov | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.