Complex logic when assigning value to variable

My question is, how and which approach would you recommend, when I would have to assign value to a variable that is a bit more complex. Bellow this text, you can see a short example that I have written. Both month and year variables can be assigned in the variables tab, but I am having a hard time finding an approach, where there is more line of logic to implement.
Thanks for all the help!

var month = new Random().Next(1,13);
var year = new Random().Next(DateTime.Now.Year, DateTime.Now.Year + 100);

if(month == 1 || month == 3 || month == 5 || month == 7
|| month == 8 || month == 10 || month == 12 )
{
days = 31;
}
else if(month == 2)
{
if(year % 4 == 0 && year % 100 == 0 && year % 400 == 0)
{
days = 28;
}
else{
days = 29;
}
}
else
{
days = 30;
}

var day = new Random().Next(1, days + 1);

Use the Else If activity.

1 Like

Hey @Rok_Furlan! Lets go to the solution!!

Step 1 - Let’s initialize our variables.

int_Month - new Random().Next(1,13)- Same idea as you.
int_Year - new Random().Next(DateTime.Now.Year, DateTime.Now.Year + 100) - Same idea as you.
int_MaxDays - This is the variable that will assume your logic explained above.
int_Days - You will receive the value you explained above at the end of the execution.

Step 2 - Implement your main logic.

If(int_Month = 1 Or int_Month = 3 Or int_Month = 5 Or int_Month = 7 Or int_Month = 8 Or int_Month = 10 Or int_Month = 12,
	31,
	If(int_Month = 2,
		If(int_Year Mod 4 = 0 And int_Year Mod 100 = 0 And int_Year Mod 400 = 0,
			28,
			29
			),
		30
	)
)

Step 3 - Make the assignment of days.

new Random().Next(1, int_MaxDays + 1)

Hope it helps!!

1 Like

And welcome to the comunnity!!! :tada: :tada: :mechanical_arm: :mechanical_arm:

1 Like

That’s very messy and a black box. The Else If activity is much cleaner.

Hey @postwick ! Thanks for the comment and the feedback! But you are wrong!

Best Regards!!

1 Like

Boys boys, you do make me laugh.

The real question though is why implement all this logic when all of it is already included in the DateTime class.

.NET has the DateTime.DaysInMonth(year As Int32, month As Int32) method for Days in Month - this takes in account leap years automatically.

.NET has the DateTime.IsLeapYear(year As Int32) Method for leap years.

Rule of thumb @Rok_Furlan: Don’t create unnecessary work for yourself :smile:

Kind Regards

4 Likes

@Rok_Furlan
it looks like a date creation randomly within today and the next 100 Years

if so, then we can do it by this as well

grafik

 (now.AddYears(100).Date - now.Date).TotalDays
 36524
 now.AddDays(new Random().Next Mod 36524)
 [02/23/2085 23:38:37]
 now.AddDays(new Random().Next Mod 36524)
 [04/05/2044 23:38:41]
 now.AddDays(new Random().Next Mod 36524)
 [08/05/2057 23:38:43]
 now.AddDays(new Random().Next Mod 36524)
 [01/17/2042 23:38:44]
 now.AddDays(new Random().Next Mod 36524)
 [06/07/2120 23:38:45]
 now.AddDays(new Random().Next Mod 36524)
 [11/18/2022 23:38:46]
1 Like

It is by definition a black box.

As for messy, let’s compare that to…

1 Like

thank you!!!

1 Like

You’re welcome, @Rok_Furlan!! If this solved your problem, mark the answer as “solved” that then the topic will be finalized and your doubt may be that of future programmers!! Thank you!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.