English 中文(简体)
C# 每一错误
原标题:C# foreach error
  • 时间:2011-09-18 18:21:07
  •  标签:
  • c#
string connection = "Data Source=" + server + ";Initial Catalog=" + dbase + 
";User ID=" + userid + ";Password=" + password;

conn = new SqlConnection(connection);
conn.Open();
SqlCommand cmd = new SqlCommand("SUBNETTEMP", conn);
cmd.CommandType = CommandType.StoredProcedure;

Parameters d = new Parameters();
d.addparam(15, 1);
d.addparam(15, 2);

foreach (var param in d)
{
    cmd.Parameters.Add(new SqlParameter(param.Key, param.Value));
} 

There s a class called parameters:

class Parameters
{
    Dictionary<int, int> paramids;
    public Dictionary<int, int> addparam(int sid, int sbid)
    {
        if (paramids == null)
            paramids = new Dictionary<int, int>();
        paramids.Add(sid, sbid);
        return paramids;
    }
}

我不敢肯定,为什么我错过了:“每次发言都无法按类型ConsoleApplication1的变量操作。 参数:ConsoleApplication1。 参数并不包含基因组数字的公开定义。

最佳回答

www.un.org/Depts/DGACM/index_french.htm

class Parameters {
    Dictionary<int, int> paramids;
    ....
    public Dictionary<int, int> ParamIDs {
        get { return paramids; }
    }
}

因此,现在这项工作将:

foreach (var param in d.ParamIDs) {
....
}
问题回答

页: 1 同错误一样。

您要么可以在<条码><><>>中的现场进行检索,但对于您来说,需要公开披露,这是海事组织的一个坏想法。

备选案文是执行<条码>GetE numberator,将其转至paramids.GetE amountator:

public IEnumerator<KeyValuePair<int,int>> GetEnumerator()
{
    return paramids.GetEnumerator();
}

您还应加入并实施<代码>。 缩略语


作为副句子:我考虑将内部字典作为<代码>addparam的回报价值,即可疑的设计选择。 如果您希望转播一个流体的APIC/代码,而不是内部字典。 页: 1

另外,我还使用网上命名公约,因此请您使用AddParam而不是addm


So your complete class would be:

class Parameters:IEnumerable<KeyValuePair<int,int>>
{
    Dictionary<int, int> paramids;

    public Parameters AddParam(int sid, int sbid)
    {
        if (paramids == null)
            paramids = new Dictionary<int, int>();
        paramids.Add(sid, sbid);
        return this;
    }

    public IEnumerator<KeyValuePair<int,int>> GetEnumerator()
    {
        return paramids.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

如果您希望使用<代码>参数类别作为字典——从<代码>继承。 Dictionary<int, int>:

class Parameters: Dictionary<int, int>
{
}

但在这种情况下,可能最好只使用<代码>。 Dictionary<int, int>

。 您应重新列出<代码>参数类别(看一幅超编的轨道,以单独分类),并直接使用<代码>Dictionary<>。

另一种选择是,有<条码>参数执行<条码>。

class Parameters
{
    Dictionary<int, int> paramids;
    public Dictionary<int, int> addparam(int sid, int sbid)
    {
        if (paramids == null)
            paramids = new Dictionary<int, int>();
        paramids.Add(sid, sbid);
        return paramids;
    }

    public IEnumerator<KeyValuePair<int, int>> GetEnumerator() 
    { 
        return paramids.GetEnumerator(); 
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签