public class Class1
{
private string abc;
public string ab
{
get
{
return abc;
}
set
{
if(abc.Length>6)
{
throw new ArgumentException("过长");
}
else
{
this.abc=value;
}
}
}
}我是想限制ab的长度,但是上面的代码会“提示未将对象引用设置到对象的实例。”public class Class1
{
private string abc;
public string ab
{
get
{
return abc;
}
set
{
if(ab.Length>6)
{
throw new ArgumentException("过长");
}
else
{
this.abc=value;
}
}
}
}也不行
{
private string abc;
public string ab
{
get
{
return abc;
}
set
{
if(abc.Length>6)
{
throw new ArgumentException("过长");
}
else
{
this.abc=value;
}
}
}
}我是想限制ab的长度,但是上面的代码会“提示未将对象引用设置到对象的实例。”public class Class1
{
private string abc;
public string ab
{
get
{
return abc;
}
set
{
if(ab.Length>6)
{
throw new ArgumentException("过长");
}
else
{
this.abc=value;
}
}
}
}也不行
private string abc;
没有直接给定初值 ,也没有在默认构造函数中初始,所以应加一个判断null
public class Class1
{
private string abc;
public string ab
{
get{ return abc;}
set{
if(value !=null && value.Length>6) throw new ArgumentException("过长");
else abc = value;
}
}
}
/// 创建自定义如创建Excel实例错误。
/// </summary>
public class ExceptionExcelCreateInstance:Exception
{
#region 实现...
string _Message = "创建Excel类实例时错误!"; /// <summary>
/// 创建类的新实例。
/// </summary>
public ExceptionExcelCreateInstance()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <param name="message">错误信息。</param>
public ExceptionExcelCreateInstance(string message)
{
this._Message = message;
} /// <summary>
/// 获取错误信息。
/// </summary>
public override string Message
{
get
{
return this._Message;
}
}
#endregion}//End ExceptionExcelCreateInstance
使用throw自定义异常类似throw new ExceptionExcelCreateInstance("....");