2010-08-30 88 views
-1

我的字典声明如下如何在C#

public static Dictionary<object, DataInfo> DataDic = new Dictionary<object, DataInfo>(); 

    public class DataInfo 
    { 
     public string DSPER;  // Data Sample Period 
     public int TOTSMP;  // Total Samples to be made 
     public int REPGSZ;  // Report Group Size 
     public List<int> IDList; // Array List to collect all the enabled IDs 
    } 

功能InitDataDic下面叫,我怎么能写的重新分配tdi.TOTSMP后的字典.Remove().Add()的代码更新复杂的字典值的某些部分= 0在true条件下以简单的方式。

public void InitDataDic (object objid, DataInfo datainfo, int totsmp) 
{ 
    DataInfo tdi = new DataInfo(); 
    object trid = objid; 
    tdi = datainfo; 

    if (DataDic.ContainsKey(trid) == true) 
    { 
     DataDic.Remove(trid); // here, i mentioned above 
     tdi.TOTSMP = 0; 
     DataDic.Add(trid, tdi); // here, i mentioned above 
    } 
    else 
    { 
     tdi.TOTSMP = topsmp; 
     DataDic.Add(trid, tdi); 
    } 
} 
+0

有没有人建议您致电领域,而不是''DataSamplePeriod' DSPER'(这是不可读和神秘的),同样为所有其他人呢? – Timwi 2010-08-30 07:28:48

+0

@Timwi,简短的'DSPER'来自SEMI E5标准手册。当然这是有点难以理解和神秘的。实际上,它非常好地匹配了标准手册中定义的参数。谢谢。 – 2010-08-31 05:37:15

+0

因此,你永远在别人的错误,而不是主动去解决它,你可以。 – Timwi 2010-08-31 07:40:19

回答

1

如果您想更新字典中的对象(r​​ef型),只需更新对象即可。

if (DataDic.TryGetValue(trid, out tdi) 
{ 
    // already exists in dict, tdi will be initialized with ref to object from dict 
    tdi.TOTSMP = 0; // update tdi 
} 
else 
{ 
    .... 
} 
+0

它工作得很好,谢谢。 – 2010-08-30 07:11:30

+0

最后,我发现了另一个更简单的解决方案,即'DataDic [trid] .TOTSMP = tdi.TOTSMP'来更新存在的字典值。 – 2010-08-31 09:06:12