Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The task:Describe a class that implements a binary counter that can increase or decrease its value by one in this range. Provide initialization of the counter with default values ​​and arbitrary values. Exceeding the range creates exceptions.

But I always get the error that "this argument is out of range of acceptable values." Please help me fix)

What I have tried:

C#
<pre>class Counter
    {
        int val;
        public int Value { get { return val; } set { val = value; InRange(); } }
        int min { get; }
        int max { get; }
 
        public Counter(int _val, int _min, int _max)
        {
            Value = _val;
            min = _min;
            max = _max;
        }
        public Counter(int _min, int _max)
        {
            min = _min;
            max = _max;
            Value = new Random().Next(min, max + 1);
        }
 
        public static Counter operator ++(Counter c)
        {
            c.Value++;
            return c;
        }
        public static Counter operator --(Counter c)
        {
            c.Value--;
            return c;
        }
 
        void InRange()
        {
            if (val > max || val < min) ;
            throw new ArgumentOutOfRangeException("Значение не в заявленном диапазоне.");
            
        }
 
        class Class1
        {
            static void Main()
            {
                Counter counter = new Counter(5, 0, 10); 
                Counter counterRand = new Counter(7, 29);
                counter++;
                counterRand--;
 
            }
        }
    }
}
Posted
Updated 25-May-20 3:07am

Get the Class1 class out of the Counter class.
 
Share this answer
 
Comments
Grampy Cat 25-May-20 1:45am    
Oh, thx. but is class1 written correctly? Because as there was a mistake, so it remained.
phil.o 25-May-20 1:49am    
What mistake?
And I see obvious flaw: you do not test whether bounds are properly ordered on creation.
Grampy Cat 25-May-20 1:52am    
"Exception not processed" it's ok?
phil.o 25-May-20 1:55am    
No. We would need to know the exact type of the exception, as well as the pace in the code where it happens.
Grampy Cat 25-May-20 1:59am    
Oh sry, and how to fix it?
Change your constructor: set the Max and Min levels before you set your Value.
Your current way, you set the Value first, which does a call to InRange which tests it against the Max and Min you haven't set yet - so they default to zero and you will always fail the test unless the Value you set is also zero.
 
Share this answer
 
Comments
Grampy Cat 25-May-20 3:26am    
You explain it very well, but I still do not fully understand.. So.. Can you show what exactly needs to be changed with pieces of code?
OriginalGriff 25-May-20 3:57am    
Oh come on!

Read what I said, look at your code, and if you need to, run it through the debugger - in fact do that first: it'll show you exactly what is happening with your code while it is running. You should be able to fix this - you wrote teh original code, after all so you you know exactly what it is doing and what it is meant to do!
Grampy Cat 25-May-20 4:21am    
I just do not fully understand my mistake and therefore I ask you for help. I already realized that at first I have a check, and then a value is set, but nothing happens.
OriginalGriff 25-May-20 4:29am    
Literally: read what I said, and think about the order in which things happen; use the debugger to follow your code through if you don't understand - it will show you exactly what happens, and when.

Put a breakpoint on the first line of the Counter constructor and run your app in the debugger. When it reaches the breakpoint, it will stop and let you take control. Step through your code line by line and watch what is happening to your variables!

Yes, I could tell you exactly what to do, and it'd be a lot quicker for us both - but you wouldn't learn anything, so you'd repeat the same error next time.

Give it a try - it's not complicated, and it's an important skill to learn.
Grampy Cat 25-May-20 5:21am    
In fact, I am from Russia and I will say that the community of this forum is much kinder than the Russian forum. And what's funny, I just found out about the breakpoint ... For which many thanks to you. But now I tried everything that could come to my mind, and could you tell me what needs to be changed.
Of course, I will still try to understand my mistake, but now I need the result ..
I tested Phil and Griff's answers in this .NET fiddle, it works:
C# Online Compiler | .NET Fiddle[^]
 
Share this answer
 
Comments
Grampy Cat 25-May-20 9:45am    
Listen, how does this code look like a block diagram?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900