0

I need to insert a string (from one window(QueryBuilder)) into an array(of another window(Main)).

In the Main i have a method as

public void DisplayCalcQuery(string argFromQueryBuilder)
{ 
    int itemsInUserBuiltQueries = UserBuiltQueries.Count();
    UserBuiltQueries[itemsInUserBuiltQueries] = argFromQueryBuilder.ToString();
    //displayng the user built query(queries) on the stack panel meant to display it.
    foreach (string query in UserBuiltQueries)
    {
        CheckBox checkQueries = new CheckBox() { Content = query };
        stackPanel1.Children.Add(checkQueries);
        checkboxes.Add(checkQueries);
    }
}

Where UserBuiltQueries is declared as

string[] UserBuiltQueries; 

However when from the other window i do

 backscreen.DisplayCalcQuery(ttextBox1.Text.ToString()); //where backscreen is the Main

The argument is passed well but i get an error as

{"Value cannot be null.\r\nParameter name: source"}

What did I do wrong ?

6
  • 3
    have you initialized? Commented Dec 5, 2013 at 9:03
  • It seams does not initialized yet. Commented Dec 5, 2013 at 9:05
  • 2
    please add stacktrace? Commented Dec 5, 2013 at 9:05
  • After initializing, UserBuiltQueries[itemsInUserBuiltQueries - 1] ? Commented Dec 5, 2013 at 9:06
  • @AnoushkaSeechurn On what line is the error? Commented Dec 5, 2013 at 9:09

3 Answers 3

1

These lines are wrong

int itemsInUserBuiltQueries = UserBuiltQueries.Count();
UserBuiltQueries[itemsInUserBuiltQueries] = argFromQueryBuilder.ToString();

Arrays start at index zero and end at index (Count - 1), so, if UserBuiltQueries.Count() returns 10 you could use indexes from 0 to 9. Essentially, using index 10, you are adding a new string outside the end of the array.

However, if your requirements force you to expand the array, it is better and more easy to code if you use a List<string> instead. Adding new elements will be a lot more easier and you could still use the List as an Array for common tasks.

    List<string> UserBuiltQueries = new List<string>();

    .....

    public void DisplayCalcQuery(string argFromQueryBuilder)
    { 
        UserBuiltQueries.Add(argFromQueryBuilder);

        //displayng the user built query(queries) on the stack panel meant to display it.
        foreach (string query in UserBuiltQueries)
        {
            CheckBox checkQueries = new CheckBox() { Content = query };
            stackPanel1.Children.Add(checkQueries);
            checkboxes.Add(checkQueries);
        }
    }

By the way, you should stop to unnecessarily convert a string to a string. You pass a ttextBox1.Text.ToString() but ttextBox1.Text is already a string. Inside the method the parameter argFromQueryBuilder is already a string and there is no need to convert to a string

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of using string[] for UserBuildQueries, use List. When you need it as an array, you can simply say: UserBuildQueries.ToArry()

Rewrite the function to

public void DisplayCalcQuery(string argFromQueryBuilder)
{ 
  UserBuiltQueries.Add(argFromQueryBuilder.ToString());
  //displayng the user built query(queries) on the stack panel meant to display it.
  foreach (string query in UserBuiltQueries)
  {
     CheckBox checkQueries = new CheckBox() { Content = query };
     stackPanel1.Children.Add(checkQueries);
     checkboxes.Add(checkQueries);
  }
}

Comments

0

In c# but I think in all programming language indexis start from 0:

so if an array has length or count =1 the index is 0 array[0], array.lenght==1

int itemsInUserBuiltQueries = UserBuiltQueries.Count()-1;
UserBuiltQueries[itemsInUserBuiltQueries] = argFromQueryBuilder.ToString();

And double check that your array is initialized before using it!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.