C# script: Error 1: Index out of range

Hi there,

I wrote a simple C# code to help me calculate the amount of rainwater that would be stored in a tank after using some of it for irrigation.
Everything seemed fine on the first run, but then I got the annoying 1. Index out of range error. I keep moving things around my code and keep failing to find the answer.

Any chance someone can take a look at it?

Much appreciated.
210716 - HMO.gh (32.7 KB)

Dear @dariocorral just had a very short look at your script
maybe those two questions help:
what is the difference between x,y,i in the for loop. ? they all have the same increment
i++ (starts with 1)
x++ (starts with 1)
y++ (starts with 0)
are you sure you handle the first element = index 0 correct ?


It was not clear what you really wanted to calculate in what manner, so I guessed - a lot. I don’t know if this is what you are after, but here is my version.

210716 - HMO - jesterKing_guessing.gh (28.8 KB)

I sprinkled comments richly throughout my code.

You’ll also find I added a deficit output in the node. Because I couldn’t figure out a better way to tell that there were months where there was not enough rain, nor enough in the tank to meet the irrigation requirement (I assumed requirement meant the amount of water needed for irrigation each month).

You’ll find that with the monthly precipitation the month May runs a deficit.

Anyway, I hope that my code gives you ideas of how to approach your problem.

  private void RunScript(List<double> rainwater, double tank, double requirement, ref object available_water, ref object stored_water, ref object deficit)
    // first seed availableWater with rainwater - this we get each month anyway.
    List<double> availableWater = new List<double>(new double[rainwater.Count]);

    // initialize with 0's, because we start with an empty tank. We'll carry over
    // amounts we can store from previous month - or deduct if we had to use due
    // to less rain than irrigation requirement
    List<double> storedWater = new List<double>(new double[rainwater.Count]);

    // used to record any deficits
    List<double> deficits = new List<double>(new double[rainwater.Count]);

    // Assume all excess is lost, if we got more rain than irrigation requirement
    // and tank is full we don't get more available water. The tank is the limit.

    for(int i = 0; i < rainwater.Count; i++) {
      // assume empty tank at start of sequence (stored water = 0)
      // after that what we had stored in previous month
      double waterFromTank = i == 0 ? 0 : storedWater[i - 1];

      // we have what rained and what we have in tank
      double available = rainwater[i] + waterFromTank;
      // record this amount in our available water list
      if(i < rainwater.Count) {
        availableWater[i] = available;

      // current month available - irrigation
      double waterLeftAfterIrrigation = available - requirement;

      if(waterLeftAfterIrrigation < 0 && i > 0) {
        // we needed more than what we had in tank and what rained.
        // do we blame global warming?
        if(i < rainwater.Count) {
          deficits[i] = waterLeftAfterIrrigation;
        waterLeftAfterIrrigation = 0;
      } else if (waterLeftAfterIrrigation > tank) {
        // we have more water this time around than can fit in the tank
        // should really have procured that bigger tank.
        waterLeftAfterIrrigation = tank;
      // put whatever we have left in the tank.
      if(i < rainwater.Count) {
        storedWater[i] = waterLeftAfterIrrigation;

    // set our outputs to what we calculated.
    available_water = availableWater;
    stored_water = storedWater;
    deficit = deficits;

Hello @Tom_P and sorry for the late answer.

First of all I have to say I have a very basic knowledge of C# or any other programing language.

I used x because I ran a part of the code outside of the loop and saved the result in the storedWater list.

After that inside the loop I intend to add up the previous month storedWater and the current month rainwater. Therefore I used x for the rainwater, since by now x = 1 and y for the storedWater (y = 0).

Regarding the i = 1. I learned that in order to use the for loop I have to include a variable to start the counter, ergo i = 1.

Hope that helps.

Hi @nathanletwory, actually you guess right. I apologize, clearing that out would had helped a lot. There are definitely months where there would be deficits, specially if you change the size of your catchment area, which is all part of the overall task I am attempting to do.

I will carefully review your code. Thanks a lot, this surely will help me a lot.


@nathanletwory I checked the code and it does what I intended. Also I enjoyed the notes. So your guess was right. Learned a lot from it, from how to simplify operations to organization in the code.

I copy some of it and slightly modify the code to show a second possibility in which we first put all the water in the stored tank and then we deduct the requirement. This returns the same result when the tank has enough water, but for the sake of the exercise it is good to have both options.

Thank you again for your help.
210717 - HMO - jesterKing_guessing_V2.gh (30.3 KB)

I’m glad the example helped.

It is a good way to explain your intents while coding. Assuming
that explanation is correct you can always use it to verify your
code actually does what you said it should.

I have started doing that with a set of Rhino Python scripts
at jesterking.github.io/rhipy. Have
a look at how this could work. The latest script I did this way was
how to create a pbr material with textures. The final script is

Anyway, good luck with your own GH definition. I hope it works out :slight_smile:

1 Like

5 posts were split to a new topic: Accessing texture repeat