Solved

Convert VB to C# - Validate Routing Number Routine

Posted on 2009-07-08
12
1,948 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 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 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