向Item 添加数据返回空


using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespaceNetClient
{
    public class KG
    {
        List items = new List();
        public void Test(Item item)
        {
            //当前记录
             item = new Item() { Name = "", Point = "", State = "1" };
            
            if (item.State == "0")
                //筛选记录
                items.ForEach(item2 =>
                {
                    if (item2.ShortName == item.ShortName && item2.State == "0")
                    {
                        item2.State = "1";
                    }

                });
            //添加记录
            items.Add(item);
        }
    }

    public class Item
    {
        /// 
        /// 设备名称
        /// 
        public string Name { get; set; }



        /// 
        /// 设备名称简称,去掉最后两个字符
        /// 
        public string ShortName { get { return Name.Substring(0, Name.Length - 2); } }

        /// 
        /// 设备状态
        /// 
        public string State { get; set; }

        /// 
        /// 设备点号
        /// 
        public string Point { get; set; }
    }
}
KG kg=new KG();
kg.Test(item);  
kg.test向item 添加数据,调试是空的,哪里错了呢?

在您的代码中,有以下问题:

在Test方法中,您将参数item重新实例化,这将导致您传递给方法的原始item实例未被修改。您可以删除此行代码。

在Item类中,如果Name属性为空字符串,则ShortName属性会引发异常。您应该添加一个检查,以确保Name属性不为空字符串。

在Test方法中,您应该在向items列表添加item之前更新其状态,以便您添加的实例具有正确的状态。

下面是更新后的代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
 
namespace NetClient
{
    public class KG
    {
        List<Item> items = new List<Item>();
        public void Test(Item item)
        {
            if (item.State == "0")
            {
                //筛选记录
                items.ForEach(item2 =>
                {
                    if (item2.ShortName == item.ShortName && item2.State == "0")
                    {
                        item2.State = "1";
                    }
                });
            }
            
            //添加记录
            items.Add(item);
        }
    }
 
    public class Item
    {
        private string _name;
        /// 
        /// 设备名称
        /// 
        public string Name
        {
            get { return _name; }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new ArgumentException("Name cannot be null or empty.");
                }
                _name = value;
            }
        }



 
        /// 
        /// 设备名称简称,去掉最后两个字符
        /// 
        public string ShortName { get { return Name.Substring(0, Name.Length - 2); } }
 
        /// 
        /// 设备状态
        /// 
        public string State { get; set; }
 
        /// 
        /// 设备点号
        /// 
        public string Point { get; set; }
    }
}

您可以使用以下代码测试:

Item item = new Item() { Name = "TestItem", Point = "", State = "0" };
KG kg = new KG();
kg.Test(item);

请注意,我将Name属性的值设置为“TestItem”,这样在ShortName属性中使用Substring方法时,不会引发异常。您应该将Name属性设置为您实际使用的值。

修改后的代码,可以尝试一下:

public class KG
{
    List<Item> items = new List<Item>();
    public void Test(Item item)
    {
        if (item == null) {
            item = new Item() { Name = "", Point = "", State = "1" };
        }
            
        if (item.State == "0") {
            items.ForEach(item2 => {
                if (item2.ShortName == item.ShortName && item2.State == "0") {
                    item2.State = "1";
                }
            });
        }
        
        items.Add(item);
    }
}

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^