2
\$\begingroup\$

Im curious if my approach is correct in this case:

I would like to get two DateTimes from a public property, but one of the dates i want them only if a user has purchase a feature or if he has not activate a switch, otherwise i want to get the same DateTime twice.

More specifically, the way im getting the values now is this:

public class MainApplication : Application
{
public DateTime DateFrom { get; set; } = DateTime.Now;
public DateTime DateTo { get; set; } = DateTime.Now;
}

AnotherClass
{
            SearchButton.Click += async (sender, e) =>
            {
                await SearchFoNumbersAsync(ApplicationState.DateFrom, ApplicationState.DateTo, DailySearchSwitch.Checked, SeperateCheckBox.Checked, ShowListsCheckBox.Checked).ConfigureAwait(false);
            };
            private async Task SearchFoNumbersAsync(DateTime dateFrom, DateTime dateTo, bool isDailySearchEnabled, bool seperateSearch, bool showDrawTime)
            {
                ((MainActivity)Activity).DisplayLoadingMessage(true, GetString(Resource.String.Common_SearchTitle), GetString(Resource.String.Common_SearchMessage));

                if (isDailySearchEnabled || !PurchaseFunctions.HasPurchasedDateRangeSelection())
                    dateTo = dateFrom;
...
            }
}

So i want to get DateTo only
if (isDailySearchEnabled || !PurchaseFunctions.HasPurchasedDateRangeSelection()), otherwise i want to get the DateFrom value in the place of DateTo

Im sure this approach is not correct so im thinking i should get the DateTo this way:

public class MainApplication : Application
{
    public DateTime DateFrom { get; set; } = DateTime.Now;
    private DateTime DateTo { get; set; } = DateTime.Now;

    public DateTime GetDateTo(bool isDailySearchEnabled)
    {
        if (isDailySearchEnabled || !PurchaseFunctions.HasPurchasedDateRangeSelection())
             return DateFrom;
        else
             return DateTo;
    }

    public void SetDateTo(DateTime dateTime)
    {
        DateTo = dateTime;
    }
}

Is my new approach a good idea?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Probably the direction is correct but the class API is inconsistent for now. DateFrom is available, DateTo is not.

As for me, the following way looks more consistent.

public class MainApplication : Application
{
    public DateTime DateFrom { get; set; } = DateTime.Now;
    public DateTime DateTo { get; set; } = DateTime.Now;

    public DateTime GetSearchDate(bool isDailySearchEnabled)
        => isDailySearchEnabled || !PurchaseFunctions.HasPurchasedDateRangeSelection() ? DateFrom : DateTo;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.