C# 从一个方法到另一个方法获取变量值?

C# 从一个方法到另一个方法获取变量值?,c#,C#,在这种情况下,我试图将“FileSizeType”的值(一个整数变量)输入到“NomCategine”下的方法中,并将其转换为字符串(注释是这样说的) 我建议使用enum类 public enum Test { Small = 1, Medium = 2, Large = 3, Huge = 4 } 然后,您可以使用 int integer = 1; if (Enum.IsDefined(typeof(Test), integer) { Console.W

在这种情况下,我试图将“FileSizeType”的值(一个整数变量)输入到“NomCategine”下的方法中,并将其转换为字符串(注释是这样说的)


我建议使用enum类

public enum Test
{
    Small = 1,
    Medium = 2,
    Large = 3,
    Huge = 4
}
然后,您可以使用

int integer = 1;
if (Enum.IsDefined(typeof(Test), integer) 
{
  Console.WriteLine((Test)integer). 
}
else 
{
  Console.WriteLine("Bad Integer");
}

output:
Small

查看现有代码,您已经从
ChoisirCategory
返回了
FileSizeType
的值,因此您可以将其捕获到变量中,然后将其传递给
nomcegate
方法以获取类别名称,例如:

int categoryId = ChoisirCategory();
string categoryName = NomCategorie(categoryId);

请注意,还有许多其他的改进(例如,如果用户输入“2”而不是“2”,会发生什么情况),但我认为这些建议可能超出了基于这个问题的范围。

下面是如何简化代码的,如果您将上述两个建议结合起来。我还添加了一个if子句来验证该值是否不高于可用值

static enum AvailableSizes
{
        Small = 1,
        Medium = 2,
        Large = 3,
        Huge = 4
}

static int ChoisirCategory()
{
    int FileSizeType;

GetInput:
    Console.Write("What type do you want: ");
    FileSizeType = Convert.ToInt32(Console.ReadLine());

    // Ensure the value is not higher than expected
    // (you could also check that it is not below the minimum value)
    if (FileSizeType > Enum.GetValues(typeof(AvailableSizes)).Cast<int>().Max());
    {
        Console.WriteLine("Value too high.");
        goto GetInput;
    }

    return FileSizeType;
}


static string NomCategorie(int c)
{
    if (Enum.IsDefined(typeof(AvailableSizes), c) 
    {
        return (AvailableSizes)c; 
    }
    else 
    {
        return "Invalid category";
    }
}
使用此代码,如果输入值高于4,它将输出“Value too high.”并再次提问,直到值不高于4为止

如果用户键入0或负值,则将输出“无效类别”


这可能有助于您决定处理输入错误的位置:在用户输入之后或将数字解析为字符串期间。

您调用该方法并将其作为参数传递,类似于将用户输入的字符串传递给
Convert.ToInt32
方法。您可以执行
string categoryName=nomcegory(choisircegory())Main
方法调用code>。那么,使用参数调用该方法似乎是一个显而易见的选择:
var requestedType=nomcegorie(FileSizeType)
?另外,
nomcegorie
不需要分配
FileType
来返回值,它也可以是(例如)
return“Small”
等等。但是,您已经设置了
choisircegory
以返回
int
,因此您必须将
nomcegate
返回的字符串转换为
int
或返回一个字符串,或者改用
FileType
枚举?
static enum AvailableSizes
{
        Small = 1,
        Medium = 2,
        Large = 3,
        Huge = 4
}

static int ChoisirCategory()
{
    int FileSizeType;

GetInput:
    Console.Write("What type do you want: ");
    FileSizeType = Convert.ToInt32(Console.ReadLine());

    // Ensure the value is not higher than expected
    // (you could also check that it is not below the minimum value)
    if (FileSizeType > Enum.GetValues(typeof(AvailableSizes)).Cast<int>().Max());
    {
        Console.WriteLine("Value too high.");
        goto GetInput;
    }

    return FileSizeType;
}


static string NomCategorie(int c)
{
    if (Enum.IsDefined(typeof(AvailableSizes), c) 
    {
        return (AvailableSizes)c; 
    }
    else 
    {
        return "Invalid category";
    }
}
string categoryStr = NomCategorie(ChoisirCategory());
Console.WriteLinte(categoryStr);  // or do whatever you want with the returned value