Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It may sound like a task itself: it can increase or decrease its value by one in a given range. An initialization value is provided. In the absence abroad, exceptions are thrown.
And now I wanted to ask you if everything meets the condition or am I missing something?

What I have tried:

C#
using System;
namespace laba7
{
    public class Program
    {
        public static void Main()
        {
            int x, y, z;
            Console.WriteLine("Начало решения");
            Console.WriteLine("Введите значение");
            x = int.Parse(Console.ReadLine());
            Console.WriteLine("Введите минимум");
            y = int.Parse(Console.ReadLine());
            Console.WriteLine("Введите максимум");
            z = int.Parse(Console.ReadLine());
            Counter counter = new Counter(x, y, z);
            counter--;
            Console.WriteLine("Ответ: {0}", counter.Value);
            
            Console.WriteLine("Конец решения");
            Console.ReadLine();

        }

        class Counter
        {
            int val;

            public int Value
            {
                get { return val; }
                set { val = value; InRange(); }
            }

            private int min { get; set; }

            private int max { get; set; }

            public Counter(int _val, int _min, int _max)
            {
                min = _min;
                max = _max;
                Value = _val;
            }

            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()
            {
                try
                {
                    if (val > max || val < min)
                        throw new ArgumentOutOfRangeException("Значение не в заявленном диапазоне.");
                }
                catch (ArgumentOutOfRangeException e)
                {
                    Console.WriteLine($"Ошибка: {e}");
                }
            }

        }

    }
}
Posted
Updated 2-Jun-20 0:32am
Comments
Richard MacCutchan 1-Jun-20 11:32am    
Does it work according to the requirements?
Grampy Cat 2-Jun-20 2:10am    
Well, it seems like yes, but for example it adds or subtracts a unit only depending on this line at the beginning, but should it be so?
Counter counter = new Counter(x, y, z);
counter--;

1 solution

Quote:
C#
try
{
    if (val > max || val < min)
        throw new ArgumentOutOfRangeException("Значение не в заявленном диапазоне.");
}
catch (ArgumentOutOfRangeException e)
{
    Console.WriteLine($"Ошибка: {e}");
}
That bit makes no sense. Throwing an exception just so you can catch it on the next line and write a message to the console wastes a lot of resources. Just use:
C#
if (val > max || val < min)
    Console.WriteLine("Ошибка: Значение не в заявленном диапазоне.");

But you'll still have the issue that your object is in an invalid state, and the caller has no way of knowing that. It would be better to validate the value before changing the state of your object:
C#
public int Value
{
    get { return val; }
    set 
    { 
        if (value > max || value < min)
            throw new ArgumentOutOfRangeException("Значение не в заявленном диапазоне.");
        
        val = value;
    }
}
 
Share this answer
 
Comments
Maciej Los 2-Jun-20 6:38am    
5ed!
Grampy Cat 2-Jun-20 8:02am    
Ok, thx. Does the program generally work like a binary counter? or like decimal counter?
In this regard, I did everything right?
Richard Deeming 2-Jun-20 9:04am    
I'm not sure what you mean by a "binary" or "decimal" counter. A number is a number; it doesn't have a base until you display it to a human.

A computer will (usually) store numbers in binary, using a standard representation often defined by the IEEE. But that doesn't affect how you use them.

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