Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert VB to C# - Validate Routing Number Routine

Posted on 2009-07-08
12
Medium Priority
?
2,108 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 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
Independent Software Vendors: 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!

 
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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

963 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