Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 奇怪错误(输入字符串格式不正确。)_C#_Winforms_Exception_Types_Compiler Errors - Fatal编程技术网

C# 奇怪错误(输入字符串格式不正确。)

C# 奇怪错误(输入字符串格式不正确。),c#,winforms,exception,types,compiler-errors,C#,Winforms,Exception,Types,Compiler Errors,这是我的代码: namespace Class_Properties { public partial class Form1 : Form { private string firstHeight1 = ""; public int firstHeight { get { return Convert.ToInt32( firstHeight1 ); }

这是我的代码:

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();
        }
    }
}
然后是另一类:

namespace Class_Properties {
    public partial class Form2 : Form {
        public Form2() {
            InitializeComponent();
            Form1 mainWindow = new Form1();
            this.Height = mainWindow.firstHeight;
        }
    }
}
运行时,我键入
200
作为
textbox2
的值,然后单击
button1
,然后Visual Studio显示以下异常:


如何解决此错误?

当firstHeight1抛出异常时,它的值是多少

您可能需要查看int.TryParse():

如果无法解析该值,它将不会设置
输出
,而不会引发异常

有关int.TryParse的更多信息:

编辑:问题是您正在实例化Form1,而Form1的新实例(来自Form2)中从未设置该值。您应该将Form2中的属性设置为该值

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


抛出异常时firstHeight1的值是多少

您可能需要查看int.TryParse():

如果无法解析该值,它将不会设置
输出
,而不会引发异常

有关int.TryParse的更多信息:

编辑:问题是您正在实例化Form1,而Form1的新实例(来自Form2)中从未设置该值。您应该将Form2中的属性设置为该值

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


因为您的
firstHeight1
String.Empty
并且在
Int
类型中找不到空字符串的等效项。因此出现了错误


在Form2实例中,值仍然是
String.Empty
。首先将其设置为某个值。

因为您的
firstHeight1
String。空的
,并且在
Int
类型中找不到空字符串的等效项。因此出现了错误


在Form2实例中,值仍然是
String.Empty
。首先将其设置为某个值。

Form2
中,当您说:

Form1 mainWindow = new Form1();
this.Height = mainWindow.firstHeight;
您没有访问先前使用过的
表单1
,而是创建了一个全新的表单,其文本框值为空

您应该做的是在创建第二个表单时将高度值传递到该表单中:

public Form2(int height) 
{
   // use height here
}
然后在按钮中单击:

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

Form2
中,当您说:

Form1 mainWindow = new Form1();
this.Height = mainWindow.firstHeight;
您没有访问先前使用过的
表单1
,而是创建了一个全新的表单,其文本框值为空

您应该做的是在创建第二个表单时将高度值传递到该表单中:

public Form2(int height) 
{
   // use height here
}
然后在按钮中单击:

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

你可以这样做

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

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

        return returnValue;
    }
}
然后,当需要值时,只需调用
FirstHeight
属性:

if (FirstHeight.HasValue)
{
    // Access the int value like so:
    int height = FirstHeight.Value;
}
如果不想使用可为null的类型,可以改为:

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;
    }
}

你可以这样做

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

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

        return returnValue;
    }
}
然后,当需要值时,只需调用
FirstHeight
属性:

if (FirstHeight.HasValue)
{
    // Access the int value like so:
    int height = FirstHeight.Value;
}
如果不想使用可为null的类型,可以改为:

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;
    }
}
这就是失败:

        InitializeComponent();
        Form1 mainWindow = new Form1();
        this.Height = mainWindow.firstHeight;  //<--
尽管不可否认,最好只发送您需要的:

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

// called like so from Form1:
var form2 = new Form2(this.firstHeight);
这就是失败:

        InitializeComponent();
        Form1 mainWindow = new Form1();
        this.Height = mainWindow.firstHeight;  //<--
尽管不可否认,最好只发送您需要的:

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

// called like so from Form1:
var form2 = new Form2(this.firstHeight);
你的代码看起来像

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 
    }
} }

您的代码看起来像

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 
    }
}
}

这起作用了。。。所以我必须明白,我只需将
Form1
作为参数传递给
Form2()
…@Victor:我真的建议只将您需要的发送给Form2,而不是Form1本身。如果可以的话,耦合是一种需要避免的习惯,基于您的示例,Form2真的没有必要了解Form1。这很有效。。。所以我必须明白,我只需将
Form1
作为参数传递给
Form2()
…@Victor:我真的建议只将您需要的发送给Form2,而不是Form1本身。如果可以的话,耦合是一种需要避免的习惯,并且基于您的示例,Form2确实没有必要了解Form1。