Reputation: 51
Can I have a function that checks if true or false and send my verbal to other classes?
I tried:
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;
}
}
}
I want to send the verbal yyyy
, mm
, dd
to my Program.cs
class.
What should I do?
Upvotes: 0
Views: 131
Reputation: 2190
you have to declare your variables as below...
public static string yyyy;
public static string mm ;
public static string dd ;
Or
protected static string yyyy;
protected static string mm ;
protected static string dd ;
as per your need and depends where your program.cs file is...
Upvotes: -1
Reputation: 1039488
Don't reinvent wheels, use the DateTime.TryParseExact
method which is built specifically for this purpose. Forget about regexes and substrings when you are dealing with dates in the .NET framework:
public static bool CheckDate(string number, out DateTime date)
{
return DateTime.TryParseExact(number, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
}
And now as you can see defining the CheckDate
becomes kinda meaningless because it already exists in the BCL. You would simply use it like this:
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
}
UPDATE:
Since I got a remark in the comments section that I am not actually answering the question, you could use a similar approach to the one I recommend. But please, promise me you will never write a code like this, it's just for illustration of the TryXXX pattern.
define a model:
public class Patterns
{
public string DD { get; set; }
public string MM { get; set; }
public string YYYY { get; set; }
}
and then modify your CheckDate method so that it sends an out parameter:
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;
}
}
which you could use like this:
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
}
Upvotes: 7
Reputation: 2002
The CheckDate
function’s aim is to check if a date is valid. Don’t introduce crappy side effects: write another function that actually sends your stuff to the object you want.
If you want to check if a string is a date, do it in CheckDate
.
When you know a string is a date, extract the date elements you want from it through such a ExtractDateElem
function, but please, no side-effects.
Upvotes: 0