Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.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将数据分配给WPF中的对象数组# 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Data; 使用System.Windows.Documents; 使用System.Windows.Input; 使用System.Windows.Media; 使用System.Windows.Media.Imaging; 使用System.Windows.Shapes; 使用System.IO; 名称空间车间管理 { /// ///MainWindow.xaml的交互逻辑 /// 公共部分类主窗口:窗口 { 公共类用户 { 公共字符串İsim{set;get;} 公共字符串Borç{set;get;} } 公共类项目 { 公共字符串类似{set;get;} 公共字符串İsim{set;get;} 公共十进制Birim{set;get;} 公共十进制Miktar{set;get;} } 公共主窗口() { this.InitializeComponent(); if(!(File.Exists(“C:\\data\\users”))//软件在此检查文件是否存在 { MessageBox.Show(“错误!!!”; this.Hide(); } 如果(!(File.Exists(“C:\\data\\items”)) { MessageBox.Show(“错误!!!”; this.Hide(); } 字符串dan_in=“”; 斯劳梅因河; int i=0,j=0; 列表项=新列表(); 首先读取字符串; sr_main=File.OpenText(“C:\\data\\items”);//这是我们获取items数据的文件 first_read=sr_main.ReadToEnd(); 字符串[]manip_read=first_read.Split('\t');//在文件中,数据之间只有“/t”。例如“name1/tprice1/tcount1/tdanger1t/name2/tprice2/tcount2/tdanger2”等。 item[]items=new item[300];//这里我声明了一个300项长的数组,这意味着如果有301,软件将崩溃,解决这个问题! foreach(manip_read中的字符串行) { if(i==0)items[j].İsim=line;//此行记录items名称 如果(i==1)项[j].Birim=Convert.ToDecimal(行);//此行保留价格 if(i==2)items[j].Miktar=Convert.ToDecimal(line);//这一行保留了剩余的数量 如果(i==3)项[j].Tehlike=line;//并且此行保持危险级别 i++; if(i==4)//这里循环将数据添加到列表中 { if(items[j].Miktar_C#_Wpf_Arrays_File Io - Fatal编程技术网

C# 无法使用C将数据分配给WPF中的对象数组# 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Data; 使用System.Windows.Documents; 使用System.Windows.Input; 使用System.Windows.Media; 使用System.Windows.Media.Imaging; 使用System.Windows.Shapes; 使用System.IO; 名称空间车间管理 { /// ///MainWindow.xaml的交互逻辑 /// 公共部分类主窗口:窗口 { 公共类用户 { 公共字符串İsim{set;get;} 公共字符串Borç{set;get;} } 公共类项目 { 公共字符串类似{set;get;} 公共字符串İsim{set;get;} 公共十进制Birim{set;get;} 公共十进制Miktar{set;get;} } 公共主窗口() { this.InitializeComponent(); if(!(File.Exists(“C:\\data\\users”))//软件在此检查文件是否存在 { MessageBox.Show(“错误!!!”; this.Hide(); } 如果(!(File.Exists(“C:\\data\\items”)) { MessageBox.Show(“错误!!!”; this.Hide(); } 字符串dan_in=“”; 斯劳梅因河; int i=0,j=0; 列表项=新列表(); 首先读取字符串; sr_main=File.OpenText(“C:\\data\\items”);//这是我们获取items数据的文件 first_read=sr_main.ReadToEnd(); 字符串[]manip_read=first_read.Split('\t');//在文件中,数据之间只有“/t”。例如“name1/tprice1/tcount1/tdanger1t/name2/tprice2/tcount2/tdanger2”等。 item[]items=new item[300];//这里我声明了一个300项长的数组,这意味着如果有301,软件将崩溃,解决这个问题! foreach(manip_read中的字符串行) { if(i==0)items[j].İsim=line;//此行记录items名称 如果(i==1)项[j].Birim=Convert.ToDecimal(行);//此行保留价格 if(i==2)items[j].Miktar=Convert.ToDecimal(line);//这一行保留了剩余的数量 如果(i==3)项[j].Tehlike=line;//并且此行保持危险级别 i++; if(i==4)//这里循环将数据添加到列表中 { if(items[j].Miktar

C# 无法使用C将数据分配给WPF中的对象数组# 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Data; 使用System.Windows.Documents; 使用System.Windows.Input; 使用System.Windows.Media; 使用System.Windows.Media.Imaging; 使用System.Windows.Shapes; 使用System.IO; 名称空间车间管理 { /// ///MainWindow.xaml的交互逻辑 /// 公共部分类主窗口:窗口 { 公共类用户 { 公共字符串İsim{set;get;} 公共字符串Borç{set;get;} } 公共类项目 { 公共字符串类似{set;get;} 公共字符串İsim{set;get;} 公共十进制Birim{set;get;} 公共十进制Miktar{set;get;} } 公共主窗口() { this.InitializeComponent(); if(!(File.Exists(“C:\\data\\users”))//软件在此检查文件是否存在 { MessageBox.Show(“错误!!!”; this.Hide(); } 如果(!(File.Exists(“C:\\data\\items”)) { MessageBox.Show(“错误!!!”; this.Hide(); } 字符串dan_in=“”; 斯劳梅因河; int i=0,j=0; 列表项=新列表(); 首先读取字符串; sr_main=File.OpenText(“C:\\data\\items”);//这是我们获取items数据的文件 first_read=sr_main.ReadToEnd(); 字符串[]manip_read=first_read.Split('\t');//在文件中,数据之间只有“/t”。例如“name1/tprice1/tcount1/tdanger1t/name2/tprice2/tcount2/tdanger2”等。 item[]items=new item[300];//这里我声明了一个300项长的数组,这意味着如果有301,软件将崩溃,解决这个问题! foreach(manip_read中的字符串行) { if(i==0)items[j].İsim=line;//此行记录items名称 如果(i==1)项[j].Birim=Convert.ToDecimal(行);//此行保留价格 if(i==2)items[j].Miktar=Convert.ToDecimal(line);//这一行保留了剩余的数量 如果(i==3)项[j].Tehlike=line;//并且此行保持危险级别 i++; if(i==4)//这里循环将数据添加到列表中 { if(items[j].Miktar,c#,wpf,arrays,file-io,C#,Wpf,Arrays,File Io,这里的问题是,我以前运行过这部分软件,但没有items[300]数组。当时,只有一个item实例,但现在我还需要将它们保存在一个数组中。第一条if语句似乎有错误,该语句试图分配第一项(项[0])的nameİsim值 谢谢你的帮助 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Contro

这里的问题是,我以前运行过这部分软件,但没有items[300]数组。当时,只有一个item实例,但现在我还需要将它们保存在一个数组中。第一条if语句似乎有错误,该语句试图分配第一项(项[0])的nameİsim值

谢谢你的帮助

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;
        }
    }
}
使用动态列表而不是固定长度数组:

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 items=newlist();
使用动态列表而不是固定长度数组:

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 items=newlist();

您没有为元素本身赋值-您需要的地方:

List<item> items = new List<item>();
当前,您只是尝试在不存在的对象上设置属性,即通过空引用

(顺便说一下,我也同意使用
列表
的想法。我还将
重命名为
,以符合.NET命名约定。)

根据您当前的代码,最简单的解决方案可能是:

items[j] = new item();

您没有为元素本身赋值-您需要:

List<item> items = new List<item>();
当前,您只是尝试在不存在的对象上设置属性,即通过空引用

(顺便说一下,我也同意使用
列表
的想法。我还将
重命名为
,以符合.NET命名约定。)

根据您当前的代码,最简单的解决方案可能是:

items[j] = new item();

但我认为我只能使用列表项来操纵项目计数。然后我会尝试,再次感谢,但我认为我只能使用列表项来操纵项目计数。我会再试一次,再次感谢,我相信我的代码不是“debugabble”类型:D无论如何,谢谢:D@gkaykck:是的,我会认真考虑尝试重构代码以提高可读性。例如,只在需要时声明变量,并将方法分解为更小的方法。好吧,我相信我的代码不是“debugabble”类型:D无论如何,谢谢:D@gkaykck:是的,我会认真考虑尝试重构代码以提高可读性。比如只在需要时声明变量,以及将方法分解成更小的方法。