Share via

what sproblem in below code?

Mohammad Ali Echreshavi 101 Reputation points
2022-08-24T07:07:48.177+00:00
   class sample_class1  
    {  
         
        public static int SquareNumber(int value)   
        {  
            var expression = GetSquareExpression();   
            expression.Compile();   
            return expression(value);   
        }   
        public static Expression<Func<int, int>> GetSquareExpression()   
        {  
            return i => i * i;   
        }   
    }  
  
class sample_class2  
    {  
  
        [Flags]   
        enum FortbildungsForm   
        {   
            Halbtagsveranstaltung = 1,  
            Ganztagsveranstaltung = 2,   
            Seminar = 4, Sonstiges = 8   
        }  
        class FortbildungsFormular : IValidatableObject   
        {  
            public FortbildungsForm? Form { get; set; }   
            public string? FormSonstige { get; set; }   
            public IEnumerable<ValidationResult> Validate(ValidationContext context)   
            {  
                if (!Form.HasValue || Form == 0)   
                    yield return new ValidationResult("test1.", new[] { nameof(Form) });  
  
                if (Form.HasValue && (Form.Value & FortbildungsForm.Sonstiges) != 0)  
                    yield return new ValidationResult("test2", new[]  
                    {  
                        nameof(FormSonstige)  
                    });  
            }   
        }  
    }  
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


Answer accepted by question author

Karen Payne MVP 35,606 Reputation points Volunteer Moderator
2022-08-24T09:46:58.143+00:00

In the project file <Nullable>enable</Nullable>

using System.ComponentModel.DataAnnotations;  
  
namespace C1.Classes;  
  
internal class SampleClass1  
{  
  
    public static int SquareNumber(int value)  
    {  
        Expression<Func<int>> squared = () => value * value;  
        var expression = squared.Compile();  
        return expression();  
    }  
}  
  
internal class SampleClass2  
{  
  
    [Flags]  
    enum FortbildungsForm  
    {  
        Halbtagsveranstaltung = 1,  
        Ganztagsveranstaltung = 2,  
        Seminar = 4, Sonstiges = 8  
    }  
    class FortbildungsFormular : IValidatableObject  
    {  
        public FortbildungsForm? Form { get; set; }  
        public string? FormSonstige { get; set; }  
        public IEnumerable<ValidationResult> Validate(ValidationContext context)  
        {  
            if (Form is null or 0)  
            {  
                yield return new ValidationResult("test1.", new[] { nameof(Form) });  
            }  
  
            if (Form.HasValue && (Form.Value & FortbildungsForm.Sonstiges) != 0)  
            {  
                yield return new ValidationResult("test2", new[]  
                {  
                        nameof(FormSonstige)  
                });  
            }  
        }  
    }  
}  
  
  

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.