Convert VB to C# - Validate Routing Number Routine

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:

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
You can try this courtesy of the linked tool:
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

tatton777Author Commented:
What is the namespace for Conversion  and String ?
Kevin CrossChief Technology OfficerCommented:
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.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

tatton777Author Commented:
I'm getting 'Does not exist in the current context' errors for


tatton777Author Commented:
whoops. forget Sum.
Kevin CrossChief Technology OfficerCommented:
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.
tatton777Author Commented:

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kevin CrossChief Technology OfficerCommented:

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 CrossChief Technology OfficerCommented:
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.
Kevin CrossChief Technology OfficerCommented:
Think should be:

Accepted: tatton777 (http:#24807138)

Assisted split:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.