Handling properties

Discussion in 'Programmer's Corner' started by atferrari, Dec 19, 2008.

  1. atferrari

    Thread Starter AAC Fanatic!

    Jan 6, 2004
    2,648
    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
     
  2. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    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
    Code ( (Unknown Language)):
    1.  
    2. using System;
    3.  
    4. class Example
    5. {
    6.    public static void Main()
    7.    {
    8.       // Get a string array with the names of ConsoleColor enumeration members.
    9.       String[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));
    10.  
    11.       // Display each foreground color except black on a constant black background.
    12.       Console.WriteLine("All the foreground colors (except Black) on a constant black background:");
    13.  
    14.       foreach (string colorName in colorNames)
    15.       {
    16.          // Convert the string representing the enum name to the enum value.
    17.          ConsoleColor color = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), colorName);
    18.  
    19.          if (color == ConsoleColor.Black) continue;
    20.  
    21.          Console.Write("{0,11}: ", colorName);
    22.          Console.BackgroundColor = ConsoleColor.Black;
    23.          Console.ForegroundColor = color;
    24.          Console.WriteLine("This is foreground color {0}.", colorName);
    25.          // Restore the original foreground and background colors.
    26.          Console.ResetColor();
    27.       }
    28.       Console.WriteLine();
    29.  
    30.       // Display each background color except white with a constant white foreground.
    31.       Console.WriteLine("All the background colors (except White) with a constant white background:");
    32.  
    33.       foreach (string colorName in colorNames)
    34.       {
    35.          // Convert the string representing the enum name to the enum value.
    36.          ConsoleColor color = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), colorName);
    37.  
    38.          if (color == ConsoleColor.White) continue;
    39.  
    40.          Console.Write("{0,11}: ", colorName);
    41.          Console.ForegroundColor = ConsoleColor.White;
    42.          Console.BackgroundColor = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), colorName);
    43.          Console.WriteLine("This is background color {0}.", colorName);
    44.          Console.ResetColor();
    45.       }
    46.    }
    47. }
    48. //The example displays the following output:
    49. // All the foreground colors (except Black) on a constant black background:
    50. //    DarkBlue: This is foreground color DarkBlue.
    51. //   DarkGreen: This is foreground color DarkGreen.
    52. //    DarkCyan: This is foreground color DarkCyan.
    53. //     DarkRed: This is foreground color DarkRed.
    54. // DarkMagenta: This is foreground color DarkMagenta.
    55. //  DarkYellow: This is foreground color DarkYellow.
    56. //        Gray: This is foreground color Gray.
    57. //    DarkGray: This is foreground color DarkGray.
    58. //        Blue: This is foreground color Blue.
    59. //       Green: This is foreground color Green.
    60. //        Cyan: This is foreground color Cyan.
    61. //         Red: This is foreground color Red.
    62. //     Magenta: This is foreground color Magenta.
    63. //      Yellow: This is foreground color Yellow.
    64. //       White: This is foreground color White.
    65. //
    66. // All the background colors (except White) with a constant white background:
    67. //       Black: This is background color Black.
    68. //    DarkBlue: This is background color DarkBlue.
    69. //   DarkGreen: This is background color DarkGreen.
    70. //    DarkCyan: This is background color DarkCyan.
    71. //     DarkRed: This is background color DarkRed.
    72. // DarkMagenta: This is background color DarkMagenta.
    73. //  DarkYellow: This is background color DarkYellow.
    74. //        Gray: This is background color Gray.
    75. //    DarkGray: This is background color DarkGray.
    76. //        Blue: This is background color Blue.
    77. //       Green: This is background color Green.
    78. //        Cyan: This is background color Cyan.
    79. //         Red: This is background color Red.
    80. //     Magenta: This is background color Magenta.
    81. //      Yellow: This is background color Yellow.
    82.  
    Hope this is helpful.
    Mark
     
  3. atferrari

    Thread Starter AAC Fanatic!

    Jan 6, 2004
    2,648
    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!!
     
  4. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    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
     
  5. atferrari

    Thread Starter AAC Fanatic!

    Jan 6, 2004
    2,648
    764
    Additional question, Mark:

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

    Gracias.
     
  6. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    "Using" is probably the better choice for describing what you're doing with these properties.
     
  7. atferrari

    Thread Starter AAC Fanatic!

    Jan 6, 2004
    2,648
    764
    OK, noted. Gracias
     
Loading...