在c#中----如何创建“类对象”作为数组.....? [英] In c#----how to create a "class object" as array.....?

查看:99
本文介绍了在c#中----如何创建“类对象”作为数组.....?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了下面这段代码,但是显示错误就像system.nullreference异常对象没有设置为......等等。



什么我试过了:



i tried this below code but its showing error Like system.nullreference Exception object not set to......etc..

What I have tried:

using System;
class _8__CreatingObjectAsArray
{
   static int i;
    static void Main(string[] args)
    {
        students[] s = new students[5];
        //for (int i = 0; i < s.Lengts; i++)
        //    s[i] = new students[5];
        Console.WriteLine(s.Length);
        for (i = 0; i < s.Length; i++)
            s[i].getvals();
        
        for (i = 0; i < s.Length; i++)
            s[i].display();
    }
}
class students
{
    private int rno, marks;
    private string sname;
    public void getvals()
    {
        Console.WriteLine("enter roll no of student...");
        rno = int.Parse(Console.ReadLine());
        Console.WriteLine("enter name of student...");
        sname = Console.ReadLine();
        Console.WriteLine("enter marks of student...");
        marks = int.Parse(Console.ReadLine());
    }
    public void display()
    {
        Console.WriteLine("roll no = "+rno+"stu name = "+sname+"marks = "+marks);
    }
}

推荐答案

在这一行:

On this line:
s[i].getvals();



..你试图访问学生 -object在数组中,但还没有。此时的数组只包含空值,所以你在这里得到了空引用异常。



我假设你已经注释掉了for循环应该是这样的:


..you're attempting to access a students-object in the array but there is none yet. The array at this point contains nothing but nulls, so you're getting the null-reference exception here.

I assume the for-loop which you've commented out should look like this:

for (int i = 0; i < s.Length; i++)
       s[i] = new students();

(然后将分配一个新的学生 -object每个数组索引。)



旁注:在C#中,类名和方法名应为CamelCase,并作为学生的一个对象代表一个学生,我建议将课程命名为学生。方法:显示 GetVals GetValues

(Which then will assign a new students-object to each array-index.)

Sidenote: In C#, class names and method names should be "CamelCase" and as one object of students represents one student, I suggest to name the class Student instead. And the methods: Display and GetVals or GetValues.


您好,问题是,您刚刚创建了一个用于保存Students类型对象的数组,您没有将学生对象分配给该数组,该数组为空。所以只需通过分配学生对象来实例化数组。在main中更改代码如下:

Hi, the problem is, you just created an array for holding objects of type Students, you didn't assign student objects to that array, the array is empty. so just instantiate the array by assigning student object. change the code as follows inside main,
students[] s = new students[5];
s[0] = new students();
s[1] = new students();
s[2] = new students();
s[3] = new students();
s[4] = new students();
//for (int i = 0; i < s.Lengts; i++)
//    s[i] = new students[5];
Console.WriteLine(s.Length);
for (i = 0; i < s.Length; i++)
    s[i].getvals();

for (i = 0; i < s.Length; i++)
    s[i].display();

Console.ReadLine();


这篇关于在c#中----如何创建“类对象”作为数组.....?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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