Solved

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

Posted on 2011-09-10
3
275 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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