Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how do i set a const arraylength value to a method in C#. for example a public method to calculate average of ten double numbers using double array parameters.but not more 10 and not less 10.
Posted
Updated 31-Jan-14 9:06am
v2
Comments
BillWoodruff 31-Jan-14 17:31pm    
Are you talking about passing ten doubles to a method as arguments (parameters, or are you talking about how you use an Array double[10], and make sure it has ten elements ?

OriginalGriff's reply assumes you are passing the doubles as arguments, and Matt's assumes you are using an Array, and just need to enforce its length == 10.

If I understand you want to do something like:
C#
public double AverageOf10(double[10] array)
{ //...
}

and have this:
C#
double[] values = new double[11]{1,2,3,4,5,6,7,8,9,10,11};
double avg = AverageOf10(values);

give a compile time error. (Edit: or even a run time error, but do it automatically.)

If that's what you want, you can't do that.
The best you can do would be to explicitly check the length of the passed array within the method at run-time:
C#
public double AverageOf10(double[] array)
{
  if (array == null)
    throw new ArgumentNullException("array");
  if (array.Length != 10)
    throw new ArgumentException("array is incorrect length", "array");
  //...
}
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 31-Jan-14 23:59pm    
5 Exactly
You have to declare the method with exactly the number of parameters you expect - you can't use params unless it is a truly variable number of parameters. You also can't specify the number of elements in the method declaration for a non-params array (because that would have to be enforced at compile time, and it can't do that).
 
Share this answer
 
Comments
Andreas Gieriet 31-Jan-14 20:38pm    
Who the heck gives a 1 to this answer?
It is concise and correct. Read it carefully! C# does not allow for setting a fix array size in formal parameters, and OriginalGriff tells above what the options are...
So, down-voter, what's wrong with this?
Andi
PS: My 5 for this solution.
If you have this situation at many locations in your code base, you might decide to hide that 10 element constraint into a class, e.g. (assuming some magic precondition check class AssertArgument):
C#
class MyValues
{
    public static readonly int INDEX_CONSTRAINT = 10;
    // pass the exact number of elements (see INDEX_CONSTRAINT). Throws some argument exceptions if passed non-matching elements
    public MyValues(params double[] elements)
    {
       AssertArgument.NotNull(elements, "elements");
       AssertArgument.AreEqual(elements.Length, INDEX_CONSTRAINT, "elements");
       _elements = elements;
    }
    // The array is guaranteed to contain always the same number of elements (see INDEX_CONSTRAINT).
    public double[] Elements { get { return _elements; } }
    private double[] _elements;
}
...
MyMethod(MyValues myValues)
{
    // AssertArgument.AreEqual(myValues.Elements.Length, 10); --> Not needed here anymore since the class guarantees the correct number of Values elements
    ...
    foreach(double d in myValues.Elements)
    {
        ...
    }
    ...
}


Beware: you might also over-do with that approach. This is only meaningful if the MyValue class gets a meaningful abstract name. If you fail to find such a name, the approach might not be useful at all.

Cheers
Andi
 
Share this answer
 
Comments
Amt-Coder 31-Jan-14 20:10pm    
But i want to specify it as method parameter like Averageof10numbers(double[10] numbers). But this method gives error. And it looks like there is no solution for this.thanks:)
Andreas Gieriet 31-Jan-14 20:34pm    
You want to do something the language does not allow. For whatever reason, the designers of C# felt this is not useful (I disagree on that decision, though).
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900