Solved

Passing fixed size array as parameter, ensuring array is proper size

Posted on 2011-09-10
3
274 Views
Last Modified: 2012-05-12
Is there a way to specify the array size when an array is a parameter to a method? e.g.
static void MyMethod(int[] array)
{
    //Array must be exactly 3 elements in size.
}

<example>
int[] array = { -5, -6, -7 };
MyMethod(array);  //OK

int[] array2 = { -5, -6, -7, -8 };
MyMethod(array2); //compiler error

Open in new window

or do I have to construct an entire class consisting of one 3 element int array just so I can ensure the array is the correct size? (I'm guessing I have to go with the class.)
0
Comment
Question by:deleyd
3 Comments
 
LVL 10

Accepted Solution

by:
Irzana earned 500 total points
ID: 36518050
You cannot define the array size for method parameter, inside the method you can check for array size before doing any logic.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36519077
__NO POINTS__

Agreed, check inside the callee.
0
 

Author Closing Comment

by:deleyd
ID: 36520239
But that makes it a runtime error. I was hoping to catch it at compile time.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now