0

true か false かをチェックし、口頭で他のクラスに送信する関数を作成できますか?

私は試した:

public class Func
{
    public static bool CheckDate(string number)
    {
        string new_number = number.ToString();
        if (new_number.Length==8)
        {
           string yyyy = new_number.Substring(0, 4);
           string mm = new_number.Substring(4, 2);
           string dd = new_number.Substring(6, 2);
           return true;
        }
        else
        {
            return false;
        }
    }
}

yyyy口頭で , mm,ddProgram.csクラスに送りたいです。

私は何をすべきか?

4

3 に答える 3

7

車輪を再発明しないでくださいDateTime.TryParseExact。この目的のために特別に構築された方法を使用してください。.NET フレームワークで日付を扱うときは、正規表現と部分文字列を忘れてください。

public static bool CheckDate(string number, out DateTime date)
{
    return DateTime.TryParseExact(number, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
}

ご覧のとおり、CheckDateは既に BCL に存在するため、 を定義しても意味がありません。次のように簡単に使用できます。

string number = "that's your number coming from somewhere which should be a date";
DateTime date;
if (DateTime.TryParseExact(
    number, 
    "dd/MM/yyyy", 
    CultureInfo.InvariantCulture, 
    DateTimeStyles.None, 
    out date
))
{
    // the number was in the correct format 
    // => you could use the days, months, from the date variable which is now a DateTime

    string dd = date.Day.ToString();
    string mm = date.Month.ToString();
    string yyyy = date.Year.ToString();
    // do whatever you intended to do with those 3 variables before
}
else
{
    // tell the user to enter a correct date in the format dd/MM/yyyy
}

アップデート:

コメント セクションで、実際には質問に答えていないという発言を受け取ったので、私が推奨するアプローチと同様のアプローチを使用できます。ただし、TryXXX パターンを説明するためだけに、このようなコードを書くことは絶対にないと約束してください。

モデルを定義します。

public class Patterns
{
    public string DD { get; set; }
    public string MM { get; set; }
    public string YYYY { get; set; }
}

次に、CheckDate メソッドを変更して、out パラメーターを送信するようにします。

public static bool CheckDate(string number, out Patterns patterns)
{
    patterns = null;
    string new_number = number.ToString();
    if (new_number.Length == 8)
    {
       Patterns = new Patterns
       {
           YYYY = new_number.Substring(0, 4),
           MM = new_number.Substring(4, 2),
           DD = new_number.Substring(6, 2)
       }
       return true;
    }
    else
    {
        return false;
    }
}

次のように使用できます。

string number = "that's your number coming from somewhere which should be a date";
Patterns patterns;
if (CheckDate(numbers, out patterns)
{
    string dd = patterns.DD;
    string mm = patterns.MM;
    string yyyy = patterns.YYYY;
    // do whatever you intended to do with those 3 variables before
}
else
{
    // tell the user to enter a correct date in the format dd/MM/yyyy
}
于 2013-01-02T08:30:53.943 に答える
0

関数のCheckDate目的は、日付が有効かどうかを確認することです。くだらない副作用を持ち込まないでください。必要なオブジェクトに実際に送信する別の関数を作成してください。

文字列が日付かどうかを確認したい場合は、 で行いますCheckDate

文字列が日付であることがわかっている場合は、そのような関数を使用して必要な日付要素を抽出しますが、ExtractDateElem副作用はありません。

于 2013-01-02T08:31:14.787 に答える
-1

以下のように変数を宣言する必要があります...

 public static  string yyyy;
 public static  string mm ;
 public static  string dd ; 

または

 protected static  string yyyy;
 protected static  string mm ;
 protected static  string dd ;

あなたの必要に応じて、あなたのprogram.csファイルがどこにあるかによって異なります...

于 2013-01-02T08:34:39.510 に答える