当我尝试在C#中调用类时,如何解决我的问题# [英] How Do I Solve My Issue When I Trying To Call Class On Class In C#

查看:55
本文介绍了当我尝试在C#中调用类时,如何解决我的问题#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了2级学生和得分如下:

  public   class 学生
{
public string FirstName {获取; set ; }
public string LastName { get ; set ; }
public int ID { get ; set ; }
public 得分* {span class =code-keyword> get
; set ; }
}
public class 得分
{
public float 数学{ get ; set ; }
public float physic { get ; set ; }
}



我创建一个新的main()就像这样

  static   void  Main( string  [] args)
{
// 得分sc =新得分();
学生st = new Student();

st.ID = 1 ;
st.FirstName = David;
st.LastName = ;
- >(Thiproblem)st.score.Math = 3 ;
- >(Thiproblem)st.score.physic = 5 ;
Console.WriteLine( 数学:{0},st.score.Math);
Console.WriteLine( Physic:{0},st.score.physic) ;
Console.ReadLine();
}



我不知道它是怎么回事。请帮帮我。

解决方案

学生的班级成员得分是一个参考。创建新的Student对象时,默认情况下为 null 。在您可以访问它以将其成员(数学/物理)设置为某个值之前,您必须创建一个得分实例并将其分配给学生的得分成员:

< pre lang =c#> static void Main( string [] args)
{
学生st = 学生();

st.ID = 1 ;
st.FirstName = David;
st.LastName = ;
st.score = new 得分(); // < ---
st.score.Math = 3 ;
st.score.physic = 5 ;
Console.WriteLine( Math:{0},st.score.Math) ;
Console.WriteLine( Physic:{0},st.score.physic) ;
Console.ReadLine();
}


1。当你收到错误时,请发布它以便人们可以更快地做出回应。

2.你得到的错误可能是关于未设置的对象引用,可能。

3.它没有用,因为你还没有实例化分数类。



 ... 
st.score = new 得分(); // 就像你对学生所做的那样
st.score.Math = 3 ;


您创建了一个放置分数的学生。

它实际上并没有包含一个。

所以,要么:

  static   void  Main( string  [] args)
{
学生st = 学生();
st.ID = 1 ;
st.FirstName = David;
st.LastName = ;
st.score = new 得分();
st.score.Math = 3 ;
st.score.physic = 5 ;
Console.WriteLine( Math:{0},st.score.Math) ;
Console.WriteLine( Physic:{0},st.score.physic) ;
Console.ReadLine();
}



或(我认为更好):

  public   class 学生
{
public 学生()
{
得分= 得分();
}
public string FirstName {获得; set ; }
public string LastName { get ; set ; }
public int ID { get ; set ; }
public 得分* {span class =code-keyword> get
; set ; }
}
public class 得分
{
public float 数学{ get ; set ; }
public float physic { get ; set ; }
}



还有其他变种......


I created 2 class Student and Score like this:

public class Student
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int ID { get; set; }
        public Score score { get; set; }
    }
    public class Score
    {
        public float Math { get; set; }
        public float physic { get; set; }
    }


And i create a new main() like this

static void Main(string[] args)
        {
           // Score sc = new Score();
            Student st = new Student();

            st.ID = 1;
            st.FirstName = "David";
            st.LastName = "Chen";
           ->(Thiproblem) st.score.Math = 3;
           ->(Thiproblem) st.score.physic = 5;
            Console.WriteLine("Math:{0}",st.score.Math);
            Console.WriteLine("Physic:{0}", st.score.physic);
            Console.ReadLine();
        }


I don't know how it' didn't work. Help me please.

解决方案

The class-member "score" of "Student" is a reference. When you create a new Student-object, it is null by default. Before you can access it in order to set its members (Math / physic) to some value, you have to create an instance of "Score" and assign it to the Student's "score" member:

static void Main(string[] args)
        {
            Student st = new Student();
 
            st.ID = 1;
            st.FirstName = "David";
            st.LastName = "Chen";
            st.score = new Score();  // <---
            st.score.Math = 3;
            st.score.physic = 5;
            Console.WriteLine("Math:{0}",st.score.Math);
            Console.WriteLine("Physic:{0}", st.score.physic);
            Console.ReadLine();
        }


1. When you get an error, please post it so people can respond more quickly.
2. The error you got would have been something about an object reference not set, likely.
3. It does not work because you have not instantiated the score class.

...
st.score = new Score(); // just like you did with student
st.score.Math = 3;


You created Student to have a "place to put" a Score.
It doesn't actually contain one until you put it there.
So, either:

static void Main(string[] args)
{
    Student st = new Student();
    st.ID = 1;
    st.FirstName = "David";
    st.LastName = "Chen";
    st.score = new Score();
    st.score.Math = 3;
    st.score.physic = 5;
    Console.WriteLine("Math:{0}",st.score.Math);
    Console.WriteLine("Physic:{0}", st.score.physic);
    Console.ReadLine();
}


Or (better in my opinion):

public class Student
{
    public Student()
    {
      score = new Score();
    }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int ID { get; set; }
    public Score score { get; set; }
}
public class Score
{
    public float Math { get; set; }
    public float physic { get; set; }
}


And there are other variations as well...


这篇关于当我尝试在C#中调用类时,如何解决我的问题#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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