# 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!!!

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

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

`````` (now.AddYears(100).Date - now.Date).TotalDays
36524
[02/23/2085 23:38:37]
[04/05/2044 23:38:41]
[08/05/2057 23:38:43]
[01/17/2042 23:38:44]
[06/07/2120 23:38:45]
[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.