我在哪里可以存储信息以用于不同的功能

时间:2015-04-13 15:13:42

标签: c# scope

我知道这很傻,但我是初学者,我被要求制作一个程序,接受来自方法GetStudentInfo()的学生信息,并使用另一种方法将其打印出PrintStudentInfo,我得到了一切都差不多完成了,除了我不知道我可以在哪里声明变量。我得到了名字'XXXX' doesn't exist in the current context.,这是代码:

class Program
{

    static void Main(string[] args)
    {

        GetStudentInfo();
        printStudentInfo();

    }
    //Student info ..
    public static void GetStudentInfo()
    {
        Console.WriteLine("Enter Student's first name : ");
        firstname = Console.ReadLine();

        Console.WriteLine("Enter Student's last name : ");
        lastname = Console.ReadLine();

        Console.WriteLine("Enter Student's Address : ");
        address = Console.ReadLine();

        Console.WriteLine("Enter Student's country :");
        country = Console.ReadLine();

        Console.WriteLine("Enter Student's state : ");
        state = Console.ReadLine();

        Console.WriteLine("Enter Student's Telephone number :");
        tele = Console.ReadLine();
    }
 static void printStudentInfo()
    {
        Console.WriteLine("Student name is" + firstname + lastname);
        Console.WriteLine("Prof address is " + address);
        Console.WriteLine("Student country/state is" + country + state);
        Console.WriteLine("Student telephone is " + tele);

    }

2 个答案:

答案 0 :(得分:4)

我建议:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        public class Student
        {
            public string firstname;
            public string lastname;
            public string address;
            public string country;
            public string state;
            public string tele;
        }
        public class StudentProcessor
        {
            //Student info ..
            public Student GetStudentInfo()
            {
                Student s = new Student();
                Console.WriteLine("Enter Student's first name : ");
                s.firstname = Console.ReadLine();

                Console.WriteLine("Enter Student's last name : ");
                s.lastname = Console.ReadLine();

                Console.WriteLine("Enter Student's Address : ");
                s.address = Console.ReadLine();

                Console.WriteLine("Enter Student's country :");
                s.country = Console.ReadLine();

                Console.WriteLine("Enter Student's state : ");
                s.state = Console.ReadLine();

                Console.WriteLine("Enter Student's Telephone number :");
                s.tele = Console.ReadLine();

                return s;
            }
            public void printStudentInfo(Student s)
            {
                Console.WriteLine("Student name is" + s.firstname + s.lastname);
                Console.WriteLine("Prof address is " + s.address);
                Console.WriteLine("Student country/state is" + s.country + s.state);
                Console.WriteLine("Student telephone is " + s.tele);

            }
        }
        static void Main(string[] args)
        {
            StudentProcessor p = new StudentProcessor();
            Student s = p.GetStudentInfo();
            p.printStudentInfo(s);

        }

    }
}

答案 1 :(得分:1)

放:

private static string firstname;
private static string lastname;
private static string address;
private static string country;
private static string state;
private static string tele;
如果你只是想让它起作用,可以在课程(主要方法)中使用

,但是如果你想做得更好,就像Ewan说的那样。

例如:

class Program
{
    private static string firstname;
    private static string lastname;
    private static string address;
    private static string country;
    private static string state;
    private static string tele;
    static void Main(string[] args)
    {

     ....
    }
    ....

}
相关问题