-4

(New to C#) I am creating a jagged array form in C# as shown below, then pass it to form 2:

          // Answers jagged array that is declared in form 1
         private Question[][] _answers;                 

The following code is what I am using to create the second form and pass array to it:

        //Code to pass all elements of array to new array in form 2 
        Question[][] DisplayAnswers = new Question[1][];

        //Code for new form2
        ResultsForm resultsForm = new ResultsForm();

        //Code for dialog results from form 2  
        DialogResult dialogResult = resultsForm.ShowDialog();

Am I passing the array correctly?

RahulD
  • 709
  • 2
  • 16
  • 39
  • possible duplicate of [Form TextBox values to Form2 TextBox values](http://stackoverflow.com/questions/14425713/form-textbox-values-to-form2-textbox-values) – Venson Aug 04 '13 at 17:05
  • 1
    I don't see any connection or interaction between `DisplayAnswers`, `resultsForm`, and `_answers`. Do you? How do you *think* your code shown passes the answers array to `resultsForm`? – stakx - no longer contributing Aug 04 '13 at 17:06
  • The form resultsForm will show the answers in the jagged array that the user enters in form1(main form) when they click on the results option. – user2421417 Aug 04 '13 at 18:24
  • @user2421417: That's how you want it to work. But can you explain precisely how those four lines of code (above) achieve this goal (according to your thinking)? I'm not asking this to annoy you; but it would be helpful to know your understanding of that code before formulating an answer. – stakx - no longer contributing Aug 04 '13 at 23:34
  • Form1 jagged array to pass to Form2 -- private Question[][] _answers;. Then on Form1 pass the jagged array to an array declared in Form2, -- Question[][] DisplayAnswers = new Question[1][]; before the ResultsForm is created -- ResultsForm resultsForm = new ResultsForm(); . Finally, DialogResult dialogResult = resultsForm.ShowDialog(); is for the result from clicking the OK button to close the form. – user2421417 Aug 05 '13 at 16:12

2 Answers2

1

I think you mean to do this

    //Code to pass all elements of array to new array in form 2 
    Question[][] DisplayAnswers = new Question[1][];

    //Code for new form2
    ResultsForm resultsForm = new ResultsForm(DisplayAnswers);

    //Code for dialog results from form 2  
    DialogResult dialogResult = resultsForm.ShowDialog();

Keep in mind, you need initalize the second part of the jagged array as well - or it'll throw a null reference exception upon trying to access it.

Vaughan Hilts
  • 2,839
  • 1
  • 20
  • 39
puneet
  • 769
  • 1
  • 9
  • 34
  • if your second form takes the array as argument in constructor then you can do as above – puneet Aug 04 '13 at 17:16
  • Is passing an array from one form at another the same as passing the value of a variable from one form at another? – user2421417 Aug 04 '13 at 18:25
  • yes the variable which we pass can be of any type or class, even some user defined class. You just need to cast it back – puneet Aug 04 '13 at 18:30
  • yes you can pass any variable. The type of variable can be any class, even user defined. You just need to have a custom constructor in the other form. If you are receiving params then you can cast the object to the specific type. – puneet Aug 04 '13 at 18:33
0

You actually have a couple of options, you can actually pass the array in the constructor, provided you've added it to the constructor on resultsForm. Another option is to make the array public static then access it from resultsForm by qualifying it with the name of the main form.

tinstaafl
  • 6,908
  • 2
  • 15
  • 22
  • 1
    Why making it `static`? It will just only create problems if two instances of the form ever open at the same time, and it leads to bad OOP. An instance property on the ResultForm would be fine here. – Alejandro Aug 04 '13 at 17:59
  • I didn't say it was a perfect option just one option. – tinstaafl Aug 04 '13 at 18:32