[英]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.