2010-06-27 31 views
0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 
using System.IO; 
namespace shop_management 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public class user 
     { 
      public string İsim { set; get; } 
      public string Borç { set; get; } 

     } 
     public class item 
     { 
      public string Tehlike { set; get; } 
      public string İsim { set; get; } 
      public decimal Birim { set; get; } 
      public decimal Miktar { set; get; } 

     } 
     public MainWindow() 
     { 
      this.InitializeComponent(); 
      if (!(File.Exists("C:\\data\\users"))) //here the software checks if the files exist or not 
      { 
       MessageBox.Show("Error!!!"); 
       this.Hide(); 
      } 
      if (!(File.Exists("C:\\data\\items"))) 
      { 
       MessageBox.Show("Error!!!"); 
       this.Hide(); 
      } 
      string dan_in=""; 
      StreamReader sr_main; 
      int i = 0,j=0; 
      List<item> list_items = new List<item>(); 
      string first_read; 
      sr_main = File.OpenText("C:\\data\\items"); //this is the file that we take our items data 
      first_read = sr_main.ReadToEnd(); 
      string[] manip_read = first_read.Split('\t'); //in the file, there is only "/t"s between datas. for example "name1/tprice1/tcount1/tdanger1t/name2/tprice2/tcount2/tdanger2" etc. 
      item[] items = new item[300]; //here i declared a 300 items long array, which means the software will crash if there is 301, solve this! 
      foreach (string line in manip_read) 
      { 


       if (i == 0) items[j].İsim = line; //this line keeps record of the items name 
       if (i == 1) items[j].Birim = Convert.ToDecimal(line); // this line keeps the price 
       if (i == 2) items[j].Miktar = Convert.ToDecimal(line); // this line keeps how many left 
       if (i == 3) items[j].Tehlike = line; //and this line keeps the danger level 
       i++; 
       if (i == 4) //here the loop adds the data to list 
       { 
        if (items[j].Miktar < Convert.ToDecimal(items[j].Tehlike) || items[j].Miktar == Convert.ToDecimal(items[j].Tehlike)) dan_in = "!!!"; 
        list_items.Add(new item() { İsim = items[j].İsim, Miktar =items[j].Miktar , Birim=items[j].Birim, Tehlike=dan_in }); 
        dan_in = ""; 
        i = 0; 
        j++; 
       } 

      } 
      grid_items.ItemsSource = list_items; 
     } 
    } 
} 

这里的问题是,我运行这部分软件之前,但没有项目[300]数组。那时,只有一个项目的实例,但现在我还需要将它们保存在一个数组中。似乎尝试分配第一个项目(项目[0])的名称İsim值的第一个if语句存在错误。不能分配数据到WPF中的对象数组与C#

感谢您的帮助

回答

4

你不是元素本身分配的值 - 你需要地方

items[j] = new item(); 

目前你只是想设置适当的绑定一个不存在的对象 - 即通过空引用。

(我会用List<item>还有,顺便的想法一致。我还重新命名itemItem以配合.NET命名约定。)

鉴于当前的代码中,最简单的办法大概是这样的:

if (i == 0) 
{ 
    items[j] = new item(); 
    items[j].İsim = line; 
} 
+0

+1对于'鉴于您当前的代码':-) – 2010-06-27 07:02:10

+0

好吧,我相信我的代码不是“可调试”类型:D 无论如何感谢:D – gkaykck 2010-06-27 07:05:03

+0

@gkaykck:是的,我会认真考虑试图重构你的代码,使其更具可读性。比如只需要在需要时声明变量 - 并将方法分解为更小的变量。 – 2010-06-27 07:06:45

0
item[] items = new item[300]; //here i declared a 300 items long array, which means the software will crash if there is 301, solve this! 

使用动态列表,而不是一个固定长度的数组:

List<item> items = new List<item>(); 
+0

感谢,但它不是一个问题:d – gkaykck 2010-06-27 06:58:22

+1

正确的,但它确实很难读懂你的代码。 – 2010-06-27 06:59:57

+0

但我想我只能使用list_items来操作物品数量。我会尝试,然后再次感谢 – gkaykck 2010-06-27 07:00:42