我如何搜索特定的结构值?也许更好的方法? [英] How can I search for a specific struct value? Maybe a better approach?

查看:92
本文介绍了我如何搜索特定的结构值?也许更好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到我之前创建一个结构,有一个特定的值。一旦我找到了,我想在该结构设置变量。我不知道如何做到这一点。是否有这样做的更好的办法?也许类? ?还是应该结构正常工作



例如,我的结构:

 公共结构MyTest的
{
公共字符串设备;
公共字符串状态;
公共字符串修改;
公共串号;
公共字符串ledmo;
}



我的测试代码:

  MyTest的这项测试=新MyTest的(); 
thisTest.device =等等;
thisTest.number = blah2;

MyTest的thisTest2 =新MyTest的();
thisTest2.device = blah5;
thisTest2.number = blah6;

//在我的代码另一部分。
//需要找到MyTest的结构'设备'变量=字符串'嗒嗒'
VAR Foundit = MyTest.find(设备==等等);
Foundit.revision = blah9999;


解决方案

我会使用一个类,因为的可变结构是邪恶



基本上,因为每个结构复制,即使你找到合适的结构,你永远只能改变一个副本。比方说 MyTest.find 找到thisTest2会发生什么情况是这样的。



  VAR Foundit = MyTest.Find(设备==等等); 
//线之上已经取得thisTest2的副本,该副本是FoundIt

Foundit.revision =blah9999;
//你在thisTest2副本修改的修订版本,
//因此thisTest2的内容保持不变

要与你需要让你在列表或其他数据结构创建类的每个实例的类做到这一点,所以你知道你可以看看它。



如果你这样做,你还需要告诉列表中,当你与每个对象完成,否则他们将永远流连和垃圾回收从来没有得到。



我去任何前进一步,你确定这是解决这个问题的最好方法是什么?



总之,说你的类是迈德特,你可以把一个静态的工厂方式上这家名为创建,它会把每个新。迈德特对象到一个列表

 公共类迈德特
{
私人静态列表<迈德特> allMyDatas =新的List<&迈德特GT;();
公共静态的IEnumerable<&迈德特GT; AllInstances
{
{返回allMyDatas;}
}

公共字符串设备{搞定;设置;}
公共字符串状态{搞定;设置;}
公共字符串修改{搞定;设置;}
公共串号{搞定;设置;}
公共字符串Ledmo {搞定;设置;}

私人迈德特()//构造函数私有只保证会员
{//函数可以创建一个新的迈德特
}
公共静态MyData的创建( )
{
变种newData =新迈德特();
allMyDatas.Add(newData);
返回newData;
}

公共静态无效删除(迈德特itemToRemove)
{
allMyDatas.Remove(itemToRemove);
}
}



无论你使用迈德特你需要删除它,当你完成它。



您的代码变得

  VAR这项测试= MyData.Create(); 
thisTest.Device =嗒嗒;
thisTest.Number =blah2;

VAR thisTest2 = MyData.Create();
thisTest2.Device =blah5;
thisTest2.Number =blah6;

//在我的代码另一部分。
//需要找到迈德特结构'设备'变量=字符串'嗒嗒'
VAR Foundit = MyData.AllInstances.FirstOrDefault(MD = GT; md.Device ==嗒嗒);
如果(!Foundit = NULL)
Foundit.Revision =blah9999;

更改FoundIt现在也改变了这项测试



PS:这一点很重要外迈德特能 MyData的实例。如果能,那么就不会有迈德特的一个实例,你不能在AllInstances找到。声明构造私有意味着,如果MyData的外部的代码试图像 VAR someData =新迈德特


将产生一个编译器错误

I'm trying to find a struct I created earlier that has a specific value. Once I found it, I want to set variables on that struct. I don't know how to do this. Is there a better way of doing this? Maybe classes? Or should structs work?

For example, my struct:

public struct MyTest
{
    public string device;
    public string status;
    public string revision;
    public string number;
    public string ledmo;        
}

My Test Code:

MyTest thisTest=new MyTest();
thisTest.device=blah;
thisTest.number=blah2;

MyTest thisTest2=new MyTest();
thisTest2.device=blah5;
thisTest2.number=blah6;

//Another Part in my code.
//Need to find the MyTest Structure that 'device' variable = the string 'blah'
var Foundit=MyTest.find(device==blah);
Foundit.revision=blah9999;

解决方案

I'd use a class, because Mutable structs are evil

Basically, because every struct is copied, even if you do find the right struct, you'll only ever change one copy. Lets say MyTest.find finds thisTest2 what happens is this

var Foundit = MyTest.Find(device==blah); 
// The line above has made a copy of thisTest2, that copy is in FoundIt

Foundit.revision = "blah9999";
// You've changed revision in the copy of thisTest2, 
// therefore the contents of thisTest2 remain unchanged

To do this with a class you'll need to keep every instance of the class you create in a list or other data structure, so you know you can look it up.

If you do this you also need to tell the list when you're finished with each object, otherwise they'll hang around forever and never get garbage collected.

Before I go any further, are you sure this is the best way to solve this problem?

Anyway, say your class is MyData, you can put a static factory method on this called Create, which will put each new MyData object into a list.

public class MyData
{
    private static List<MyData> allMyDatas = new List<MyData>();
    public static IEnumerable<MyData> AllInstances
    {
        get {return allMyDatas;}
    }

    public string Device {get; set;}
    public string Status {get; set;}
    public string Revision {get; set;}
    public string Number {get; set;}
    public string Ledmo {get; set;}

    private MyData() // Private ctor ensures only a member 
    {                // function can create a new MyData
    }
    public static MyData Create()
    {
        var newData = new MyData();
        allMyDatas.Add(newData);
        return newData;
    }

    public static void Delete(MyData itemToRemove)
    {
        allMyDatas.Remove(itemToRemove);
    }
}

Everywhere you use a MyData you'll need to Delete it when you're finished with it.

Your code becomes

var thisTest = MyData.Create();
thisTest.Device = "blah";
thisTest.Number = "blah2";

var  thisTest2 = MyData.Create();
thisTest2.Device = "blah5";
thisTest2.Number = "blah6";

//Another Part in my code.
//Need to find the MyData Structure that 'device' variable = the string 'blah'
var Foundit = MyData.AllInstances.FirstOrDefault(md => md.Device == "blah");
if(Foundit != null)
    Foundit.Revision = "blah9999";    

Changing FoundIt now also changes thisTest

P.S.: It's important that nothing outside MyData can new an instance of MyData. If it could, then there would be an instance of MyData that you couldn't find in AllInstances. Declaring the constructor private means a compiler error will be generated if code outside MyData tries something like var someData = new MyData

这篇关于我如何搜索特定的结构值?也许更好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆