Parameter restrictions

  • Thread starter Thread starter Peter Kirk
  • Start date Start date
P

Peter Kirk

Hi

in C# is it possible to restrict the values that may be supplied to a
function, like this:

public IList GetList( int param )

where param can only take values 1 - 3, for example. Of course I can do it
in code, but I mean that the client of my function can see he can only
supply 1,2, or 3.

Can I use some sort of "enum" and specify the parameter as this "enum" type?

Also, is it possible to define special values in an interface? For example,
I want to define an interface for my class with the "GetList( int param )"
function - can I define the restricted type for param in this interface, so
that all implemtors can only use this type, and clients can see from the
interface what the possible values are?

Thanks,
Peter
 
Yes, this is a typical scenario where you should use an enumeration instead
of integer values that represent something.
/* prototype */
public enum ListOption
{
Option1 = 1,
Option2 = 2,
Option3 = 3
}
public interface IListProvider ( )
{
IList GetList (
ListOption option
);
}
// OK now use it like this, example:
IListProvider listProvider = new IListProviderImplementation();
IList list = listProvider.GetList(ListOption.Option2);
 
Peter Kirk said:
Hi

in C# is it possible to restrict the values that may be supplied to a
function, like this:

public IList GetList( int param )

where param can only take values 1 - 3, for example. Of course I can do it
in code, but I mean that the client of my function can see he can only
supply 1,2, or 3.

Can I use some sort of "enum" and specify the parameter as this "enum" type?

Yes, exactly that.
Also, is it possible to define special values in an interface? For example,
I want to define an interface for my class with the "GetList( int param )"
function - can I define the restricted type for param in this interface, so
that all implemtors can only use this type, and clients can see from the
interface what the possible values are?

Yes.
 
Sorry,
public interface IListProvider ( )
should of course be
public interface IListProvider

and also, the user might get around the enum restriction by casting an
integer
into ListOption that is not defined in the enumeration(there is nothing to
do about that just hwo it is):
IList list = listProvider.GetList((ListOption) 4);

The solution here is,
in the GetList implementation, check first to see if it is defined and throw
exception if not.
IList IListProvider.GetList ( ListOption option )
{
if (! System.Enum.IsDefined(typeof(ListOption), option))
throw new ArgumentException("Not a valid ListOption value.",
"option");
}
 
Hi, thanks, I have still a few more questions...

Dennis Myrén said:
/* prototype */
public enum ListOption {
Option1 = 1,
Option2 = 2,
Option3 = 3
}

In which file are you defining this enum? I was hoping I could define it in
IListProvider, but that is illegal.

public interface IListProvider ( )
{
IList GetList (
ListOption option
);
}
// OK now use it like this, example:
IListProvider listProvider = new IListProviderImplementation();
IList list = listProvider.GetList(ListOption.Option2);

Thanks again
Peter
 
OK, I got it. I can just define the enum in its own file, it doesn't have to
be part of a class.
Thanks.
 
Back
Top