paulwhelan
asked on
Crash as value not long enough?
Hi
Im getting a crash in my code (at the part commented below)
public virtual object InitialValue
{
get
{
return dataTypeInitialValue;
}
set
{
List<string> reasons = new List<string>();
IsValid(value, reasons);
//LINE BELOW - As reasons.Count = 1
//The string '' is less than the minimum length of 10 characters.
if(reasons.Count > 0)
throw new ApplicationException(reaso ns[0]);
dataTypeInitialValue = value;
}
Now the Minimum and Maximum valid length can change all the time.
Just at this point its crashing as its trying to assing an InitialValue to be "" instead of the minimum required value of 10 (in this case).
The thing is when I created this control (in our program) I gave it a minimum value of 10 but didnt assign the InitialValue to be anything (hence its "" and crashing)
The question is should I change the IsValid method to allow for an empty InitialValue string even if the minimum is set to something greater than 0?
Any ideas?
Thanks
Paul
The following code might help.
public override void IsValid(object value, List<string> reasons)
{
if (value == null) return;
if (!(value is string)) throw new ApplicationException("Valu e is of type '" + value.GetType().FullName + "', not 'string'.");
string stringValue = (string)value;
if (_bHasMinLength && stringValue.Length < _iMinLength) reasons.Add("The string '" + stringValue + "' is less than the minimum length of " + _iMinLength + " characters.");
if (_bHasMaxLength && stringValue.Length > _iMaxLength) reasons.Add("The string '" + stringValue + "' is greater than the maximum length of " + _iMaxLength + " characters.");
// TODO: regexp and allowed characters
}
}
Im getting a crash in my code (at the part commented below)
public virtual object InitialValue
{
get
{
return dataTypeInitialValue;
}
set
{
List<string> reasons = new List<string>();
IsValid(value, reasons);
//LINE BELOW - As reasons.Count = 1
//The string '' is less than the minimum length of 10 characters.
if(reasons.Count > 0)
throw new ApplicationException(reaso
dataTypeInitialValue = value;
}
Now the Minimum and Maximum valid length can change all the time.
Just at this point its crashing as its trying to assing an InitialValue to be "" instead of the minimum required value of 10 (in this case).
The thing is when I created this control (in our program) I gave it a minimum value of 10 but didnt assign the InitialValue to be anything (hence its "" and crashing)
The question is should I change the IsValid method to allow for an empty InitialValue string even if the minimum is set to something greater than 0?
Any ideas?
Thanks
Paul
The following code might help.
public override void IsValid(object value, List<string> reasons)
{
if (value == null) return;
if (!(value is string)) throw new ApplicationException("Valu
string stringValue = (string)value;
if (_bHasMinLength && stringValue.Length < _iMinLength) reasons.Add("The string '" + stringValue + "' is less than the minimum length of " + _iMinLength + " characters.");
if (_bHasMaxLength && stringValue.Length > _iMaxLength) reasons.Add("The string '" + stringValue + "' is greater than the maximum length of " + _iMaxLength + " characters.");
// TODO: regexp and allowed characters
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
please ignore