Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed Array As A Parameter

Tags:

c#

I have a web service that has an array as a parameter. The problem is I only want to let the user send me an array of 15 entries. Of course if they sent me a bigger array I could write code to only take the first 15, but I dont want to do that. I would like to let the user understand we need 15 and only 15 entries and no more.

The only way I know to initialize an array to a fixed value is instantiate it and I cannot do that as a parameter.

Maybe an array as a parameter isnt the best option? Any Ideas?

like image 538
Nick LaMarca Avatar asked Jul 05 '11 21:07

Nick LaMarca


People also ask

Can you use an array as a parameter?

Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.

How is a fixed size array defined?

A fixed array is an array for which the size or length is determined when the array is created and/or allocated. A dynamic array is a random access, variable-size list data structure that allows elements to be added or removed.

What is a [- 1 in C?

So, a[0] is the element in address 1000 , a[1] is the element in the address 1001 , …, a[57] is the element in the address 1057 , and so on… Definitely, a[i] means element in the address 1000 + i . So, a[-1] means the element in the address 1000 + -1 , which is element in the address 999 .

Do arrays have a fixed size?

Arrays are fixed size. Once we initialize the array with some int value as its size, it can't change.


1 Answers

You can't really control the array length of the input to your operation, but you can add some dynamic validation (i.e., throw an ArgumentException if a user passes an array of a different length). Another option would be to simply have 15 parameters to your operation, but that may not be something too readable if the parameters are related and an array would make more sense.

like image 83
carlosfigueira Avatar answered Sep 22 '22 12:09

carlosfigueira