Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert VB to C# - Validate Routing Number Routine

Posted on 2009-07-08
12
Medium Priority
?
2,069 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
12 Comments
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 2000 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 60

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
Amazon Web Services EC2 Cheat Sheet

AWS EC2 is a core part of AWS’s cloud platform, allowing users to spin up virtual machines for a variety of tasks; however, EC2’s offerings can be overwhelming. Learn the basics with our new AWS cheat sheet – this time on EC2!

 
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
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 2000 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 60

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 60

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 60

Expert Comment

by:Kevin Cross
ID: 24982345
Think should be:

Accepted: tatton777 (http:#24807138)

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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