在 visual studio 中的 c# 中从 extern/second 文件中获取方法
Get method from extern/second file in c# in visual studio
我正在尝试在另一个文件中获取 make 方法,然后将它们调用到主文件中,这是我第一次尝试这个,我不知道出了什么问题。
我在第二种方法SecondClass
和方法Second
中命名了class。然后在主文件中我写了 SecondClass.Second();
.
这是我在主文件中的代码:
using System;
namespace uppdrag_2.cs
{
class Program
{
static void Main(string[] args)
{
// Menu choices
string[] menuOptions = new string[] {"New customer\t", "New staff\t", "Serivce\t", "Reparation", "Garantie", "End program"};
int menuSelect = 0;
while (true)
{
Console.Clear();
Console.CursorVisible = false;
Console.WriteLine("Hello and welcome! Please choose type of registration:");
if (menuSelect == 0)
{
Console.WriteLine("* " + menuOptions[0] + "<--");
Console.WriteLine(menuOptions[1]);
Console.WriteLine(menuOptions[2]);
Console.WriteLine(menuOptions[3]);
Console.WriteLine(menuOptions[4]);
Console.WriteLine(menuOptions[5]);
}
else if (menuSelect == 1)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine("* " + menuOptions[1] + "<--");
Console.WriteLine(menuOptions[2]);
Console.WriteLine(menuOptions[3]);
Console.WriteLine(menuOptions[4]);
Console.WriteLine(menuOptions[5]);
}
else if (menuSelect == 2)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine(menuOptions[1]);
Console.WriteLine("* " + menuOptions[2] + "<--");
Console.WriteLine(menuOptions[3]);
Console.WriteLine(menuOptions[4]);
Console.WriteLine(menuOptions[5]);
}
else if (menuSelect == 3)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine(menuOptions[1]);
Console.WriteLine(menuOptions[2]);
Console.WriteLine("* " + menuOptions[3] + "<--");
Console.WriteLine(menuOptions[4]);
Console.WriteLine(menuOptions[5]);
}
else if (menuSelect == 4)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine(menuOptions[1]);
Console.WriteLine(menuOptions[2]);
Console.WriteLine(menuOptions[3]);
Console.WriteLine("* " + menuOptions[4] + "<--");
Console.WriteLine(menuOptions[5]);
}
else if (menuSelect == 5)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine(menuOptions[1]);
Console.WriteLine(menuOptions[2]);
Console.WriteLine(menuOptions[3]);
Console.WriteLine(menuOptions[4]);
Console.WriteLine("* " + menuOptions[5] + "<--");
}
var keyPressed = Console.ReadKey();
if (keyPressed.Key == ConsoleKey.DownArrow && menuSelect != menuOptions.Length - 1)
{
menuSelect++;
}
else if (keyPressed.Key == ConsoleKey.UpArrow && menuSelect >= 1)
{
menuSelect--;
}
else if (keyPressed.Key == ConsoleKey.Enter)
{
switch (menuSelect)
{
case 0:
Newcustomer();
break;
case 1:
NewStaff();
break;
case 2:
Service();
break;
case 3:
Reparation();
break;
case 4:
Garantie();
break;
case 5:
EndProgram();
break;
}
}
}
}
public static void Newcustomer(){
Console.WriteLine("You have chosen to registrate a new customer!");
Console.Clear();
//Here I can`t reach the method from the other file
SecondClass.Second();
Console.Clear();
}
public static void NewStaff(){
Console.Clear();
Console.WriteLine("You have chosen to registrer a new staffmember!");
Console.Clear();
}
public static void Service(){
Console.WriteLine("You have chosen to register a service matter!");
Console.Clear();
}
public static void Reparation(){
Console.WriteLine("You have choosen to registrer a reparation matter!");
Console.Clear();
Console.ReadKey();
}
public static void Garantie(){
Console.WriteLine("You have choosen to registrate a garantie matter!");
Console.Clear();
Console.ReadKey();
}
public static void EndProgram(){
Console.Clear();
Console.WriteLine("You have chosen to end the program! \nPress Enter to end...");
Console.ReadKey();
Environment.Exit(1);
}
}
}
这是我在第二个文件中的代码:
using System;
namespace uppdrag_2.cs
{
public static class SecondClass
{
public static void Second()
{
Console.WriteLine("utskrift från andra filen");
}
}
}
提前致谢!
好心的 regrads Johanna
针对“名称‘SecondClass’在当前上下文中不存在”的问题,您可以尝试以下步骤来解决。
首先,我们必须点击“显示潜在修复”,然后选择“在新文件中生成 class‘SecondClass’”。
其次,我们将得到一个名为“SecondClass.cs”的新文件。
最后,我们只需要将代码复制到新文件中即可。
综上所述,主文件中的代码不会再报错了
我正在尝试在另一个文件中获取 make 方法,然后将它们调用到主文件中,这是我第一次尝试这个,我不知道出了什么问题。
我在第二种方法SecondClass
和方法Second
中命名了class。然后在主文件中我写了 SecondClass.Second();
.
这是我在主文件中的代码:
using System;
namespace uppdrag_2.cs
{
class Program
{
static void Main(string[] args)
{
// Menu choices
string[] menuOptions = new string[] {"New customer\t", "New staff\t", "Serivce\t", "Reparation", "Garantie", "End program"};
int menuSelect = 0;
while (true)
{
Console.Clear();
Console.CursorVisible = false;
Console.WriteLine("Hello and welcome! Please choose type of registration:");
if (menuSelect == 0)
{
Console.WriteLine("* " + menuOptions[0] + "<--");
Console.WriteLine(menuOptions[1]);
Console.WriteLine(menuOptions[2]);
Console.WriteLine(menuOptions[3]);
Console.WriteLine(menuOptions[4]);
Console.WriteLine(menuOptions[5]);
}
else if (menuSelect == 1)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine("* " + menuOptions[1] + "<--");
Console.WriteLine(menuOptions[2]);
Console.WriteLine(menuOptions[3]);
Console.WriteLine(menuOptions[4]);
Console.WriteLine(menuOptions[5]);
}
else if (menuSelect == 2)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine(menuOptions[1]);
Console.WriteLine("* " + menuOptions[2] + "<--");
Console.WriteLine(menuOptions[3]);
Console.WriteLine(menuOptions[4]);
Console.WriteLine(menuOptions[5]);
}
else if (menuSelect == 3)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine(menuOptions[1]);
Console.WriteLine(menuOptions[2]);
Console.WriteLine("* " + menuOptions[3] + "<--");
Console.WriteLine(menuOptions[4]);
Console.WriteLine(menuOptions[5]);
}
else if (menuSelect == 4)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine(menuOptions[1]);
Console.WriteLine(menuOptions[2]);
Console.WriteLine(menuOptions[3]);
Console.WriteLine("* " + menuOptions[4] + "<--");
Console.WriteLine(menuOptions[5]);
}
else if (menuSelect == 5)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine(menuOptions[1]);
Console.WriteLine(menuOptions[2]);
Console.WriteLine(menuOptions[3]);
Console.WriteLine(menuOptions[4]);
Console.WriteLine("* " + menuOptions[5] + "<--");
}
var keyPressed = Console.ReadKey();
if (keyPressed.Key == ConsoleKey.DownArrow && menuSelect != menuOptions.Length - 1)
{
menuSelect++;
}
else if (keyPressed.Key == ConsoleKey.UpArrow && menuSelect >= 1)
{
menuSelect--;
}
else if (keyPressed.Key == ConsoleKey.Enter)
{
switch (menuSelect)
{
case 0:
Newcustomer();
break;
case 1:
NewStaff();
break;
case 2:
Service();
break;
case 3:
Reparation();
break;
case 4:
Garantie();
break;
case 5:
EndProgram();
break;
}
}
}
}
public static void Newcustomer(){
Console.WriteLine("You have chosen to registrate a new customer!");
Console.Clear();
//Here I can`t reach the method from the other file
SecondClass.Second();
Console.Clear();
}
public static void NewStaff(){
Console.Clear();
Console.WriteLine("You have chosen to registrer a new staffmember!");
Console.Clear();
}
public static void Service(){
Console.WriteLine("You have chosen to register a service matter!");
Console.Clear();
}
public static void Reparation(){
Console.WriteLine("You have choosen to registrer a reparation matter!");
Console.Clear();
Console.ReadKey();
}
public static void Garantie(){
Console.WriteLine("You have choosen to registrate a garantie matter!");
Console.Clear();
Console.ReadKey();
}
public static void EndProgram(){
Console.Clear();
Console.WriteLine("You have chosen to end the program! \nPress Enter to end...");
Console.ReadKey();
Environment.Exit(1);
}
}
}
这是我在第二个文件中的代码:
using System;
namespace uppdrag_2.cs
{
public static class SecondClass
{
public static void Second()
{
Console.WriteLine("utskrift från andra filen");
}
}
}
提前致谢!
好心的 regrads Johanna
针对“名称‘SecondClass’在当前上下文中不存在”的问题,您可以尝试以下步骤来解决。
首先,我们必须点击“显示潜在修复”,然后选择“在新文件中生成 class‘SecondClass’”。
其次,我们将得到一个名为“SecondClass.cs”的新文件。
最后,我们只需要将代码复制到新文件中即可。
综上所述,主文件中的代码不会再报错了