| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6260 人关注过本帖, 1 人收藏
标题:c#中如何向动态数组添加结构体作为元素?
只看楼主 加入收藏
qianwantao
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-10-4
结帖率:100%
收藏(1)
已结贴  问题点数:20 回复次数:5 
c#中如何向动态数组添加结构体作为元素?
在一个博客上(http://)看到可以给动态数组添加对象作为元素,只复制了几句,前面定义过类student:
 ArrayList students = new ArrayList();     
 //实例化几个Student类对象     
Student rose = new Student("rose",25,"reading");     
 Student jack = new Student("jack",28,"singing");     
 Student mimi = new Student("mimi",26,"dancing");     
 //利用ArrayList类的add()方法添加元素     
students.add(rose);     
students.add(jack);     
 students.add(mimi);

现在我用同样思路给动态数组添加结构体变量作为元素,结果出错,如下:   
struct student        
{string name;            
 int math;            
 int english;              
string address;              
bool  IsPartyNumber;
 };        
 student a={"xiaoming",90,80,"xian",false },                 
b={"liming",85,85,"ankang",true },                 
c={"juan",78,74,"henan",false },                 
d={"feng",85,40,"jiangxi",true };                 
ArrayList students = new ArrayList();         
students.Add(a);         
students.Add(b);         
students.Add(c);         
students.Add(d);  
最后这四句出错,为什么?c#中如何向动态数组添加结构体作为元素?
   



[ 本帖最后由 qianwantao 于 2011-10-4 17:36 编辑 ]
搜索更多相关主题的帖子: singing dancing reading 如何 
2011-10-04 17:34
哦是菜鸟
Rank: 2
等 级:论坛游民
帖 子:40
专家分:15
注 册:2011-9-14
收藏
得分:10 
  struct student        
        {
           public string name;
           public int math;
           public int english;
           public string address;
           public bool IsPartyNumber;
        };        

        private void Form1_Load(object sender, EventArgs e)
        {
            student a = new student();
            a.name = "";
            ArrayList students = new ArrayList();
            students.Add(a);
            ///这样就行了。。。。。你看下你查到的是用什么语言写的语法跟C#不一样
        }
2011-10-05 00:00
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:5 
程序代码:
struct student       
{string name;            
int math;            
int english;             
string address;             
bool  IsPartyNumber;
};  //C#结构体没有分号的     
student a={"xiaoming",90,80,"xian",false },                
b={"liming",85,85,"ankang",true },                
c={"juan",78,74,"henan",false },                
d={"feng",85,40,"jiangxi",true };      
//C++的写法是不对的,改下吧         

   唯实惟新 至诚致志
2011-10-05 00:10
serious
Rank: 6Rank: 6
等 级:侠之大者
威 望:1
帖 子:81
专家分:497
注 册:2009-8-18
收藏
得分:0 
你不应该用"ArrayList"。
在C#中,你应该可以写:

程序代码:
static void Main()
{
    Student jack = null;

    IList<Student> list = new List<Student>
    {
        new Student("rose", 25, "reading"),
        (jack = new Student("jack", 28, "singing")),
        new Student("mimi", 26, "dancing")
    };
   
    ...
}

2011-10-05 05:55
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:5 
看楼主的例子,是想将结构体放入到集合中,在C#中,结构体的用法跟类的用法是相同的,看下面代码
程序代码:
    public struct student
    {
        public string name;
        public int math;
        public int english;
        public string address;
        public bool IsPartyNumber;
    }

    class Program
    {
        static void Main(string[] args)
        {
            student a = new student();
            a.name = "xiaoming";
            a.math = 90;
            a.english = 80;
            a.address = "xian";
            a.IsPartyNumber = false;

            student b = new student();
            b.name = "liming";
            b.math = 85;
            b.english = 85;
            b.address = "ankang";
            b.IsPartyNumber = true;

            student c = new student();
            c.name = "juan";
            c.math = 78;
            c.english = 74;
            c.address = "henan";
            c.IsPartyNumber = false;

            student d = new student();
            d.name = "feng";
            d.math = 85;
            d.english = 40;
            d.address = "jiangxi";
            d.IsPartyNumber = true;

            ArrayList students = new ArrayList();
            students.Add(a);
            students.Add(b);
            students.Add(c);
            students.Add(d);
        }
    }

2011-10-05 13:10
jeanine123
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:65
专家分:192
注 册:2011-9-17
收藏
得分:0 
学习一下。
2011-10-05 16:59
快速回复:c#中如何向动态数组添加结构体作为元素?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041748 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved