在私有bool c#中使用public static void中的字符串

本文关键字:public static void 字符串 bool | 更新日期: 2023-09-27 18:09:39

所以我试图验证我从MySQL数据库收到的字符串,但由于某种原因无法访问。我知道这是一个愚蠢的问题,但有人能帮我吗?我觉得它可能与公共vs私人和静态有关,但在玩弄了我能想到的每一个组合后,通过变量,它仍然不断给我错误。什么好主意吗?

string failReason = "";
int valid = 0;
public static void getNewRow()
    {
        try
        {
            string getNewRow = "SELECT * FROM appointments WHERE valid IS NULL";
            DbConnection mysqlDB = new DbConnection();
            MySqlCommand mysqlCommand = new MySqlCommand(getNewRow, mysqlDB.GetLocalMySQLConnection());
            MySqlDataReader reader = mysqlCommand.ExecuteReader();
            while (reader.Read())
            {

                    int id = reader.GetInt32("id");
                    string accountID = reader.GetString("accountID");
                    string appDate = reader.GetString("appDate");
                    string appTime = reader.GetString("apptime");
                    string patientName = reader.GetString("patientName");
                    string appPhone = reader.GetString("appPhone");
                    string appPhone2 = reader.GetString("appPhone2");
                    string smsPhone = reader.GetString("smsPhone");
                    string special = reader.GetString("special");
                    string email = reader.GetString("email");
                    string provider = reader.GetString("provider");
                    string location = reader.GetString("location");
                    string other = reader.GetString("other");

                    Console.WriteLine("ID: " + id);
                    Console.WriteLine("AccountID: " + accountID);
                    Console.WriteLine("Appointment Date: " + appDate);
                    Console.WriteLine("Appointment Time: " + appTime);
                    Console.WriteLine("Patient Name: " + patientName);
                    Console.WriteLine("Phone 1: " + appPhone);
                    Console.WriteLine("Phone 2: " + appPhone2);
                    Console.WriteLine("SMS Phone: " + smsPhone);
                    Console.WriteLine("Special: " + special);
                    Console.WriteLine("Email: " + email);
                    Console.WriteLine("Provider: " + provider);
                    Console.WriteLine("Location: " + location);
                    Console.WriteLine("Other: " + other);



            }


        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.InnerException);
        }
    }
    private bool validName()
    {
        if (patientName.Length < 20)
        {
            failReason = "Bad Name";
            return false;
        }
        else
        {
            return true;
        }
    }
private bool validName()
    {
        if (patientName.Length < 20)
        {
            failReason = "Bad Name";
            return false;
        }
        else
        {
            return true;
        }
    }

在私有bool c#中使用public static void中的字符串

patientName只存在于getNewRow的范围内。您需要使其成为类作用域变量(就像您使用failReason一样,如果您想在getNewRow之外的任何地方使用它们)。您也可以将它们作为参数传递,但我没有看到您的方法开始时有参数。

string patientName = reader.GetString("patientName");

是本地的public static void getNewRow()

让它像这样:

static string failReason = "";
static int valid = 0;
static string patientName = string.Empty;
public static void getNewRow()
{
    try
    {
        string getNewRow = "SELECT * FROM appointments WHERE valid IS NULL";
        DbConnection mysqlDB = new DbConnection();
        MySqlCommand mysqlCommand = new MySqlCommand(getNewRow, mysqlDB.GetLocalMySQLConnection());
        MySqlDataReader reader = mysqlCommand.ExecuteReader();
        while (reader.Read())
        {

                int id = reader.GetInt32("id");
                string accountID = reader.GetString("accountID");
                string appDate = reader.GetString("appDate");
                string appTime = reader.GetString("apptime");
                patientName = reader.GetString("patientName");
                string appPhone = reader.GetString("appPhone");
                string appPhone2 = reader.GetString("appPhone2");
                string smsPhone = reader.GetString("smsPhone");
                string special = reader.GetString("special");
                string email = reader.GetString("email");
                string provider = reader.GetString("provider");
                string location = reader.GetString("location");
                string other = reader.GetString("other");

                Console.WriteLine("ID: " + id);
                Console.WriteLine("AccountID: " + accountID);
                Console.WriteLine("Appointment Date: " + appDate);
                Console.WriteLine("Appointment Time: " + appTime);
                Console.WriteLine("Patient Name: " + patientName);
                Console.WriteLine("Phone 1: " + appPhone);
                Console.WriteLine("Phone 2: " + appPhone2);
                Console.WriteLine("SMS Phone: " + smsPhone);
                Console.WriteLine("Special: " + special);
                Console.WriteLine("Email: " + email);
                Console.WriteLine("Provider: " + provider);
                Console.WriteLine("Location: " + location);
                Console.WriteLine("Other: " + other);



        }


    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.InnerException);
    }
}
private static bool validName()
{
    if (patientName.Length < 20)
    {
        failReason = "Bad Name";
        return false;
    }
    else
    {
        return true;
    }
}
private static bool validName()
{
    if (patientName.Length < 20)
    {
        failReason = "Bad Name";
        return false;
    }
    else
    {
        return true;
    }
}

您应该决定是否将所有方法设置为静态。你不能从静态方法中调用非静态方法