我正在尝试找到我之前创建的具有特定值的结构。一旦找到它,我想在该结构上设置变量。我不知道该怎么做。有没有更好的方法呢?也许上课?或结构是否有效?
例如,我的结构:
public struct MyTest
{
public string device;
public string status;
public string revision;
public string number;
public string ledmo;
}
我的测试代码:
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;
答案 0 :(得分:2)
为了能够找到之前创建的对象的实例,需要将这些实例保存在某处 一种解决方案是将它们放入列表中,然后搜索该列表:
var list = new List<MyTest>();
MyTest thisTest=new MyTest();
thisTest.device=blah;
thisTest.number=blah2;
list.Add(thisTest);
MyTest thisTest2=new MyTest();
thisTest2.device=blah5;
thisTest2.number=blah6;
list.Add(thisTest2);
现在您可以使用LINQ搜索:
var foundItems = list.Where(x => x.device == "blah");
foreach(var foundItem in foundItems)
{
foundItem.revision = "blah9999";
}
请注意:
这仅在您使用类而不是结构时才有效,正如Binary Worrier在其评论中指出的那样。
答案 1 :(得分:2)
我会使用一个班级,因为Mutable structs are evil
基本上,因为每个struct
都被复制,即使你找到了正确的结构,你也只会改变一个副本。让我们说MyTest.find
找到thisTest2会发生什么呢
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
要使用类来执行此操作,您需要将您创建的类的每个实例保留在列表或其他数据结构中,因此您知道可以查找它。
如果你这样做,你还需要在完成每个对象时告诉列表,否则它们将永远存在并且永远不会被垃圾收集。
在进一步讨论之前,您确定这是解决此问题的最佳方法吗?
无论如何,假设您的课程为MyData
,您可以在此Create
上放置一个静态工厂方法,该方法会将每个新的MyData对象放入一个列表中。< / p>
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);
}
}
当你完成它时,无论你在哪里使用MyData都需要Delete
。
您的代码变为
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";
现在更改FoundIt也会更改thisTest
P.S。:MyData
之外的任何内容都不能new
MyData
的实例。如果可以的话,那么在AllInstances中找不到MyData
的实例。声明构造函数私有意味着如果MyData外部的代码尝试var someData = new MyData
答案 2 :(得分:0)
您可以使用列表和Linq。
var test = new List<MyTest>();
//Add some items
var foundIt = test.SingleOrDefault(test => test.device == "abc");//Maximum one
if(foundIt != null)//Use a class for MyTest.
foundIt.device = "123"
答案 3 :(得分:0)
在这种情况下,由于动态字符串大小以及存在如此多字符串的事实,类会更好地工作。
使用测试代码,您应该在该类中的某处存储List<MyTest>
,并将thisTest
和thisTest2
添加到列表中。您可以稍后使用FindAll或类似方法检索特定值(或某个device
的所有值)。
List<MyTest> list = new List<MyTest>();
//add MyTests here...
var foundIt = list.FindAll(x => x.device == "blah");