开发者

Let us build a common utility class for asp.net together [closed]

It's difficult to tell what is being a开发者_运维问答sked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

Which method would you like to place in tyour common class in asp.net application? A common function which you use in almost all your asp.net project question.


These should have always been there IMO:

/// <summary>
/// Answers true if this String is not null or empty
/// </summary>
public static bool HasValue(this string s)
{
  return !string.IsNullOrEmpty(s);
}

/// <summary>
/// Answers true if this String is either null or empty.
/// </summary>
public static bool IsNullOrEmpty(this string s)
{
  return string.IsNullOrEmpty(s);
}

Because going back to type string.IsNullOrEmpty(value) instead of continuing on feels so damn unnatural, at least to me anyway. Love it or hate it, to me (and I'm the one using it...) it's clear to read and saves me a ton of time, keystrokes and frustration.

Yes, this will probably be closed soon, but, well, I felt like semi-ranting against lack of basic string functions :)


public static bool TryFindControl<T>(this Control control, string id, out T foundControl) where T : class
{
   return (foundControl = control.FindControl(id) as T) != null;
}


Let me start first, This is the function I use in almost all my asp.net projects Function: To check whether string is numeric or not

 public bool IsNumeric(string str)
{
    if (str == null || str.Length == 0)
        return false;
    System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
    byte[] bytestr = ascii.GetBytes(str);
    foreach (byte c in bytestr)
    {
        if (c < 48 || c > 57)
        {
            return false;
        }
    }
    return true;
}


Having a "common" class for functionality is generally not good design. Methods should belong to a class which encapsulates specific functionality.

However, one exception would be static classes containing extension methods, and I generally have this sort of pattern

public static class EnumerableExtensions{} // all extensions for IEnumerable<T>
public static class StringExtensions{} // all extensions for String.


A long time back I got this function somewhere from internet and I still use it, not always to generate random password ;-)

 public static string GeneratePassword(int passwordLength)
    {

        int iZero = 0, iNine = 0, iA = 0, iZ = 0, iCount = 0, iRandNum = 0;
        string sRandomString = string.Empty;

        //' we'll need random characters, so a Random object 
        //' should probably be created...
        Random rRandom = new Random(System.DateTime.Now.Millisecond);

        //' convert characters into their integer equivalents (their ASCII values)
        iZero = Convert.ToInt32("0");
        iNine = Convert.ToInt32("9");
        iA = Convert.ToInt32('A');
        iZ = Convert.ToInt32('Z');

        //' initialize our return string for use in the following loop
        sRandomString = string.Empty;


        //' now we loop as many times as is necessary to build the string 
        //' length we want
        while (iCount < passwordLength)
        {
            //' we fetch a random number between our high and low values
            iRandNum = rRandom.Next(iZero, iZ);

            // ' here's the cool part: we inspect the value of the random number, 
            // ' and if it matches one of the legal values that we've decided upon,  
            // ' we convert the number to a character and add it to our string
            if ((iRandNum >= iZero) && (iRandNum <= iNine) || (iRandNum >= iA) && (iRandNum <= iZ))
            {
                if (iRandNum >= iZero && iRandNum <= iNine)
                    sRandomString = sRandomString + iRandNum.ToString();
                else
                    sRandomString = sRandomString + Convert.ToChar(iRandNum);

                iCount = iCount + 1;
            }

        }
        //' finally, our random character string should be built, so we return it
        return sRandomString;


    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜