Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Crash as value not long enough?

Posted on 2006-05-12
Medium Priority
Last Modified: 2010-04-16

Im getting a crash in my code (at the part commented below)

public virtual object InitialValue
                return dataTypeInitialValue;
                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(reasons[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?

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("Value 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
Question by:paulwhelan
LVL 11

Accepted Solution

vo1d earned 500 total points
ID: 16668902
i cant follow up your problem.
when is InitialValue called?
if you wanna add a value to your reasons list?
and why are you passing an object in your IsValid method instead of using a string directly?
you only save strings in your list, correct?

Author Comment

ID: 16718209
Sorry worked it out myself
please ignore

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question