Solved

code conversion from vb.net to c#

Posted on 2007-11-13
7
180 Views
Last Modified: 2013-11-26
Here is the vb.net 1.1 code, I want to convert it to c#2.0 , I don't know vb.net.
Please help me.It's extremely urgent.Thanks a million in advance.I'd be grateful to u.

Private Sub GetPaymentHoldIndicator(ByVal sAcctNo As String)
        Dim bRetVal As Boolean = False
        Dim sPymtHoldInd As String = "N"
        Dim sPymtScore As String = ""
        Dim sPymtStartDt As String = ""
        Dim sPymtStopDt As String = ""
        Dim oConn As New tsysConnect
        Dim oCIFS As New TSYS2.TS2.CIFS     'Inquire Payment & Fraud Scores

        Try
            oCIFS.RequestedAccount = sAcctNo
            bRetVal = oConn.PerformInquire(oCIFS)

            If bRetVal Then
                sPymtScore = oCIFS.Payment_Score.Trim
                sPymtStartDt = oCIFS.Payment_Score_Start_Date.Trim
                sPymtStopDt = oCIFS.Payment_Score_Stop_Date.Trim

                If Not IsNumeric(sPymtScore) Then Exit Sub

                If CLng(sPymtScore) > 0 Then                                                                    ' If Payment Score is not "000"
                    If CLng(sPymtScore) <> 999 And CLng(sPymtScore) <> 213 And CLng(sPymtScore) <> 217 Then     ' Exception Case - Ignore
                        If Not IsDate(sPymtStartDt) Then Exit Sub
                        If Not IsDate(sPymtStopDt) Then Exit Sub

                        ' If current date falls between start date and stop date
                        If CDate(sPymtStartDt) <= DateTime.Today Then
                            If CDate(sPymtStopDt) >= DateTime.Today Then
                                sPymtHoldInd = "Y"
                            End If
                        End If
                    End If
                End If

                If sPymtHoldInd = "Y" Then
                    lbPymtHoldInd.Visible = True
                    lPymtHldInd.Visible = False
                    lPymtHoldInd.Text = sPymtHoldInd
                    lPymtScore.Text = sPymtScore
                    lPymtStartDt.Text = sPymtStartDt
                    lPymtStopDt.Text = sPymtStopDt
                Else
                    lbPymtHoldInd.Visible = False
                    lPymtHldInd.Visible = True
                    lPymtHldInd.Text = sPymtHoldInd
                End If
            End If
        Catch ex As Exception
            Dim oError As New Blast2.Errors
            oError.LogToEvent(ex.ToString)
            Throw New Exception(ex.ToString())
        End Try
    End Sub
0
Comment
Question by:amukta
  • 4
  • 3
7 Comments
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20275778
I may have missed something here or there, but since you have some 3rd party components I can't test it against the compiler.  However,  this should get you 90% or more:


void GetPaymentHoldIndicator(string sAcctNo)
{
        bool RetVal = false;
        string PymtHoldInd = "N";
        string PymtScore;
        string PymtStartDt;
        string PymtStopDt;
        tsysConnect oConn = new tsysConnect();
        TSYS2.TS2.CIFS oCIFS = new TSYS2.TS2.CIFS();    //Inquire Payment & Fraud Scores

        try
        {
            oCIFS.RequestedAccount = sAcctNo;
            RetVal = oConn.PerformInquire(oCIFS);

            if (RetVal)
            {
                PymtScore  = oCIFS.Payment_Score.Trim();
                PymtStartDt = oCIFS.Payment_Score_Start_Date.Trim();
                PymtStopDt = oCIFS.Payment_Score_Stop_Date.Trim();

                int z;
                if (!int.TryParse(PymtScore,out z)) return;

                if (Convert.ToInt64(PymtScore) > 0)                             // If Payment Score is not "000"
                {
                    if (Convert.ToInt64(PymtScore) != 999 && Convert.ToInt64(PymtScore) != 213 && Convert.ToInt64(PymtScore) != 217 )  // Exception Case - Ignore
                    {
                        DateTime Start, Stop;
                        if (!DateTime.TryParse(PymtStartDt,out Start)) return;
                        if (!DateTime.TryParse(PymtStopDt,out Stop)) return;

                        // If current date falls between start date and stop date
                        if ( Start <= DateTime.Today &&Stop >= DateTime.Today)
                                PymtHoldInd = "Y";
                       
                    }
                }

                if (PymtHoldInd == "Y")
                {
                    lbPymtHoldInd.Visible = true;
                    lPymtHldInd.Visible = false;
                    lPymtHoldInd.Text = PymtHoldInd;
                    lPymtScore.Text = PymtScore;
                    lPymtStartDt.Text = PymtStartDt;
                    lPymtStopDt.Text = PymtStopDt;
                }
                else
                {
                    lbPymtHoldInd.Visible = false;
                    lPymtHldInd.Visible = true;
                    lPymtHldInd.Text = PymtHoldInd;
               }
            }
        }
        catch (Exception ex )
        {
            Blast2.Errors oError = new Blast2.Errors();
            oError.LogToEvent(ex.ToString());
            throw new Exception(ex.ToString(), ex);
        }
    }
0
 

Author Comment

by:amukta
ID: 20288414
Hi!
Thank you very much!
Instead of
 DateTime Start;
if (!DateTime.TryParse(PymtStartDt,out Start)) return;
                       Can I use
Try{
if(PymtStartDt==DateTime.Parse(PymtStartDt)
}
catch{
return;
}
 ;
and similarly for
DateTime Start;
if (!DateTime.TryParse(PymtStopDt,out Stop)) return
and also for
 int z;
  if (!int.TryParse(PymtScore,out z)) return;
Can I do this s done above?
Please let me know ASAP!
IT'S EXTREMELY URGENT!

THANKS A MILLION IN ADVANCE!
0
 

Author Comment

by:amukta
ID: 20288417
Hi!
Thank you very much!
Instead of
 DateTime Start;
if (!DateTime.TryParse(PymtStartDt,out Start)) return;
                       Can I use
Try{
if(PymtStartDt==DateTime.Parse(PymtStartDt)
}
catch{
return;
}
 ;
and similarly for
DateTime Start;
if (!DateTime.TryParse(PymtStopDt,out Stop)) return
and also for
 int z;
  if (!int.TryParse(PymtScore,out z)) return;
Can I do this as done above?
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20289243
Looking at this line:
    if(PymtStartDt==DateTime.Parse(PymtStartDt)
You're missing a parentheses at the end, and you're comparing a string type to a datetime type, so you might not get the results you expect.  Also, looking back at the original code it converted PymtScore to a number no fewer than five times, and I should have used a long anyway.  You could simplify that whole section like this:

    long score=0;
    try {
        score = long.Parse(PymtScore);

        // If Payment Score is not "000"
        if (score > 0 && score != 999 && score != 213 && score != 217) //exception cases
        {
             // If current date falls between start date and stop date
            if ( DateTime.Parse(PymtStartDt) <= DateTime.Today && DateTime.Parse(PymtStopDt) >= DateTime.Today)
                    PymtHoldInd = "Y";
        }
    }
    catch {
        return;
    }







0
 

Author Comment

by:amukta
ID: 20290731
DateTime Start;
if (!DateTime.TryParse(PymtStartDt,out Start)) return;
  Can I use
Try{
if(PymtStartDt==DateTime.Parse(PymtStartDt).ToString())
}
catch{
return;
}
 ;
and similarly for
DateTime Stop;
if (!DateTime.TryParse(PymtStopDt,out Stop)) return
can i use
try{
if(PymtStopDt==DateTime.Parse(PymtStopDt).ToString())
}
catch{
return;}
and instead of
int z;
  if (!int.TryParse(PymtScore,out z)) return;

Can I use
try{
if(PymtScore==double.Parse(PymtScore))
}
catch{
return;}


Can I do this as done above?
Please let me know.

I didn't undertstand y we need to change the code to the latest oen u've sen tme
Thanks
0
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 500 total points
ID: 20291654
The new code means you only have to convert PymtScore to a numeric type one time instead of five, you only need one try/catch block instead of three, and get's rid of an extra layer of nesting your {} braces.

Now let's look again at this line:
  if(PymtStartDt==DateTime.Parse(PymtStartDt).ToString())

It still won't work, because the .ToString() method is not guaranteed to generate the same string as PymtStartDt.  In fact, by specifying different format options I could create many different versions of the string that all represent that same date.  For example, if PymtStartDt is "11/15/2007" you could tell .ToString() to output "November 15, 2007" instead, as just one example.  What will be used by default depends on the datetime settings in the control panel of the computer where the code runs.  Here's a version that would work:

    DateTime.Parse(PymtStartDt)

No if() statement, no comparison.  It will work because if PymtStartDt cannot be converted to a datetime an exception will be thrown for the enclosing try/catch block.  This will also work for PymtStopDt and PymtScore, though in the case of the latter I would use long.Parse() rather than double.Parse(), since that more closely matches the original.
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20292465
One more thing I thought of:
I can only presume your initial reluctance to use TryParse() results either because you're using .Net 1.1, which doesn't have TryParse(), or because you want the code to match the original vb.  Since you were reluctant to go with my 2nd version, which meets the first hypothesis, I'm inclined to believe it's the latter.  In that case, this being C#, I'd put the try/catch all on one line like this so it more closely resembles the vb line for line:
try {DateTime.Parse(PymtStartDt)} catch {return;}
try {DateTime.Parse(PymtStopDt} catch {return;}
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

791 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