Home

Enumerations and Classes

 

An Enumeration as a Member Variable 

If you have created an enumeration, it has characteristics that resemble those of an integer type. This allows you to use it as a data type to declare a variable. To create a member variable that is of an enumeration type, follow the same rules as done for the primitive types: the name of the enumeration, followed by the name of the variable, and followed by a semi-colon. Here is an example:

public enum HouseType
{
	Unknown,
	SingleFamily,
	TownHouse,
	Condominium
};

public value class CHouse
{	
public:
	String ^ PropertyNumber;
	String ^ Address;
	String ^ City;
	String ^ State;
	String ^ ZIPCode;
	HouseType TypeOfHome;
	int Bedrooms;
	float Bathrooms;
	Byte Stories;
	bool HasCarGarage;
	int YearBuilt;
	int  Condition;
	__wchar_t Style;
	double Value;
};

In the same way, you can declare as many enumeration variables as you want. After declaring the variable, to initialize it, assign it the desired member of the enumeration. Here is an example:

int main()
{
	CHouse ^ home = gcnew CHouse;

	home->TypeOfHome = SingleFamily;

	. . .

	Console::WriteLine();
	return 0;
}

Once the member variable has been initialized, you can use it as you see fit as we will learn and practice in future lessons. At a minimum, you can pass it to Write() or WriteLine() to display its value. Here is an example:

using namespace System;

public enum HouseType
{
	Unknown,
	SingleFamily,
	TownHouse,
	Condominium
};

public value class CHouse
{	
public:
	String ^ PropertyNumber;
	String ^ Address;
	String ^ City;
	String ^ State;
	String ^ ZIPCode;
	HouseType TypeOfHome;
	int Bedrooms;
	float Bathrooms;
	Byte Stories;
	bool HasCarGarage;
	int YearBuilt;
	int  Condition;
	__wchar_t Style;
	double Value;
};

int main()
{
	CHouse ^ home = gcnew CHouse;

	home->PropertyNumber    = "288635";
	home->Address           = "6808 Lilas Drive";
	home->City              = "Silver Spring";
	home->State             = "MD";
	home->ZIPCode           = "20904";
	home->TypeOfHome        = SingleFamily;
	home->Bedrooms  = 5;
	home->Bathrooms = 1;
	home->Stories           = 3;
	home->HasCarGarage      = true;
	home->YearBuilt         = 1992;
	home->Condition         = 2;      // Good, may need minor repair
	home->Style             = L'M';   // High-rise
	home->Value             = 555825;

	Console::WriteLine(L"Property #:          {0}", home->PropertyNumber);
	Console::WriteLine(L"Address:             {0}", home->Address);
	Console::WriteLine(L"                     {0}", home->City);
	Console::WriteLine(L", ");
	Console::Write(home->State);
	Console::WriteLine(L" ");
	Console::WriteLine(home->ZIPCode);
	Console::WriteLine(L"Type of Home:        ");
	Console::WriteLine(home->TypeOfHome);
	Console::WriteLine(L"Number of Bedrooms:  {0}", home->Bedrooms);
	Console::WriteLine(L"Number of Bathrooms: {0}", home->Bathrooms);
	Console::WriteLine(L"Number of Stories:   {0}", home->Stories);
	Console::WriteLine(L"Has Car Garage:      {0}", home->HasCarGarage);
	Console::WriteLine(L"Year Built:          {0}", home->YearBuilt);
	Console::WriteLine(L"Condition:           {0}", home->Condition);
	Console::WriteLine(L"Style:               {0}", home->Style);
	Console::WriteLine(L"Property Value:      {0}", home->Value);

	Console::WriteLine();
	return 0;
}
You cannot use the data formatting features of the curly brackets such as {0} to display the value of an enumeration.

This would produce:

Property #:          288635
Address:             6808 Lilas Drive
                     Silver Spring, MD 20904
Type of Home:        SingleFamily
Number of Bedrooms:  5
Number of Bathrooms: 1
Number of Stories:   3
Has Car Garage:      True
Year Built:          1992
Condition:           2
Style:               M
Property Value:      555825

Press any key to continue . . .

An Enumeration as a Class

By default, if you declare a variable of an enumeration type in main(), it would get stored in the stack memory and the compiler would be in charge of "cleaning up" after it when the program exits. If you want, you can store the variable in the managed heap so the garbage collector would be in charge of it. To do this, when creating the enumeration, precede its name with struct or class. Here is an example:

public enum class HouseType { Unknown, SingleFamily, TownHouse, Condominium };

After creating the enumeration like this, you can use it as a data type and declare a variable of it.

 Here is an example:

using namespace System;

public enum class HouseType { Unknown, SingleFamily, TownHouse, Condominium };
	
int main()
{
	HouseType hType;

	return 0;
}

You can also use it to create a member variable of a class as we saw in the previous section. This time, you use a different technique to access the members of the enumeration. In reality, and as we will learn when we study static member variables, the members of the enumeration become static. To access a member of the enumeration, you must qualify it. To do this, type the name of the enumeration, followed by the :: operator, followed by the desired member of the enumeration. Here is an example:

using namespace System;

public enum class HouseType
{
	Unknown,
	SingleFamily,
	TownHouse,
	Condominium
};

public value class CHouse
{	
public:
	String ^ PropertyNumber;
	String ^ Address;
	String ^ City;
	String ^ State;
	String ^ ZIPCode;
	HouseType TypeOfHome;
	int Bedrooms;
	float Bathrooms;
	Byte Stories;
	bool HasCarGarage;
	int YearBuilt;
	int  Condition;
	__wchar_t Style;
	double Value;
};

int main()
{
	CHouse ^ home = gcnew CHouse;

	home->PropertyNumber    = L"288635";
	home->Address           = L"6808 Lilas Drive";
	home->City              = L"Silver Spring";
	home->State             = L"MD";
	home->ZIPCode           = L"20904";
	home->TypeOfHome        = HouseType::SingleFamily;
	home->Bedrooms  = 5;
	home->Bathrooms = 1;
	home->Stories           = 3;
	home->HasCarGarage      = true;
	home->YearBuilt         = 1992;
	home->Condition         = 2;      // Good, may need minor repair
	home->Style             = L'M';   // High-rise
	home->Value             = 555825;

	Console::WriteLine(L"Property #:          {0}", home->PropertyNumber);
	Console::WriteLine(L"Address:             {0}", home->Address);
	Console::Write(L"                     {0}", home->City);
	Console::Write(L", ");
	Console::Write(home->State);
	Console::Write(L" ");
	Console::WriteLine(home->ZIPCode);
	Console::Write(L"Type of Home:        ");
	Console::WriteLine(home->TypeOfHome);
	Console::WriteLine(L"Number of Bedrooms:  {0}", home->Bedrooms);
	Console::WriteLine(L"Number of Bathrooms: {0}", home->Bathrooms);
	Console::WriteLine(L"Number of Stories:   {0}", home->Stories);
	Console::WriteLine(L"Has Car Garage:      {0}", home->HasCarGarage);
	Console::WriteLine(L"Year Built:          {0}", home->YearBuilt);
	Console::WriteLine(L"Condition:           {0}", home->Condition);
	Console::WriteLine(L"Style:               {0}", home->Style);
	Console::WriteLine(L"Property Value:      {0}", home->Value);

	Console::WriteLine();
	return 0;
}

Practical LearningPractical Learning: Creating Enumerations

  1. To create a few enumerations, access the Flower.h header file and change it with the following:
     
    #pragma once
    
    public enum class FlowerType
    {
        Roses = 1,
        Lilies,
        Daisies,
        Carnations,
        LivePlant,
        Mixed
    };
    
    public enum class FlowerColor
    {
        Red = 1,
        White,
        Yellow,
        Pink,
        Orange,
        Blue,
        Lavender,
        Mixed
    };
    
    public enum class FlowerArrangement
    {
        Bouquet = 1,
        Vase,
        Basket,
        Mixed
    };
    
    public ref class CFlower
    {
        . . .
    
    };
  2. Save all
 

Previous Copyright © 2006-2016, FunctionX, Inc. Next