Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 109,716 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 2,304 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




C# equivalent to VB6's Val() function

This is a snippet to mimic VB6's Val() function. It will string all non-numeric characters from a string and return the numeric

Submitted By: PsychoCoder
Actions:
Rating:
Views: 1,455

Language: C#

Last Modified: March 9, 2008
Instructions: Pass the method the string you want to strip. It will return only the numeric values in the provided string. You will need a reference to System.Text.RegularExpressions

Snippet


  1. //Namespace Reference
  2. using System.Text.RegularExpressions;
  3.  
  4. // <summary>
  5. /// Function to mimic the Val() function in legacy VB.
  6. /// </summary>
  7. /// <param name="str">String we want the numeric values from</param>
  8. /// <returns>Integer</returns>
  9. /// <remarks>
  10. /// I chose to go with my own functions because there really
  11. /// isn't an intrinsic method in C# to mimic the Val() function
  12. ///</remarks>
  13. private static int Strip(string value)
  14. {
  15.         string returnVal = string.Empty;
  16.         MatchCollection collection = Regex.Matches(value, "\\d+");
  17.         foreach (Match match in collection)
  18.         {       
  19.                 returnVal += match.ToString();
  20.         }
  21.         return (int)returnVal;
  22. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month