GarudaLead
GarudaLead

Reputation: 479

C# Object Constructor with Specific Values

I want to be able to allow only certain values to a custom object method. The first example that came to mind to illustrate the point is the VB msgbox and the specific values that you use for which set of buttons you could choose.

 MsgBox("Message", vbYesNo,"title")

How would I be able to do the same in C# with a custom object?

The method will search a particular area based on the value sent.

 object.method(SearchArea1);
 object.method(SearchArea2);

I want to be able just type SearchArea1 or SearchArea2 (not as strings) just like you would use vbYesNo, vbCancel.

Does that make sense at all?

Upvotes: 0

Views: 76

Answers (2)

Claudio Redi
Claudio Redi

Reputation: 68400

You could define an enum and then use it as parameter for method

public enum SearchArea 
{
    SearchArea1,
    SearchArea2
}

public void method(SearchArea searchArea) 
{
    switch (searchArea)
    {
        case SearchArea.SearchArea1:
            // your logic for SearchArea1
            break;
        case SearchArea.SearchArea2:
            // your logic for SearchArea2
            break;
        default:
            throw new ArgumentException("Logic not implemented for provided search area");
    }
}

Upvotes: 3

ken2k
ken2k

Reputation: 48985

What you're looking for is called an enumeration.

Upvotes: 0

Related Questions