1

This is my code:

namespace Class_Properties {
    public partial class Form1 : Form {
        private string firstHeight1 = "";
        public int firstHeight {
            get {
                        return Convert.ToInt32( firstHeight1 );
            }
        }

        public Form1() {
            InitializeComponent();
        }

        private void button1_Click( object sender, EventArgs e ) {
            firstHeight1 = textBox2.Text;

            Form2 secondForm = new Form2();
            secondForm.Show();
        }
    }
}

and then the other class:

namespace Class_Properties {
    public partial class Form2 : Form {
        public Form2() {
            InitializeComponent();
            Form1 mainWindow = new Form1();
            this.Height = mainWindow.firstHeight;
        }
    }
}

When I run, I type 200 as value for textbox2 and click button1, then Visual Studio says the following exception:

enter image description here

What could I do to solve this error?

0

6 Answers 6

1

This is the failure:

        InitializeComponent();
        Form1 mainWindow = new Form1();
        this.Height = mainWindow.firstHeight;  //<--

No matter what you did on the other Form1, it won't show up in this one because it's a new instance so firstHeight == string.Empty and will fail the parse.

You'll have to send the existing Form1 to Form2:

public Form2(Form1 parent)
{
    this.Height = parent.firstHeight;
}

// called like so from Form1:
var form2 = new Form2(this);

Though admittedly, it would be better to send only what you need:

public Form2(int desiredHeight)
{
    this.Height = desiredHeight;
}

// called like so from Form1:
var form2 = new Form2(this.firstHeight);
Sign up to request clarification or add additional context in comments.

2 Comments

this worked... so I have to understand that I can make this work just by passing Form1 as parameter to Form2()...
@Victor: I'd really recommend only sending what you need to Form2 instead of Form1 itself. Coupling is a habit to avoid if you can and based on your example, there's really no need for Form2 to know about Form1.
0

What is the value of firstHeight1 when it throws the exception?

You may want to look at int.TryParse() instead:

int output = 0;
int.TryParse(firstHeight1, out output);
return output;

If it can't parse the value, it won't set output, instead of raising an exception.

More info on int.TryParse: http://msdn.microsoft.com/en-us/library/f02979c7.aspx

EDIT: The problem is you're re-instantiating Form1 and the value is never set in the new instance of Form1, from Form2. You should set a property in Form2 to that value.

class Form2
{
    public int FirstHeight { get; set; }
}

...

Form2 form2 = new Form2();
form2.FirstHeight = this.FirstHeight;
form2.Show();

Comments

0

Since your firstHeight1 is String.Empty and no equivalent for empty string can be found in Int type. Hence the error..

In your Form2 instance, the value is still String.Empty. First set it to some value.

Comments

0

In Form2 when you say:

Form1 mainWindow = new Form1();
this.Height = mainWindow.firstHeight;

You aren't accessing the Form1 you had been using earlier, you're creating a brand new one with an empty textbox value.

What you should be doing is passing the height value into the second form when it is created:

public Form2(int height) 
{
   // use height here
}

and in button click:

Form2 secondForm = new Form2(firstHeight);
secondForm.Show();

Comments

0

You can do something like this.

public int? FirstHeight
{
    get
    {
        int? returnValue = null;

        int temp;
        if (int.TryParse(textBox2.Text, out temp))
        {
            returnValue = temp;
        }

        return returnValue;
    }
}

Then you just call the FirstHeight property when you need the value:

if (FirstHeight.HasValue)
{
    // Access the int value like so:
    int height = FirstHeight.Value;
}

If you don't want to use a Nullable type, you could do this instead:

public int FirstHeight
{
    get
    {
        int returnValue; // Or some other default value you can check against.

        if (!int.TryParse(textBox2.Text, out returnValue))
        {
            // If the call goes in here, the text from the input is not convertable to an int.
            // returnValue should be set to 0 when int.TryParse fails.
        }

        return returnValue;
    }
}

Comments

0

Your code looks like

public int FirstHeight
 {
   get
     {
      double Num;
      bool isNum = double.TryParse(firstheight1, out Num);
      if (isNum)
     {
       return firstheight1;
      }
    else
    {
     return 0; or 
     your message goes here 
    }

} }

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.