Handling properties

Thread Starter

atferrari

Joined Jan 6, 2004
4,764
I understand the basics of handling properties from classes declared by yourself using {get, set}.

I would like to know how to handle properties of classes already declared by C#, i.e. System.Console.Foreground.

Could anyone help with an example or two?

Gracias
 

Mark44

Joined Nov 26, 2007
628
I understand the basics of handling properties from classes declared by yourself using {get, set}.

I would like to know how to handle properties of classes already declared by C#, i.e. System.Console.Foreground.

Could anyone help with an example or two?

Gracias
Here's an example straight out of the MSDN documentation for the ForegroundColor property (not Foreground) of the Console class, which is in the System namespace.

Here's a link to the MSDN page: http://msdn.microsoft.com/en-us/library/system.console.foregroundcolor.aspx
Rich (BB code):
using System;

class Example
{
   public static void Main() 
   {
      // Get a string array with the names of ConsoleColor enumeration members.
      String[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));

      // Display each foreground color except black on a constant black background.
      Console.WriteLine("All the foreground colors (except Black) on a constant black background:");

      foreach (string colorName in colorNames)
      {
         // Convert the string representing the enum name to the enum value.
         ConsoleColor color = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), colorName);

         if (color == ConsoleColor.Black) continue;

         Console.Write("{0,11}: ", colorName);
         Console.BackgroundColor = ConsoleColor.Black;
         Console.ForegroundColor = color;
         Console.WriteLine("This is foreground color {0}.", colorName);
         // Restore the original foreground and background colors.
         Console.ResetColor();
      }
      Console.WriteLine();

      // Display each background color except white with a constant white foreground.
      Console.WriteLine("All the background colors (except White) with a constant white background:");

      foreach (string colorName in colorNames)
      {
         // Convert the string representing the enum name to the enum value.
         ConsoleColor color = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), colorName);

         if (color == ConsoleColor.White) continue;

         Console.Write("{0,11}: ", colorName);
         Console.ForegroundColor = ConsoleColor.White;
         Console.BackgroundColor = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), colorName);
         Console.WriteLine("This is background color {0}.", colorName);
         Console.ResetColor();
      }
   }
}
//The example displays the following output:
// All the foreground colors (except Black) on a constant black background:
//    DarkBlue: This is foreground color DarkBlue.
//   DarkGreen: This is foreground color DarkGreen.
//    DarkCyan: This is foreground color DarkCyan.
//     DarkRed: This is foreground color DarkRed.
// DarkMagenta: This is foreground color DarkMagenta.
//  DarkYellow: This is foreground color DarkYellow.
//        Gray: This is foreground color Gray.
//    DarkGray: This is foreground color DarkGray.
//        Blue: This is foreground color Blue.
//       Green: This is foreground color Green.
//        Cyan: This is foreground color Cyan.
//         Red: This is foreground color Red.
//     Magenta: This is foreground color Magenta.
//      Yellow: This is foreground color Yellow.
//       White: This is foreground color White.
// 
// All the background colors (except White) with a constant white background:
//       Black: This is background color Black.
//    DarkBlue: This is background color DarkBlue.
//   DarkGreen: This is background color DarkGreen.
//    DarkCyan: This is background color DarkCyan.
//     DarkRed: This is background color DarkRed.
// DarkMagenta: This is background color DarkMagenta.
//  DarkYellow: This is background color DarkYellow.
//        Gray: This is background color Gray.
//    DarkGray: This is background color DarkGray.
//        Blue: This is background color Blue.
//       Green: This is background color Green.
//        Cyan: This is background color Cyan.
//         Red: This is background color Red.
//     Magenta: This is background color Magenta.
//      Yellow: This is background color Yellow.
Hope this is helpful.
Mark
 

Thread Starter

atferrari

Joined Jan 6, 2004
4,764
Happy New Year!

Immediately after posting I got an overwhelming list of vessels to attend thus my delay in comming back. Today, yes, today, I got time to work out this.

Console.ForegroundColor = ConsoleColor.Red; Now I am starting to grasp how it actually works.


Moreover, I was confused about where to look for. Based on your examples and using the Objects explorer in Visual C# Express 2008 I got things clear. Otherwise I would be still lost. (I am affraid you will hear of me again here.) :(

It is not 'one giant leap for mankind" but a good start anyway, eh?

Gracias for replying!!
 

Mark44

Joined Nov 26, 2007
628
De nada, Agustín,
Happy to be able to help you. Properties are pretty straightforward to work with in C#. You already mentioned in your first post that you understand about "get" and "set" properties, so there's not much more to say about them, other than to keep in mind that you should not try to assign a value to a "set" property that isn't the right type, or to try to store a value from a "get" property into a variable of the wrong type.

Prospero año nuevo!
Mark
 

Thread Starter

atferrari

Joined Jan 6, 2004
4,764
Additional question, Mark:

Is it wrong to say "handling" here or I should have said "using" instead?

Gracias.
 
Top