Solved

another way to write this?

Posted on 2010-09-16
19
552 Views
Last Modified: 2012-05-10
return ValidationHelper.GetInteger(ViewState["MessageId"], 0);
is there another way to write the above line without ValidationHelper?

//public int MessageId
        //{
        //    get
        //    {
        //        return ValidationHelper.GetInteger(ViewState["MessageId"], 0);
        //    }
        //    set
        //    {
        //        ViewState["MessageId"] = value;
        //    }
        //}
#region Assembly CMS.GlobalHelper.dll, v2.0.50727

// C:\inetpub\wwwroot\KenticoCMS\Bin\CMS.GlobalHelper.dll

#endregion



using System;

using System.Text.RegularExpressions;



namespace CMS.GlobalHelper

{

    // Summary:

    //     Global class providing Validation methods

    public static class ValidationHelper

    {

        // Summary:

        //     Gets or sets the regular expression for client id validation

        public static Regex ClientIDRexExp { get; set; }

        //

        // Summary:

        //     Codename regular expression

        public static Regex CodenameRegExp { get; set; }

        //

        // Summary:

        //     Hexa color code regular expression

        public static Regex ColorRegExp { get; set; }

        //

        // Summary:

        //     Custom user name regular expression string from web.config for user name

        //     validation

        public static string CustomUsernameRegExpString { get; }

        //

        // Summary:

        //     Email regular expression

        public static Regex EmailRegExp { get; set; }

        //

        // Summary:

        //     Filename regular expression

        public static Regex FilenameRegExp { get; set; }

        //

        // Summary:

        //     Folder regular expression

        public static Regex FolderRegExp { get; set; }

        //

        // Summary:

        //     GUID regular expression

        public static Regex GuidRegExp { get; set; }

        //

        // Summary:

        //     Identificator regular expression

        public static Regex IdentificatorRegExp { get; set; }

        //

        // Summary:

        //     Integer regular expression

        public static Regex IntRegExp { get; set; }

        //

        // Summary:

        //     Long regular expression

        public static Regex LongRegExp { get; set; }

        //

        // Summary:

        //     Codename regular expression with support of unicode letters

        public static Regex UnicodeCodenameRegExp { get; set; }

        //

        // Summary:

        //     URL regular expression

        public static Regex URLRegExp { get; set; }

        //

        // Summary:

        //     Username regular expression

        public static Regex UsernameRegExp { get; set; }

        //

        // Summary:

        //     Indicates whether to use safe or normal role names

        public static bool UseSafeRoleName { get; set; }

        //

        // Summary:

        //     Indicates whether to use safe or normal user names

        public static bool UseSafeUserName { get; set; }



        // Summary:

        //     Returns true if the object representation matches the Email list (email adresses

        //     separated by semicolon)

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool AreEmails(object value);

        //

        // Summary:

        //     Returns the byte[] representation of an object or default value if not

        //

        // Parameters:

        //   value:

        //     Value to convert.

        //

        //   defaultValue:

        //     Default value.

        public static byte[] GetBinary(object value, byte[] defaultValue);

        //

        // Summary:

        //     Returns the boolean representation of an object or default value if not

        //

        // Parameters:

        //   value:

        //     Value to convert

        //

        //   defaultValue:

        //     Default value

        public static bool GetBoolean(object value, bool defaultValue);

        //

        // Summary:

        //     Gets the code name created from the given string

        //

        // Parameters:

        //   name:

        //     Display name

        public static string GetCodeName(object name);

        //

        // Summary:

        //     Gets the code name created from the given string

        //

        // Parameters:

        //   name:

        //     Display name

        //

        //   maxLength:

        //     Maximal length of the codename

        public static string GetCodeName(object name, int maxLength);

        //

        // Summary:

        //     Gets the code name created from the given string

        //

        // Parameters:

        //   name:

        //     Display name

        //

        //   replacement:

        //     Replacement string for invalid characters

        public static string GetCodeName(object name, string replacement);

        //

        // Summary:

        //     Gets the code name created from the given string

        //

        // Parameters:

        //   name:

        //     Display name

        //

        //   replacement:

        //     Replacement string for invalid characters

        //

        //   maxLength:

        //     Maximal length of the codename

        public static string GetCodeName(object name, string replacement, int maxLength);

        //

        // Summary:

        //     Gets the code name created from the given display name

        //

        // Parameters:

        //   name:

        //     Display name

        //

        //   prefix:

        //     Prefix of the display name

        //

        //   suffix:

        //     Suffix of the display name

        public static string GetCodeName(string name, string prefix, string suffix);

        //

        // Summary:

        //     Gets the code name created from the given string

        //

        // Parameters:

        //   name:

        //     Display name

        //

        //   replacement:

        //     Replacement string for invalid characters

        //

        //   maxLength:

        //     Maximal length of the codename

        //

        //   useUnicode:

        //     If true, unicode letters are allowed in the codename, otherwise only a-Z

        //     are allowed.

        //

        //   removeDiacritics:

        //     If true, diacritics is removed from latin characters.

        public static string GetCodeName(object name, string replacement, int maxLength, bool useUnicode, bool removeDiacritics);

        //

        // Summary:

        //     Returns the DateTime representation of an object or default value if not

        //

        // Parameters:

        //   value:

        //     Value to convert.

        //

        //   defaultValue:

        //     Default value.

        public static DateTime GetDateTime(object value, DateTime defaultValue);

        //

        // Summary:

        //     Returns the DateTime representation of an object or default value if not

        //

        // Parameters:

        //   value:

        //     Value to convert.

        //

        //   defaultValue:

        //     Default value.

        //

        //   format:

        //     Format provider

        public static DateTime GetDateTime(object value, DateTime defaultValue, IFormatProvider format);

        //

        // Summary:

        //     Returns the DateTime representation of an object or default value if not

        //

        // Parameters:

        //   value:

        //     Value to convert.

        //

        //   defaultValue:

        //     Default value.

        //

        //   culture:

        //     Culture to use for processing of the string

        public static DateTime GetDateTime(object value, DateTime defaultValue, string culture);

        //

        // Summary:

        //     Returns the double representation of an object or default value if not

        //

        // Parameters:

        //   value:

        //     Value to convert

        //

        //   defaultValue:

        //     Default value

        public static double GetDouble(object value, double defaultValue);

        //

        // Summary:

        //     Returns the double representation of an object or default value if not

        //

        // Parameters:

        //   value:

        //     Value to convert

        //

        //   defaultValue:

        //     Default value

        //

        //   culture:

        //     Culture code

        public static double GetDouble(object value, double defaultValue, string culture);

        //

        // Summary:

        //     Returns the GUID representation of an object or default value if not

        //

        // Parameters:

        //   value:

        //     Value to convert

        //

        //   defaultValue:

        //     Default value

        public static Guid GetGuid(object value, Guid defaultValue);

        //

        // Summary:

        //     Gets the hash for the given value

        //

        // Parameters:

        //   value:

        //     Value to hash

        public static string GetHashString(string value);

        //

        // Summary:

        //     Gets the code name created from the given string

        //

        // Parameters:

        //   name:

        //     Display name

        public static string GetIdentificator(object name);

        //

        // Summary:

        //     Gets the code name created from the given string

        //

        // Parameters:

        //   name:

        //     Display name

        //

        //   replacement:

        //     Replacement string for invalid characters

        public static string GetIdentificator(object name, string replacement);

        //

        // Summary:

        //     Returns the integer representation of an object or default value if not

        //

        // Parameters:

        //   value:

        //     Value to convert

        //

        //   defaultValue:

        //     Default value

        public static int GetInteger(object value, int defaultValue);

        //

        // Summary:

        //     Gets the language created from the given string

        //

        // Parameters:

        //   lang:

        //     Language code

        //

        //   replacement:

        //     Replacement string for invalid characters

        public static string GetLanguage(object lang, string replacement);

        //

        // Summary:

        //     Returns the long representation of an object or default value if not

        //

        // Parameters:

        //   value:

        //     Value to convert

        //

        //   defaultValue:

        //     Default value

        public static long GetLong(object value, long defaultValue);

        //

        // Summary:

        //     Returns the integer representation of an object or default value if not.

        //      Consumes all exceptions.

        //

        // Parameters:

        //   value:

        //     Value to convert

        //

        //   defaultValue:

        //     Default value

        public static int GetSafeInteger(object value, int defaultValue);

        //

        // Summary:

        //     Gets safe version of rolename.

        //

        // Parameters:

        //   roleName:

        //     Name of role

        //

        //   siteName:

        //     Name of site

        public static string GetSafeRoleName(string roleName, string siteName);

        //

        // Summary:

        //     Gets safe version of username.

        //

        // Parameters:

        //   userName:

        //     Username

        //

        //   siteName:

        //     Name of site

        public static string GetSafeUserName(string userName, string siteName);

        //

        // Summary:

        //     Returns the string representation of an object or default value if not

        //

        // Parameters:

        //   value:

        //     Value to convert

        //

        //   defaultValue:

        //     Default value

        public static string GetString(object value, string defaultValue);

        //

        // Summary:

        //     Returns the string representation of an object or default value if not

        //

        // Parameters:

        //   value:

        //     Value to convert

        //

        //   defaultValue:

        //     Default value

        //

        //   culture:

        //     Culture

        public static string GetString(object value, string defaultValue, string culture);

        //

        // Summary:

        //     Returns the string representation of an object or default value if not

        //

        // Parameters:

        //   value:

        //     Value to convert

        //

        //   defaultValue:

        //     Default value

        //

        //   culture:

        //     Culture

        //

        //   format:

        //     Formatting string

        public static string GetString(object value, string defaultValue, string culture, string format);

        //

        // Summary:

        //     Converts the value to specified type

        //

        // Parameters:

        //   value:

        //     Value to convert

        //

        // Type parameters:

        //   ReturnType:

        //     Result type

        public static ReturnType GetValue<ReturnType>(object value);

        //

        // Summary:

        //     Returns true if the object representation matches the Boolean type

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsBoolean(object value);

        //

        // Summary:

        //     Returns true if the object representation matches the Code name

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsCodeName(object value);

        //

        // Summary:

        //     Returns true if the object representation matches the Code name

        //

        // Parameters:

        //   value:

        //     Value to check

        //

        //   useUnicode:

        //     If true, unicode letters are allowed in the codename, otherwise only a-Z

        //     are allowed.

        public static bool IsCodeName(object value, bool useUnicode);

        //

        // Summary:

        //     Returns true if the object representation matches the hexa color code expression

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsColor(object value);

        //

        // Summary:

        //     Returns true if the object representation matches the Double type

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsDouble(object value);

        //

        // Summary:

        //     Returns true if the object representation matches the Double type

        //

        // Parameters:

        //   value:

        //     Value to check

        //

        //   culture:

        //     Culture code

        public static bool IsDouble(object value, string culture);

        //

        // Summary:

        //     Returns true if the object representation matches the Email

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsEmail(object value);

        //

        // Summary:

        //     Returns true if the object representation matches the file name

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsFileName(object value);

        //

        // Summary:

        //     Returns true if the object representation matches the folder name

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsFolderName(object value);

        //

        // Summary:

        //     Returns true if the object representation matches full name form - 'ClassName'.'Identificator'

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsFullName(object value);

        //

        // Summary:

        //     Returns true if the object representation matches the Guid type

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsGuid(object value);

        //

        // Summary:

        //     Returns true if the object representation matches the Identificator

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsIdentificator(object value);

        //

        // Summary:

        //     Returns true if the object representation matches the Integer type

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsInteger(object value);

        //

        // Summary:

        //     Rreturns true if fromDate precedes toDate.

        //

        // Parameters:

        //   fromDate:

        //     Start time

        //

        //   toDate:

        //     End time

        //

        //   nullFriendly:

        //     Tolerate nil dates

        public static bool IsIntervalValid(DateTime fromDate, DateTime toDate, bool nullFriendly);

        //

        // Summary:

        //     Returns true if the object representation matches the Long type

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsLong(object value);

        //

        // Summary:

        //     Returns true if the given value is macro expression. E.g. {%CMSContext.CurrentUser%}

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsMacro(object value);

        //

        // Summary:

        //     Returns true if the object representation matches the positive number

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsPositiveNumber(object value);

        //

        // Summary:

        //     Returns true if the object representation matches the positive number

        //

        // Parameters:

        //   value:

        //     Value to check

        //

        //   culture:

        //     Culture to check

        public static bool IsPositiveNumber(object value, string culture);

        //

        // Summary:

        //     Returns true if the object representation matches given regular expression.

        //

        // Parameters:

        //   value:

        //     Value to check

        //

        //   regExp:

        //     Regular expression

        public static bool IsRegularExp(object value, string regExp);

        //

        // Summary:

        //     Returns true if the given url is valid

        //

        // Parameters:

        //   value:

        //     Url to check

        public static bool IsURL(object value);

        //

        // Summary:

        //     Returns true if the object representation matches the User name

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsUserName(object value);

        //

        // Summary:

        //     Returns true if the object representation matches the critera for valid client

        //     id value

        //

        // Parameters:

        //   value:

        //     Value to check

        public static bool IsValidClientID(object value);

    }

}

Open in new window

0
Comment
Question by:omegalove
  • 7
  • 6
  • 6
19 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33696340
omegalove,

You can use int.TryParse() which takes a default value parameter as well which it appears is all the above is doing so that if the value of the object is null or not an int it uses the default versus generating an exception like int.Parse().  

So try this:
public int MessageId

{

  get { return int.TryParse(ViewState["MessageId"], 0); }

  set { ViewState["MessageId"] = value; }

}

Open in new window

0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33696450
something along these lines:

public int MessageId
    {
        get
        {
            return GetInteger(ViewState["MessageId"], 0);
        }
        set
        {
            ViewState["MessageId"] = value;
        }
    }

    private int GetInteger(object value, int defaultValue)
    {
        try
        {
            return Convert.ToInt32(value);
        }
        catch
        {
            return defaultValue;
        }

    }

Open in new window

0
 

Author Comment

by:omegalove
ID: 33696465
/// <summary>
        /// Related message ID
        /// </summary>
        public int MessageId
        {

            get { return int.TryParse(ViewState["MessageId"], 0); }
            set { ViewState["MessageId"] = value; }
        }

Error      3      Argument 1: cannot convert from 'object' to 'string'      C:\inetpub\wwwroot\PrivateMessaging\PrivateMessaging\Controls\SendMessage.ascx.cs      79      39      PrivateMessaging
Error      4      Argument 2 must be passed with the 'out' keyword      C:\inetpub\wwwroot\PrivateMessaging\PrivateMessaging\Controls\SendMessage.ascx.cs      79      63      PrivateMessaging
Error      2      The best overloaded method match for 'int.TryParse(string, out int)' has some invalid arguments      C:\inetpub\wwwroot\PrivateMessaging\PrivateMessaging\Controls\SendMessage.ascx.cs      79      26      PrivateMessaging
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33696481
You won't be able to use TryParse in here and all errors above is telling you why.
0
 

Author Comment

by:omegalove
ID: 33696487
yes, what do I do ?
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 33696493
Did you try the code snippet I provided?
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33696536
Or you can simply do it like this:
get
{
 if(ViewState["MessageId"]==null){
return 0;
}
else{
return Convert.ToInt32(ViewState["MessageId"]);
}
}

The reason you won't be able to store any thing but int to MessageId and so the above. But there is a catch in there as well. So may be you are better off with first method.
0
 

Author Closing Comment

by:omegalove
ID: 33696589
public int MessageId
    {
        get
        {
            return GetInteger(ViewState["MessageId"], 0);
        }
        set
        {
            ViewState["MessageId"] = value;
        }
    }

    private int GetInteger(object value, int defaultValue)
    {
        try
        {
            return Convert.ToInt32(value);
        }
        catch
        {
            return defaultValue;
        }

    }
this looks good.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33696600
Sorry, I forgot the ToSTring().  Why wouldn't that work guru_sami?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33696614
Thought the whole point was you didn't want to use the helper method.  All you have done is replace it with another helper method that is not in the ValidationHelper class?
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33696652
mwvisa1:
Sorry, I was lazy to provide explanation and said it in a broader sense that it won't work, particularly the way suggested in your solution.

1: ToString() will fail if ViewState["MessageId"] is null
2. second parameter should be "out"
3: The MessageId property is int and TryParse returns "bool" and so the following fails.
                    return int.TryParse(ViewState["MessageId"], 0);

The errors points to all those.

Now it can be used but not as simple as what it looks. You will need some checks and more code just to make TryParse work.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33696662
Nevermind me, I think I was confusing myself there. The second parameter of TryParse is not the default but the variable to put the result into ... must be getting tired.  Best way is to convert and catch exception as shown. Forgive me ...
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33696681
No you are fine.  I was lazy in not testing as I started out with int.Parse which will generate an exception that you can catch and default in the same way you did and then my mind jumped to TryParse and I ran with it without thinking it through and made several mistakes there.This is what I meant to post , but think it suffers the same woes.
public int MessageId
{
  get {
    int value;
    if (!int.TryParse(Convert.ToString(ViewState["MessageId"]), out value))
    {
      value = 0;
    }
    return value;
  }
  set { ViewState["MessageId"] = value; }
}

Open in new window

0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33696698
No worries mwvisa1:
Even my first guess was a solution with TryParse but as I started typing the solution, I realized all that crap and so had to come up with an alternative.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33696734
Actually TryParse does work, tested above. You just have to use Convert.ToString() instead of ToString() which you are right is flawed if object is null.  Convert.ToString() handles the null just fine.  Nothing wrong with your solution of course, so I should have been clear about that ... I was just a little shocked by the acceptance of GetInteger solution only because that was the question was to avoid having to call an outside (non-built-in) method.

Your answer here was what I would have expected to be accepted -- http:#a33696536

But probably I just didn't understand the intent of the question, plus I can't claim to have tested my answer before posting. *smile*
0
 

Author Comment

by:omegalove
ID: 33712755
So what should be the solution?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33712788
Whatever works for you omegalove.  I was just taken a back at first because I read your question to mean you didn't want to use a non-built-in method with the property, but if all you wanted to do was see what the implementation of the helper function should be then that is perfectly fine.  No worries at all.

You were provided many nice and working samples by guru_sami.  If you are interested, I figured out my brain fart and actually put together the proper code for the thought I had -- http:#a33696681.
0
 

Author Comment

by:omegalove
ID: 33712794
0
 

Author Comment

by:omegalove
ID: 33712841
visa if u can help me out on the above question that would be awesome.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to Popup 37 80
call function 8 40
Trouble with References... 5 22
Hidden Field Value 10 34
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now