将对象列表转换为数组 [英] convert list of object to array

查看:76
本文介绍了将对象列表转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在需要随机创建问题的地方工作,以后再使用它来创建问题纸,所以我要做的是创建代表问题的Gene类,然后创建一个基因类型的列表QuestionList,然后通过循环创建基因对象并将它们存储在列表QuestionList中,现在我需要将列表转换为数组s [],但不能.有人可以帮忙吗?

I am working where I need to create question randomly to use it later to create question paper, so what I have do is creating class Gene which represent question then create a list QuestionList of type gene then I create objects of gene through loop and store them in list QuestionList, now I need to convert the list to array s [] but I can't. Any one can help ?

这是我在外部类中定义的代码,类和列表

here is my code , class and list which I defined in outer class

public class Gene
{
    public string question { get; set; }
    public string CLO { get; set; }
    public string type { get; set; }
    public Gene(string s, string t, string i)
    {
        this.question = s;
        this.type = t;
        this.CLO = i;
    }
  }
List<Gene> QuestionList = new List<Gene>();

然后循环创建位于函数中以与sql建立连接的对象

then the loop to creating object which is located in function to make connection with sql

string s = "select * Question, CLO, Question_Type FROM QuestionBank WHERE (Subject = '" + sub + "') AND (chapter = '" + chapter + "') AND (Question_Type = '" + qt.name + "') ORDER BY RAND() LIMIT = '" + qt.numOfType;
                    SqlCommand cmd = new SqlCommand(s, con);
                    SqlDataReader dr;
                    con.Open();
                    dr = cmd.ExecuteReader();
                    dr.Read();
                    while (dr.Read())
                    {
                        string ques = dr["Question"].ToString();
                        string questype = dr["Question_Type"].ToString();
                        string quesCLO = dr["CLO"].ToString(); 
                        QuestionList.Add(new Gene (ques, questype, quesCLO));
                    }
                     con.Close();
                   }

现在我需要将列表转换为数组

Now I need to convert the list to array

 string[] s = QestionList.ToArray();

我也尝试

 Gene[] s = QestionList.ToArray();

但是它们都不起作用,它向我显示错误消息字段初始化程序无法引用非静态字段方法或属性"?

but none of them work, it show me error message "a field initializer cannot reference the nonstatic field method or property" ?

推荐答案

字段初始化器无法引用非静态字段方法或 财产

a field initializer cannot reference the nonstatic field method or property

问题是您有一个引用非静态字段,方法或属性的字段初始化程序. C#编译器不会允许.

The issue is that you have a field initializer referencing a non-static field, method or property. The C# compiler won't allow that.

一种解决方案是将field移到property:

One solution is to move from a field to a property:

Gene[] s { get { return QestionList.ToArray(); } }

上述缺点是,每当您访问s时,您都在有效地克隆QestionList(即很昂贵).

The downside of above is that whenever you access s you are effectively cloning QestionList (i.e. it is expensive).

另一种方法是将您的字段留在那里:

Another would be to leave your field there:

Gene[] s;

然后将其填充到构造函数中:

and populate it inside your constructor instead:

s = QestionList.ToArray();

在构造函数中执行此操作的好处是克隆仅会发生一次.这也是不利的一面(如果您更改QestionList,则s不会反映出来).

The upside of doing it in the constructor is that the cloning will occur only once. That is also the downside (if you are altering QestionList then s won't reflect that).

这篇关于将对象列表转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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