© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
9 replies
TotechsStrypper

Refactoring if else

Help !
partial void OnNameChanging(string value)
        {
            _validationService.ClearErrors(nameof(Name));
            ErrorsList.ToList()
                      .Where(error => error.PropId == 1)
                      .All(error => ErrorsList.Remove(error));

            if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
            {
                _validationService.AddError(nameof(Name), "You must provide your pet a name");

                var errors = GetErrors(nameof(Name)).OfType<string>().ToList();
                errors.ForEach(error => ErrorsList.Add(new ValidationProperty(1, error)));

            }
            else if (value.Length < 3)
            {
                _validationService.AddError(nameof(Name), "Your pet name is a little short");

                var errors = GetErrors(nameof(Name)).OfType<string>().ToList();
                errors.ForEach(error => ErrorsList.Add(new ValidationProperty(1, error)));
            }
            else if (value == "Snow")
            {
                _validationService.AddError(nameof(Name), "Wait! ain't snow is already in your profile ?");

                var errors = GetErrors(nameof(Name)).OfType<string>().ToList();
                errors.ForEach(error => ErrorsList.Add(new ValidationProperty(1, error)));
            }
        }
partial void OnNameChanging(string value)
        {
            _validationService.ClearErrors(nameof(Name));
            ErrorsList.ToList()
                      .Where(error => error.PropId == 1)
                      .All(error => ErrorsList.Remove(error));

            if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
            {
                _validationService.AddError(nameof(Name), "You must provide your pet a name");

                var errors = GetErrors(nameof(Name)).OfType<string>().ToList();
                errors.ForEach(error => ErrorsList.Add(new ValidationProperty(1, error)));

            }
            else if (value.Length < 3)
            {
                _validationService.AddError(nameof(Name), "Your pet name is a little short");

                var errors = GetErrors(nameof(Name)).OfType<string>().ToList();
                errors.ForEach(error => ErrorsList.Add(new ValidationProperty(1, error)));
            }
            else if (value == "Snow")
            {
                _validationService.AddError(nameof(Name), "Wait! ain't snow is already in your profile ?");

                var errors = GetErrors(nameof(Name)).OfType<string>().ToList();
                errors.ForEach(error => ErrorsList.Add(new ValidationProperty(1, error)));
            }
        }
unknown.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ just a quick question about if, else, else if
C#CC# / help
2y ago
Nested else if | help
C#CC# / help
12mo ago
✅ Button and if else
C#CC# / help
3y ago