C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Become a C# Expert!

Join 306,871 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,670 people online right now. 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: 7,023

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


relax 2009-05-19 04:42:01

Public Function EncodeData(ByVal StringText As String) As String On Error GoTo ErrorManager Dim Hexval As String Dim StartPos As Integer Dim NewString As String Dim StringLength As String Dim i As Integer Dim CharVal Dim Unival StringLength = Len(StringText) StartPos = 1 i = 1 NewString = "" While i = 4) Then c = "" c = ChrW(HexToDecimal(carac)) toConvert = Mid(toConvert, ind2 + 1, Len(toConvert)) buffer = buffer & c ind = InStr(1, toConvert, "%") If (ind > 1) Then buffer = buffer & Mid(toConvert, 1, ind - 1) toConvert = Mid(toConvert, ind, Len(toConvert)) ind = InStr(1, toConvert, "%") End If Else buffer = buffer & Mid(toConvert, InStr(1, toConvert, aux), 1) toConvert = Mid(toConvert, (InStr(1, toConvert, aux) + 1), Len(toConvert)) End If End If '(ind2 < 2) Then End If Loop DecodeData = buffer Else DecodeData = "" End If Exit Function ErrorManager: DecodeData = "" End Function Private Function HexToDecimal(ByVal strHex As String) As Double On Error GoTo ErrorManager Dim lngDec As Double Dim i As Integer For i = 0 To Len(strHex) - 1 lngDec = lngDec + Val("&H" & Mid(strHex, Len(strHex) - i, 1)) * 16 ^ i Next i HexToDecimal = lngDec Exit Function ErrorManager: HexToDecimal = "" End Function


Add comment


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





Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month