Enums as parameters

  • Thread starter Thread starter Paolo
  • Start date Start date
P

Paolo

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
 
Paolo said:
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;
}
 
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.
 
Peter: thanks, that explains why I received the compiler error that I did.
 
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.
 
Paolo said:
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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top