473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 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 1276
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
6
by: Rasmus | last post by:
I've been using enum as input parameter to methods in the belief that this was a safe method of restricting the input parameters, but the example below proves me wrong. Is there a way to prevent...
2
by: Matt | last post by:
I have a module I have written that I'd like to use in a number of projets. To save the various users having to tweak the code and recompile it, I'd like to store all the parameters in the...
2
by: Stan | last post by:
Suppose I have this enum public class Flag { Hazardous = 1, Protected = 2 } and I have this webmethod:
37
by: mdh | last post by:
In one of the answers to a K&R exercise, the first couple of lines are: enum loop { NO, YES}; enum loop okloop=YES; I get the first line, but not the second. Sorry about the LOL question. ...
2
by: olympus_mons | last post by:
Hi, I'm just discovering the power of xsd.exe, so maybe I'm doing something wrong. schema files describing requests and responses. So there is an extra xsd file for each response and each...
37
by: Ian Semmel | last post by:
If I have Enum en { i1, i2, i3 } I reckon I should be able to have
4
by: zdravko.monov | last post by:
Hi! I am trying to get several parameters in functions as enum variables. Consider this: namespace Layout { enum type { DOT, NEATO, FDP, TWOPI, CIRCO };
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.