473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Parameter restrictions

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
Nov 17 '05 #1
5 2826
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);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
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

Nov 17 '05 #2
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");
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
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);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
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


Nov 17 '05 #3
In message <OE**************@TK2MSFTNGP10.phx.gbl>, Peter Kirk
<pk@alpha-solutions.dk> writes
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.

--
Steve Walker
Nov 17 '05 #4
Hi, thanks, I have still a few more questions...

"Dennis Myrén" <de****@oslokb.no> skrev i en meddelelse
news:el**************@TK2MSFTNGP09.phx.gbl...
/* 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
Nov 17 '05 #5
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.

"Peter Kirk" <pk@alpha-solutions.dk> skrev i en meddelelse
news:%2****************@tk2msftngp13.phx.gbl...
Hi, thanks, I have still a few more questions...

"Dennis Myrén" <de****@oslokb.no> skrev i en meddelelse
news:el**************@TK2MSFTNGP09.phx.gbl...
/* 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

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tom Dearst | last post by:
Gentlemen/Ladies, Is it common for a host to be so restrictive. http://ftphelp.secureserver.net/linux-phpinfo.html Linux http://ftphelp.secureserver.net/win-phpinfo.html Windows GoDaddy is the...
3
by: ruthless | last post by:
hello. I've got a problem - can I do any restrictions for my attributes? I found how to restrict elements and was trying to use them with my attributes e.g. <person sex="F"> sex can be anly...
6
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function...
2
by: Shailendra Batham | last post by:
Hello Gurus, I want to put some restrictions on my attribute tag in my XML Schema, anyone out there have any idea how to do that. here is my XML and the XML Schema <?xml version="1.0"...
1
by: Michael Dovel | last post by:
I am looking for what the server requirements and client restrictions will be for applications run from a web server using web forms. (I have not upgraded yet, am still using VB 6 Professional...
10
by: ypjofficial | last post by:
Hello All, since the programs' stack is shared among all the function inside the program, I was just doing some R&D to see whether the same stack space is used for the variables inside the...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
4
by: mast2as | last post by:
Hi everyone I need to come up with an efficient solution to add/remove/access parameters to an object. Lets say I create an object "Cart" and at run time based on the user input, lets say we add...
1
by: Paul van Brouwershaven | last post by:
Hi All, I'm struggling with the WDSL restrictions in PHP/SOAP for a while know. I would like to create some simple restrictions in my WDSL file. The script are running both on the same server...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.