代码之家  ›  专栏  ›  技术社区  ›  NEWBIE

无法在winform中设置变量

  •  0
  • NEWBIE  · 技术社区  · 11 月前

    出于某种原因,我的Updateform中的变量(包含多个变量)不能设置为任何值[当它收到发送的信息时,它总是出现null(我用多种方式发送了它,所以我100%确定我正在发送我需要的东西)],这很奇怪,因为我之前在我打开的上一个表单上设置了一个字符串,使用了我在这个表单上使用的相同方法。

    代码:

    //This is on the seriepage form
     private void SeriesPageUpdate_Click(object sender, EventArgs e)
     {
         Update update = new Update();
         update.UpdateBaseInformation = UpdateData;//Data taken from Controls in the seriepage form
         update.Show();
         
     }
    //this is on the form that i am trying to set updatebaseinformation on
     public partial class Update : Form
     {
         public DataClass UpdateBaseInformation {  get; set; } //I want to set this to what i got from the other form just once 
         DataAccesObject accesObject = new DataAccesObject();
         public Update() 
         {
             InitializeComponent();
            
             SeriesImage.ImageLocation = UpdateBaseInformation.C_Image;
             SeriesLink.Text = UpdateBaseInformation.C_Link;
             SeriesLocation.Text = UpdateBaseInformation.C_Location;
             SeriesTitle.Text = UpdateBaseInformation.C_Title;
             SeriesRating.Text =UpdateBaseInformation.C_Rating;
             SeriesDescription.Text = UpdateBaseInformation.C_Description;
             if (UpdateBaseInformation.C_Type == "Comic")
             {
                 SeriesTypeComic.Checked = true;
             }
             else if (UpdateBaseInformation.C_Type == "Show")
             {
                 SeriesTypeShow.Checked = true;
             }
             else if (UpdateBaseInformation.C_Type == "Book")
             {
                 SeriesTypeBook.Checked = true;
             }
             else if (UpdateBaseInformation.C_Type == "Movie")
             {
                 SeriesTypeMovie.Checked = true;
             }
             string[] Temporary = accesObject.SplitTagBundle(UpdateBaseInformation.C_Tags);
             for (int i = 0; i < Temporary.Length; i++)
             {
                 SeriesTag.Items.Add(Temporary[i]);
             }
         }
    }
    
    //And this is the code for setting a string in the seriepage form
     private void Mouseclicked(object sender,EventArgs e)
     {
        SeriePage page = new SeriePage();
         page.Title = ControlTitle;
         page.Show();
     }
    
    //And this is the string getting set
    public string Title { get; set; }
    

    它们完全相同,那么为什么一个有效,而另一个无效呢?

    我找不到关于这个的任何东西:(

    1 回复  |  直到 11 月前
        1
  •  2
  •   wohlstad    11 月前

    问题是这条线:

    Update update = new Update();
    

    正在调用类的构造函数 Update 。在此构造函数中,您尝试使用 UpdateBaseInformation 阶级的财产。但在这一点上,它仍然 null (其初始值)。 之所以如此,是因为你只在直线上设置了它 之后 上面的那个:

    update.UpdateBaseInformation = UpdateData;
    

    要解决这个问题,你可以通过 UpdateData 对象到构造函数,并使用它来初始化 更新基础信息 使用前:

    public partial class Update : Form
    {
         public DataClass UpdateBaseInformation {  get; set; } 
         //...
    
         // Default constructor - keep it for the usage of Forms designner:
         public Update()
         {
             InitializeComponent();
         }
    
         // Constructor which accepts DataClass:
         // -----------vvvvvvvvvvvvvvvvvvvv-
         public Update(DataClass updateData) : this()
         {
             // Initialize the property:
             UpdateBaseInformation = updateData;
             // Now UpdateBaseInformation will not be `null` and you can use it:
             SeriesImage.ImageLocation = UpdateBaseInformation.C_Image;
             //...
         }
         //...
    }
    

    当你创造 更新 通过 更新数据 致构造函数:

    private void SeriesPageUpdate_Click(object sender, EventArgs e)
    {
         //-------------------------vvvvvvvvvv--
         Update update = new Update(UpdateData);
         update.Show();
    }
    

    请注意,默认构造函数仍然可用,供窗体设计器使用。接受的构造函数 DataClass 对象在执行其余初始化之前调用它。