locked
How to call ArgumentOutOfRangeException with a nested property? RRS feed

  • Question

  • User-1188570427 posted

    I'm trying to call this to check a property to make sure it is in range:

                if (endItemOrganizationCreate.EndItemId <= 0)
    {
    throw new ArgumentOutOfRangeException(nameof(endItemOrganizationCreate.EndItemId));
    }

    I get this error though?

    Severity Code Description Project File Line Suppression State
    Error CA2208 Method CreateEndItemOrganization passes 'EndItemId' as the paramName argument to a ArgumentOutOfRangeException constructor. Replace this argument with one of the method's parameter names. Note that the provided parameter name should have the exact casing as declared on the method. 

    How do I call a argument out of range exception a nested property?

    Here is my method call:

            public async Task<int> CreateEndItemOrganization(EndItemOrganizationCreateDTO endItemOrganizationCreate)
            {
            }

    I understand it needs to check: endItemOrganizationCreate

    but how can I call ArugmentOutOfRangeException on a nested property because that needs to be checked as well.

    Tuesday, March 12, 2019 2:20 PM

Answers

  • User-893317190 posted

    Hi tvb2727,

    It seems your code has no problem, I thin you have applied too many analysis rules for your project.

    Please right click your project , click properties.

    In the left bar,choose Code Analysie,  you could see what rules you have chosen.

    Generally, we set it to Microsoft Managed Recommeded Rules, please change it to this rule if you have not.

    When I set to all rules , CA2208 shows , it is a warning ,you could omit it, it will not cause runtime error.

    Best regards,

    Ackerly Xu


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 14, 2019 1:38 AM

All replies

  • User475983607 posted

    Try explicitly setting the property.

    throw new ArgumentOutOfRangeException(paramName: nameof(endItemOrganizationCreate.EndItemId));

    Reference docs.

    https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2208-instantiate-argument-exceptions-correctly?view=vs-2017

    Tuesday, March 12, 2019 2:33 PM
  • User-1188570427 posted

    Try explicitly setting the property.

    throw new ArgumentOutOfRangeException(paramName: nameof(endItemOrganizationCreate.EndItemId));

    Reference docs.

    https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2208-instantiate-argument-exceptions-correctly?view=vs-2017

    Thanks.

    That did not seem to work. Do you think it is connected to this?

    https://developercommunity.visualstudio.com/content/problem/480715/vs2019-rosyln-analyzers-dont-work-correctly-in-pro.html

    https://github.com/dotnet/roslyn-analyzers/issues/1561

    Tuesday, March 12, 2019 2:50 PM
  • User-893317190 posted

    Hi tvb2727,

    Not sure about your problem , when I try to call ArgumentOutOfRangeException, I haven't met your problem.

    Below is my code.

    protected void Page_Load(object sender, EventArgs e)
            {
    
             
                Class1 class1 = new Class1();
          
                throw new ArgumentOutOfRangeException(nameof(class1.Size ));
            }
    
            public class Class1
            {
                private int _size;
                public int Size
                {
                    get => _size;
                    set
                    {
                        if (value < 0)
                        {
                            throw new ArgumentOutOfRangeException(nameof(Size));
                        }
    
                        _size = value;
                    }
                }
            }

    The result.

    Could you provide more code  so that we could reproduce your problem?

    Best regards,

    Ackerly Xu

    Wednesday, March 13, 2019 2:47 AM
  • User-1188570427 posted

    Hi tvb2727,

    Not sure about your problem , when I try to call ArgumentOutOfRangeException, I haven't met your problem.

    Below is my code.

    protected void Page_Load(object sender, EventArgs e)
            {
    
             
                Class1 class1 = new Class1();
          
                throw new ArgumentOutOfRangeException(nameof(class1.Size ));
            }
    
            public class Class1
            {
                private int _size;
                public int Size
                {
                    get => _size;
                    set
                    {
                        if (value < 0)
                        {
                            throw new ArgumentOutOfRangeException(nameof(Size));
                        }
    
                        _size = value;
                    }
                }
            }

    The result.

    Could you provide more code  so that we could reproduce your problem?

    Best regards,

    Ackerly Xu

    I am using Code Analysis. I assume that is causing it? Just figured it should work or maybe I just need to suppress it?

    Wednesday, March 13, 2019 12:23 PM
  • User-893317190 posted

    Hi tvb2727,

    It seems your code has no problem, I thin you have applied too many analysis rules for your project.

    Please right click your project , click properties.

    In the left bar,choose Code Analysie,  you could see what rules you have chosen.

    Generally, we set it to Microsoft Managed Recommeded Rules, please change it to this rule if you have not.

    When I set to all rules , CA2208 shows , it is a warning ,you could omit it, it will not cause runtime error.

    Best regards,

    Ackerly Xu


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 14, 2019 1:38 AM