S. Srinivasa Sivakumar's Blog World

My World! My Passion!

C# Tips

I've always loved VB for its simplicity. When a VB programmer moves to C#/.NET he misses those VB functions that helped him a lot. I wrote few functions to mimic those good old VB functions in C#.

1. Porting Common VB functions to C#: isNumeric()

bool isNumeric(char ch)
{
    //If the given character is in between 0 and 9 then
    //return true, otherwise false
    if (ch >= '0' && ch <= '9')
        return true;
    else
        return false;
}

 

2. Porting Common VB functions to C#: Asc()

int Asc(char ch)
{
    //Return the character value of the given character
    return (int)Encoding.ASCII.GetBytes(S)[0];
}

 
3. Porting Common VB functions to C#: Chr()

Char Chr(int i)
{
    //Return the character of the given character value
    return Convert.ToChar(i);
}

 
4. Porting Common functions to C#: isLower()

bool isLower(char ch)
{
    //If the given character is in between a and z then
    //return true, otherwise false
    if (ch >= 'a' && ch <= 'z')
        return true;
    else
        return false;
}

The same function can also be written as more .NET frieldly:

bool isLower(char ch)
{
    return Char.IsLower(ch));
}

 
5. Porting Common functions to C#: isUpper()

bool isUpper(char ch)
{
    //If the given character is in between A and Z then
    //return true, otherwise false
    if (ch >= 'A' && ch <= 'Z')
        return true;
    else
        return false;
}

The same function can also be written as:

bool isUpper(char ch)
{
    return Char.IsUpper(ch));
}

Comments

Raymond Chen said:

Beware that most of these functions have a very narrow world view. For example, your isLower doesn't work in Europe since it completely misses lowercase accented letters. (Even some English words contain accented letters, such as "naïve" - surely this word is lowercase, isn't it?) And Indic languages have numerals that look different from "0" through "9". Char.IsLower, Char.IsUpper, and Char.IsDigit and/or Char.IsNumeric are preferred here.
# September 17, 2003 10:31 AM

James Avery said:

You might just want to use the VisualBasic namespace, it includes alot of functions like this. (And it is written in C# anway, so there is no harm in using it)

Also, you can use reflector to check out how they wrote these functions, etc.

-James
# September 17, 2003 11:02 AM

Cadmium said:

Aren't most, if not all of these vb functions still available to C# through the vb.net namespaces?
# September 17, 2003 11:03 AM

OmegaSupreme said:

I have to say theres very little I miss about vb ;) but thanks for the functions, they may come in handy.

You could use the visualbasic namespace but its useful to have them in pure c# also because one day you may port your code to a none microsoft implementation, unlikely but you never know.
# September 17, 2003 11:19 AM

Dave Rothgery said:

Just as an FYI -- the Microsoft.VisualBasic namespace is written in VB.NET, at least according to Paul Vick, who ought to know.
# September 17, 2003 12:23 PM

Jeff Gonzalez said:

The IsNumeric function is very narrow, why not just use something like Anakrino to decompile the functionality you miss into a seperate utility dll.
# September 17, 2003 5:52 PM

TrackBack said:

# January 4, 2004 10:01 PM

JackNet (ASP.NET) said:

Thanks your functions, helps solve my problem:

I have a little bit change to your IsNumeric(), please give some advice.

public static bool IsNumeric(string words)
{
char[] chs = words.ToCharArray();

foreach (char ch in chs)
{
// If the given character is in between 0 and 9 then
// return true, otherwise false.
if (ch >= '0' && ch <= '9')
continue;
else
return false;
}
return true;
}
# April 5, 2004 3:19 AM

Phillip Jacobs said:

You are only checking if a single character is numeric. In that case you can use Char.IsNumber(ch) and it will work exactly as you want.

That is not equivalent to the Microsoft.VisualBasic.Information.IsNumeric(string) function which checks any number such as -100, 100, and 100.00 and reports them correctly.

There is a very good discussion on this topic at http://www.tulsadnug.org/DesktopDefault.aspx?tabid=23&frmView=ShowPost&PostID=624
# June 1, 2004 9:56 PM

TrackBack said:

# June 3, 2004 1:13 AM

TrackBack said:

# June 3, 2004 1:16 AM

Todd Moon said:

Your isNumeric method could be better written with a regular expression, such as:

private bool isNumeric(string subject)
{
return(System.Text.RegularExpressions.Regex.IsMatch(subject,"^[-0-9]*.[.0-9].[0-9]*$"));
}

This works with any string, negative numbers, decimals, etc.
# June 30, 2004 5:37 PM

Jayce Ooi said:

Thanks man. Your guide helps me a lot. :)

# August 10, 2007 1:51 AM

Stiven Fabiano da Câmara said:

       public static string ValidateAlfaNumeric(string value)

       {

           char[] chars = value.ToCharArray();

           List<char> charValid = new List<char>();

           foreach (char ch in chars)

           {

               if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))

                   charValid.Add(ch);

           }

           string aux = string.Empty;

           foreach (char ch in charValid)

               aux += ch.ToString();

           return aux;

       }

# August 17, 2007 9:11 PM

Sri said:

Thank you.  You helped me

# September 27, 2007 9:42 AM

Britney said:

Cool beans, you saved me an afternoon of headaches.

# April 29, 2008 12:17 PM

Ofir said:

hi! i have got a problem with char... i need the Aski value of a char but u dont know the syntex so if any one can write it?....

# August 17, 2008 5:05 AM

ForeignTrade said:

ofir, see "Porting Common VB functions to C#: Asc()"

# January 14, 2009 4:16 PM

Yoga For Kids said:

amazing stuff thanx

--------------------------------------------

my website is  

http://shirtxxl.net

Also welcome you!

# November 19, 2010 3:14 AM

ipad accessories online said:

A man is only as good as what he loves.

-----------------------------------

# December 20, 2010 2:19 AM

ipad user reviews said:

Good is good, but better carries it.

-----------------------------------

# December 24, 2010 12:20 PM

best ipad accessories said:

-----------------------------------------------------------

"I entirely concur using the above view, the planet vast internet is definitely without a doubt expanding in on the primary type of conversation round the globe and  it is because of to net sites like this that ideas are spreading so swiftly."

# January 8, 2011 8:28 PM

computer reviews said:

their is often a difficulty in the first site.

# February 11, 2011 12:00 PM

Home Security Monitoring said:

I've been browsing online more than three hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my view, if all site owners and bloggers made good content as you did, the net will be much more useful than ever before.

<b><a href="www.articlism.com/.../ideas-for-buying-a-home-security-camera.html

">Home Security Monitoring

<a/><b/>

# April 10, 2011 12:18 AM

Brigida Macura said:

hank for this excellent web log! I totally appreciate it!

# June 29, 2011 3:41 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)