简体   繁体   English

如何访问虚拟类中的内容,以便可以将它们用于MAIN类中的If / Else语句?

[英]How do I access something from a virtual class so I can use them for If/Else statements in the MAIN class?

class Registration
{
    public virtual void Register()
    {
        int Registration_System;

        Console.Write("Registration System");
        Console.Write("\n 1. Add Student" +
                      "\n 2. View Student" +
                      "\n 3. Search" +
                      "\n 4. Edit" +
                      "\n 5. Delete" +
                      "\n 6. Exit");
        Console.Write("\nChoose: "); Registration_System = int.Parse(Console.ReadLine());
    }
}

class Program //Main class
{
    static void Main(string[] args)
    {
        Registration r = new Registration();
        r.Register();
        MSIT it = new MSIT();
        MSCS cs = new MSCS();



    }
}

so basically I need to access Registration_System 's value in order to use them in an if/else system for the Main class: 所以基本上我需要访问Registration_System的值,以便在Main类的if / else系统中使用它们:

if (Registration_System == 1)
{
}
..

I'll be doing this with at least 6 other classes since I missed a couple of lessons. 因为我错过了几节课,所以我将与至少6个其他班级一起做。 So far I just need to set up a skeleton for an If/Else so I can access all the other classes but I can't do that without using Registration_System 's value. 到目前为止,我只需要为If / Else设置一个框架,这样我就可以访问所有其他类,但是如果不使用Registration_System的值就无法做到这一点。 Am I doing it correctly though? 我可以正确执行吗? Is there another way? 还有另一种方法吗?

What about using it as the return value of the Register function, like this: 使用它作为Register函数的返回值怎么样?

public virtual int Register()
{
    ....
    return Registration_System;
}

First of all there is nothing called as Virtual classes in C#. 首先,在C#中没有所谓的虚拟类。 I guess, you meant Virtual methods (?). 我想,您的意思是虚拟方法(?)。 Anyway, if you want to access the value of Registration_System in your Main class, either you'll have to make it a public global variable in the Registration class or will have to encapsulate it in a public property (again in the same Registration class) as mentioned by cpl. 无论如何,如果要访问Main类中的Registration_System的值,则必须将其设置为Registration类中的公共全局变量,或者将其封装在公共属性中(同样在同一Registration类中)如cpl所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在XNA中,如何从另一个类修改主游戏类中的某些内容? - In XNA, how do I modify something in the main game class from another class? 如何最小化else if语句的使用? - how do i minimise the use of else if statements? 如何访问参数并在主程序中使用它们? - How do I access the parameters and use them in the main program? 我如何访问从单独的类到主类的getter和setter(在C#中向下转换) - How do I get access to getters and setters from a separate class to the main class(with downcasting in C#) 如何使 randomString 答案公开,以便我可以从另一个 class 访问它 - How can I make the randomString answer public so i can access it from another class 我如何从除主要的 class 访问 XAML object? - how do I access a XAML object from a class other than main? 我如何在main函数中使用这个类? 字典和索引器(集合) - How do i use this class in the main function? Dictionaries and indexers (Collections) 我可以设置一些东西来使类在卸载时做一些事情吗 - Can I set something up to make a class do something on unload 如何从 c# 中的另一个 class 访问私人列表,以便我可以比较两个列表字段? - How to access private list from another class in c# so that I can compare two list fields? 如何通过了解该类中的变量来访问该自定义类? - How do I access the custom class by knowing a variable from that class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM