472,958 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1258
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
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.