设定方法检查问题 [英] Setter Method Checking Problem

查看:81
本文介绍了设定方法检查问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我正在尝试理解C#中的属性方法。但在设定价值期间它没有检查条件并给我错误的答案。请查看代码我告诉我如果可能的好解决方案。



代码



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 PracticeQuestion
{
class ElementContainer
{
private double 长度;
private double 宽度;

// Setter and Getter Property

public double SetLength
{
获取 {返回长度;}
设置
{
if (长度> 10
{
Console.Write( \ n \ n)有一些错误的输入。 );
}
其他
{
长度= 价值 ;
}
}
}
public double SetWidth
{
get
{
return 宽度;
}
设置
{
如果(宽度) > 10
{
Console.Write( \ n \ n有一些错误输入。);
}
else
{
Width = value ;
}
}
}
public ElementContainer()
{
Console.Write( 请输入以下数据。);
}
public 覆盖 string ToString()
{
return string .Format(< span class =code-string>
\ n \ nnlength:{0} \ n \ nWidth:{1} \ n\\\
Total面积:{2}
,长度,宽度,长度*宽度);
}
}
}







 计划
{
静态 void Main( string [] args)
{
ElementContainer Element = new ElementContainer();

// 错误是否

i set 值然后我想先检查Setter条件然后 set value 根据要求的条件。

元素.SetLength = 10 ;
Element.SetWidth = 20 ;
Console.Write(Element);
Console.ReadLine();
}
}

解决方案

如果setter遇到无效参数,则选项不是写入控制台,因为这不会向呼叫者提供任何信息。在这种情况下你应该抛出异常。

这样的东西:

  public   double  SetWidth 
{
get
{
返回宽度;
}
set
{
if (< u> value > 10
{
throw new ArgumentOutOfRangeException ( 有一些错误输入。);
}
else
{
Width = value ;
}
}
}





在调用者方面,你需要try-catch块来拦截这个。

另一个基于SA捕获的注释:在setter中你需要验证新的,而不是存储的值。


Hello Everyone!

I Am Trying to understand the Property Method in C#. But During Setting the Value it Did not check the Condition and give me Wrong Answer .Please See the Code me Tell me Good Solution if Possible.

Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PracticeQuestion
{
    class ElementContainer
    {
        private double Length;
        private double Width;

        //Setter and Getter Property

        public double SetLength
        {
            get { return Length;}
            set
            {
                if (Length > 10)
                {
                    Console.Write("\n\nThere is some Bad Input.");
                }
                else
                {
                    Length = value;
                }
            }
        }
        public double SetWidth
        {
            get
            {
                 return Width;
            }
            set
            {
                if (Width > 10)
                {
                    Console.Write("\n\nThere is some Bad Input.");
                }
                else
                {
                    Width = value;
                }
            }
        }
        public ElementContainer ()
        {
            Console.Write("Please Enter The Following Data.");
        }
        public override string ToString ()
        {
            return string.Format("\n\nlength:{0}\n\nWidth:{1}\n\nTotal Area:{2}", Length, Width,Length*Width);
        }
    }
}




class Program
   {
       static void Main ( string[] args )
       {
           ElementContainer Element = new ElementContainer();

//Error Is There

when i set The Value Then i want first it check the Setter Condition then set value According to required Condition.

           Element.SetLength = 10;
           Element.SetWidth = 20;
           Console.Write(Element);
           Console.ReadLine();
       }
   }

解决方案

If setter encounters invalid argument, your option is not to write to the console, since that won't provide any information to the caller. You should throw an exception in that case.
Something like this:

public double SetWidth
        {
            get
            {
                 return Width;
            }
            set
            {
                if (value > 10)
                {
                    throw new ArgumentOutOfRangeException("There is some Bad Input.");
                }
                else
                {
                    Width = value;
                }
            }
        }



On the caller side you need try-catch blocks to intercept this.
An other note based on SA's catch: in the setter you need to validate the new value, not the stored one.


这篇关于设定方法检查问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆