Solved

Convert VB to C# - Validate Routing Number Routine

Posted on 2009-07-08
12
2,009 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
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: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

622 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