-3

I have declared my array like this,

string[] timeArrivalArray = new string[10];

Now, let's assume that on every click on the add button, something is inserting there.

However I want to use an if condition to a certain index. I want to use something like this

if (timeArrivalArray[] == [2]) {
}

Now whenever I click the button, the timeArrivalArray[] will check if it's already in the 2nd index and then execute the code. tl;dr I just want to use the if condition to the 2nd index, how can this be?

EDIT:
My problem is it's throwing excemption in the first increment. I have a button and something inserting from some variables. However, the code above is inserted in this

if (timeArrivalArray[] == [2]) {
    //*insert variable this*
} else {
    // *insert variable that*
}   

EDIT 2:
Yes it's not the correct syntax and I just know how to do it. But the root of my problem is this

 DateTime theCalcu = DateTime.ParseExact(timeArrivalArray[2], "h:mm tt", System.Globalization.CultureInfo.InvariantCulture);

It's throwing me String reference not set to an instance of a String. Maybe because he can't find any index to the preceding indexes. Any suggestion would do.

sam
  • 931
  • 2
  • 13
  • 26
Fiendcoder1
  • 119
  • 1
  • 1
  • 12

2 Answers2

0

As much as I could comprehend from your post here is the logic I can think of. You can let me know what doesn't work out of it.

string[] timeArrivalArray = new string[10];
public void Button1_Click(object sender, EventArgs e)
        {
            if (timeArrivalArray[2] == null)
            {
                //keep inserting into the array since 2nd index is null
            }
            else if (timeArrivalArray[2] != null && timeArrivalArray[2] == "someInputValue")
            {
                //2nd index is non null and some existing value found  
                //so do something else
            }
            else
            {
                //2nd index isn't null but input value wasn't found so keep inserting
            }
        }
RBT
  • 24,161
  • 21
  • 159
  • 240
  • checking for null is not really useful as the C#'s `new` would have all the array initialized to empty string. – t0mm13b Dec 02 '16 at 01:25
  • 2
    Nope @t0mm13b. They will be initialized to null. string is a reference type. – RBT Dec 02 '16 at 01:26
  • 1
    woop... my bad, still, would be better to use `String.IsNullOrEmpty` [MSDN](https://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx) – t0mm13b Dec 02 '16 at 01:32
  • Yep! I agree to that. `string.IsNullOrEmpty` is a better option if empty string is an invalid input which OP won't want to store in his array. – RBT Dec 02 '16 at 01:36
0

whenever I click the button. The timeArrivalArray[] will check if it's already in the 2nd index[...] I just want to use the if condition to the 2nd index

This sounds to me like you want to count up the index every time a click occurs. If so you should specify an index variable outside of the scope of the click event:

string[] timeArrivalArray = new string[10];

int index = 0;

public void Button1_Click(object sender, EventArgs e)
{   // make sure you are not running out of bounds    
    if(index < timeArrivalArray.Length)
    {    
        if(index == 2)
        {
            timeArrivalArray[index] = //*insert variable this*
        } 
        else 
        {
            // *insert variable that*
        }        
    }

    index ++; // walk to the next position    
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76