0

I want to initialize an c# array. I don't know in advance how many values I am going to store on it. I know that this number will be between 0 and 5.

Is this the best approach to do this?

private CompiledRuleMethod CompileRuleMethod(Rule r)
{
  int sizeParameters = 0;
  if (r.Param1 != "") sizeParameters++;
  if (r.Param2 != "") sizeParameters++;
  if (r.Param3 != "") sizeParameters++;
  if (r.Param4 != "") sizeParameters++;
  if (r.Param5 != "") sizeParameters++;
  object[] parameters = new object[sizeParameters];
  if (r.Param1 != "") parameters[0] = r.Param1;
  if (r.Param2 != "") parameters[1] = r.Param2;
  if (r.Param3 != "") parameters[2] = r.Param3;
  if (r.Param4 != "") parameters[3] = r.Param4;
  if (r.Param5 != "") parameters[4] = r.Param5;

  return new CompiledRuleMethod(DelegateFactory.Create(typeof(RuleMethod).GetMethod(r.MethodName)),parameters);
}

I am pretty sure that isn't.

7
  • 2
    Why parameters is a object[] whereas the ParamXs are all strings? Commented Jul 4, 2019 at 6:17
  • Is the ParamX, properties from r, really look alike? Could we use reflexion to find all properties of r then filter, and map the value to an array? With some like foreach(property p in object) Commented Jul 4, 2019 at 6:19
  • what is r? why dont you make a method like "r.size()" that check all those params internally? Commented Jul 4, 2019 at 6:20
  • 1
    Do you really need an array ? If you don't know in advance how many elements will be stored, use a List instead. Commented Jul 4, 2019 at 6:20
  • No, it's not. What exactly are you doing? Commented Jul 4, 2019 at 6:21

2 Answers 2

1

Note that your current code doesn't actually work. If, say, only Param4 is empty, then sizeParameters is 4. And then you create a parameters array of length 4. However, when the execution reaches the last line, it will try to put Param5 into index 4 of the array, causing an IndexOutofRangeException.

It seems like you just want to filter out those params that are empty, and keep the ones that are not empty.

You can first put all of the params you want into an array:

var unfilteredParams = new[] {r.Param1, r.Param2, r.Param3, r.Param4, r.Param5};

And then use Where to filter it:

var filteredParams = unfilteredParams.Where(x => x != "").ToArray();
Sign up to request clarification or add additional context in comments.

4 Comments

.Cast<object>() since we want object[] parameters
@DmitryBychenko Declaring the type of filteredParams as object[] works as well, since ToArray is generic, but I don't see why the OP would want an object array instead.
@Sweeper, you are right. But i am going to ensure that always the parameters are going to be filled in order. I mean, if for example Param3 is empty, Param4 and Param5 will be empty.
Yes, I tested and it works perfectly. Now I have to work on type conversion.
0

Don't use an array. List is more appropriate if you don't know how many items it will need. Then you can use list.Add(r.Param) to append items. Though without more context it's hard to say what is the best approach.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.