views:

70

answers:

3

Hello. Vierd behaviar when passing values to and from 2nd form.

ParameterForm pf = new ParameterForm(testString);

works

ParameterForm pf = new ParameterForm();
pf.testString="test";

doesn't (testString defined as public string)

maybe i'm missing something? anyway i'd like to make 2nd variant work properly, as for now - it returns null object reference error.

thanks for help.

posting more code here:

calling

Button ParametersButton = new Button();
ParametersButton.Click += delegate
                {
                    ParameterForm pf = new ParameterForm(doc.GetElementById(ParametersButton.Tag.ToString()));
                    pf.ShowDialog(this);
                    pf.test = "test";
                    pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
                };

definition and use

   public partial class ParameterForm : Form
    {
        public string test;
        public XmlElement node;
        public delegate void ParameterSubmitResult(object sender, XmlElement e);
        public event ParameterSubmitResult Submit;

        public void SubmitButton_Click(object sender, EventArgs e)
        {
            Submit(this,this.node);
            Debug.WriteLine(test);
        }
     }

result: Submit - null object reference test - null object reference

A: 

When you want to use your second variant, you have to use a getString()-Method, where you can put the e.g. "testString". The way you wrote it, "testString" should be a method (and got brackets).

EDIT (a bit more precise):

You could write:

pf.getString(testString);

, if "pf" is an instance of your own class, otherwise you had to look up, whether you can retrieve a String in this class.

As far as you know, testString is a property that has a lowercase first letter for no apparent reason.
R. Bemrose
If the OP is following Microsoft's naming conventions (not very well) then it is probably a public field.
ErikHeemskerk
"defined as public string" seems to imply it's not a property.
Daniel Rasmussen
i assume that Forms are the same as other classes so it only matters to standars and someone's eyes :)
dnkira
Yes, Forms are just like other classes, but just like other classes, there are naming conventions which make it easier for other people to look at your code, and encapsulation principles for keeping your objects from being changed without your knowledge (among other things.)You should look into both of these - it makes your code much more professional, and helps out people (like us) who are trying to help you!
Daniel Rasmussen
thnx for notice, will take that into account
dnkira
+1  A: 
  • pf.ShowDialog(this); is a blocking call, so pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit); is never reached: switch the order.

  • Submit(this,this.node); throws a null object reference because no event is assigned to it (see above). Generally, you should always check first: if (Submit != null) Submit(this,this.node);

  • You should change `pf.ShowDialog(this); to pf.Show(this); so that your main form isn't disabled while your dialog box is open, if that's what you want, or use the model below (typical for dialog boxes.)


I'm not sure what pf_Submit is supposed to do, so this might not be the best way to go about it in your application, but it's how general "Proceed? Yes/No" questions work.

Button ParametersButton = new Button();
ParametersButton.Click += delegate
    {
        ParameterForm pf = new ParameterForm(testString);
        pf.ShowDialog(this); // Blocks until user submits
        // Do whatever pf_Submit did here.
    };

public partial class ParameterForm : Form
{
    public string test;     // Generally, encapsulate these
    public XmlElement node; // in properties

    public void SubmitButton_Click(object sender, EventArgs e)
    {
        Debug.WriteLine(test);
        this.Close(); // Returns from ShowDialog()
    }
 }
Daniel Rasmussen
A: 

the thing was in line order :)

pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);

and

pf.Test = "test";

should have been set before

   pf.ShowDialog(this);

my mistake thingking that parameter can be passed after 2nd form was displayed

thnx for answers

dnkira
If your problem is fixed, you should generally accept that answer, rather than adding it as your own. ;-)
Daniel Rasmussen
a little blind :)
dnkira
No worries. Glad I could help!
Daniel Rasmussen
me too, what i like about such places, even when there is no answer i need it pushes forward to the solution :)
dnkira