omegalove
asked on
another way to write this?
return ValidationHelper.GetIntege r(ViewStat e["Message Id"], 0);
is there another way to write the above line without ValidationHelper?
//public int MessageId
//{
// get
// {
// return ValidationHelper.GetIntege r(ViewStat e["Message Id"], 0);
// }
// set
// {
// ViewState["MessageId"] = value;
// }
//}
is there another way to write the above line without ValidationHelper?
//public int MessageId
//{
// get
// {
// return ValidationHelper.GetIntege
// }
// 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);
}
}
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;
}
}
ASKER
/// <summary>
/// Related message ID
/// </summary>
public int MessageId
{
get { return int.TryParse(ViewState["Me ssageId"], 0); }
set { ViewState["MessageId"] = value; }
}
Error 3 Argument 1: cannot convert from 'object' to 'string' C:\inetpub\wwwroot\Private Messaging\ PrivateMes saging\Con trols\Send Message.as cx.cs 79 39 PrivateMessaging
Error 4 Argument 2 must be passed with the 'out' keyword C:\inetpub\wwwroot\Private Messaging\ PrivateMes saging\Con trols\Send Message.as cx.cs 79 63 PrivateMessaging
Error 2 The best overloaded method match for 'int.TryParse(string, out int)' has some invalid arguments C:\inetpub\wwwroot\Private Messaging\ PrivateMes saging\Con trols\Send Message.as cx.cs 79 26 PrivateMessaging
/// Related message ID
/// </summary>
public int MessageId
{
get { return int.TryParse(ViewState["Me
set { ViewState["MessageId"] = value; }
}
Error 3 Argument 1: cannot convert from 'object' to 'string' C:\inetpub\wwwroot\Private
Error 4 Argument 2 must be passed with the 'out' keyword C:\inetpub\wwwroot\Private
Error 2 The best overloaded method match for 'int.TryParse(string, out int)' has some invalid arguments C:\inetpub\wwwroot\Private
You won't be able to use TryParse in here and all errors above is telling you why.
ASKER
yes, what do I do ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
get
{
if(ViewState["MessageId"]=
return 0;
}
else{
return Convert.ToInt32(ViewState[
}
}
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.
ASKER
public int MessageId
{
get
{
return GetInteger(ViewState["Mess ageId"], 0);
}
set
{
ViewState["MessageId"] = value;
}
}
private int GetInteger(object value, int defaultValue)
{
try
{
return Convert.ToInt32(value);
}
catch
{
return defaultValue;
}
}
this looks good.
{
get
{
return GetInteger(ViewState["Mess
}
set
{
ViewState["MessageId"] = value;
}
}
private int GetInteger(object value, int defaultValue)
{
try
{
return Convert.ToInt32(value);
}
catch
{
return defaultValue;
}
}
this looks good.
Sorry, I forgot the ToSTring(). Why wouldn't that work guru_sami?
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?
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["Me ssageId"], 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.
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["Me
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.
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 ...
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; }
}
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.
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.
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*
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*
ASKER
So what should be the solution?
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.
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.
ASKER
And I appreciate the help.
https://www.experts-exchange.com/questions/26484191/Need-to-rewrite-the-following.html
https://www.experts-exchange.com/questions/26484191/Need-to-rewrite-the-following.html
ASKER
visa if u can help me out on the above question that would be awesome.
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:
Open in new window