Solved

Convert VB to C# - Validate Routing Number Routine

Posted on 2009-07-08
12
1,796 Views
Last Modified: 2013-12-17
I need to write a c# method that will authenticate that a user has actually entered a value that is a bank routing number. I've found a vb method that will do this but I don't know vb. I'm going to start banging away at this but if someone could help I would REALLY appreicate it.

Here is the link that the code came from:
http://www.freevbcode.com/ShowCode.asp?ID=6582

I am trying to convert the code below from vb to c#
Public Function ValidateRoutingNumberDigits(ByVal RoutingNumber As String) As Boolean

'    Electronic Funds Transfer Routing Number Check    

    Dim Sum As Long

    Sum = 3 * Val(Mid$(RoutingNumber, 1, 1)) + 7 * Val(Mid$(RoutingNumber, 2, 1)) + Val(Mid$(RoutingNumber, 3, 1)) + _

          3 * Val(Mid$(RoutingNumber, 4, 1)) + 7 * Val(Mid$(RoutingNumber, 5, 1)) + Val(Mid$(RoutingNumber, 6, 1)) + _

          3 * Val(Mid$(RoutingNumber, 7, 1)) + 7 * Val(Mid$(RoutingNumber, 8, 1)) + Val(Mid$(RoutingNumber, 9, 1))

    ValidateRoutingNumberDigits = ((Sum Mod 10) = 0)

    

End Function

Open in new window

0
Comment
Question by:tatton777
  • 6
  • 4
12 Comments
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 500 total points
ID: 24806760
You can try this courtesy of the linked tool:
http://www.developerfusion.com/tools/convert/vb-to-csharp/
public bool ValidateRoutingNumberDigits(string RoutingNumber)

{

    //    Electronic Funds Transfer Routing Number Check    

    long Sum = 0;

    Sum = 3 * Conversion.Val(Strings.Mid(RoutingNumber, 1, 1)) + 7 * Conversion.Val(Strings.Mid(RoutingNumber, 2, 1)) + Conversion.Val(Strings.Mid(RoutingNumber, 3, 1)) + 3 * Conversion.Val(Strings.Mid(RoutingNumber, 4, 1)) + 7 * Conversion.Val(Strings.Mid(RoutingNumber, 5, 1)) + Conversion.Val(Strings.Mid(RoutingNumber, 6, 1)) + 3 * Conversion.Val(Strings.Mid(RoutingNumber, 7, 1)) + 7 * Conversion.Val(Strings.Mid(RoutingNumber, 8, 1)) + Conversion.Val(Strings.Mid(RoutingNumber, 9, 1));

        

    return ((Sum % 10) == 0);

}

Open in new window

0
 
LVL 1

Author Comment

by:tatton777
ID: 24806946
What is the namespace for Conversion  and String ?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24806985
Those are modules containing procedures Val and Mid respectively.  I don't believe you need to import any namespaces; however, I am currently in VB.NET.  Let me open C#.NET and let you know for sure.
0
 
LVL 1

Author Comment

by:tatton777
ID: 24807019
I'm getting 'Does not exist in the current context' errors for

Conversion
Strings
Sum

Thanks!
0
 
LVL 1

Author Comment

by:tatton777
ID: 24807023
whoops. forget Sum.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 500 total points
ID: 24807030
Those are VB functions, so you can try this:

RoutingNumber.substring(1,1) == Mid
For Val, you can try int.parse.  Val will automatically strip out text and return the numerical value, so you will have to do tests for is numeric or catch number format exception to make equivalent function.  If always numbers, then you should be fine.
0
 
LVL 1

Accepted Solution

by:
tatton777 earned 0 total points
ID: 24807138
Greetings...

I've written some code that works. Here it is if anyone is interested...


    //============================================================

    // RoutingNumberIsAuthentic()

    //============================================================

    public bool RoutingNumberIsAuthentic(string routingNumber)

    {

      int sum = 0;
 

      sum = 3 * Convert.ToInt32(routingNumber.Substring(0, 1)) + 7 *

                Convert.ToInt32(routingNumber.Substring(1, 1)) +

                Convert.ToInt32(routingNumber.Substring(2, 1)) +

            3 * Convert.ToInt32(routingNumber.Substring(3, 1)) + 7 *

                Convert.ToInt32(routingNumber.Substring(4, 1)) +

                Convert.ToInt32(routingNumber.Substring(5, 1)) +

            3 * Convert.ToInt32(routingNumber.Substring(6, 1)) + 7 *

                Convert.ToInt32(routingNumber.Substring(7, 1)) +

                Convert.ToInt32(routingNumber.Substring(8, 1));
 

      lblMessage.Text = "Value is " + sum.ToString() + " and we have no idea if it is valid yet. But it is supposedly : " + ((sum % 10) == 0).ToString();
 

      return ((sum % 10) == 0);

    }

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24807166
Great.

Glad you got it.  Sorry I initially didn't catch the Val/Mid functions as I was demonstrating you can use the tool as it usually will indicate when conversion fails because of unsupported operation, but it probably missed that OR it can be used in C# with some using clause I don't know. :)

Anyway, happy coding!

Best regards,
Kevin
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24982273
It had appeared originally that http:#24807030 helped tatton777 write the code that was the ultimate solution.  Maybe I am mistaken, but thought was an assist.  Especially as the function structure and return was posted here -- http:#24806760.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24982345
Think should be:

Accepted: tatton777 (http:#24807138)

Assisted split:
http:#24806760
http:#24807030
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now