Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Private Sub StockETDStdCalc()

    Dim DateSOP As Date
    Dim DateSOPFormat, DateDaySOP As String

    DateSOP = Nothing
    DateSOPFormat = Nothing
    DateDaySOP = Nothing

    DateSOP = DateTime.Now
    DateSOP = DateSOP.AddDays(+5)

    If Format(DateSOP, "ddd") = "6" Then
        DateSOP = DateSOP.AddDays(+2)
    ElseIf Format(DateSOP, "ddd") = "7" Then
        DateSOP = DateSOP.AddDays(+1)
    End If

    DateSOPFormat = Format(DateSOP, "MM") & "-" & Format(DateSOP, "dd") & "-" & Format(DateSOP, "yyyy")

    TxtBx_ETD.Text = DateSOPFormat

End Sub


I need to convert VB to C#. I not sure if the coding is correct or not, can someone help me to check this? Thanks a lot.

What I have tried:

protected void StockETDStdCalc()
{

    DateTime DateSOP;
    String DateSOPFormat, DateDaySOP;

    DateSOP = null;
    DateSOPFormat = null;
    DateDaySOP = null;

    DateSOP = DateTime.Now;
    DateSOP = DateSOP.AddDays(5);

    if (DateSOP == DateSOP.AddDays(6)) -- I THINK THIS LINE NOT CORRECT
    {
        DateSOP = DateSOP.AddDays(2);
    }
    else if (DateSOP == DateSOP.AddDays(7)) -- I THINK THIS LINE NOT CORRECT
    {
        DateSOP = DateSOP.AddDays(1);
    }

    DateSOPFormat = DateTime.Now.ToString("MM/dd/yyyy");

    TxtBx_ETD.Text = DateSOPFormat;


}
Posted
Updated 28-Jul-18 20:01pm
Comments
[no name] 28-Jul-18 22:55pm    
These conditions are not clear in C# program you are adding days and changing the value of DateSOP object multiple times. I believe in old code you are checking the day of week and then adding some days based on condition, if this is true then you can access the day of ween like DateSOP.DayOfWeek in C# it will give name of day e.g Monday.
Member 13318869 30-Jul-18 22:46pm    
yes, I wanna do that in my coding. Can you show me what line is incorrect?

1 solution

That isn't even close - either you don't understand VB or you don't understand C#...

Try using a code converter - there are several online, but this one's quite good: Code Converter C# to VB and VB to C# – Telerik[^]
It gave me this:
C#
private void StockETDStdCalc()
{
    DateTime DateSOP;
    string DateSOPFormat, DateDaySOP;

    DateSOP = default(DateTime);
    DateSOPFormat = null;
    DateDaySOP = null;

    DateSOP = DateTime.Now;
    DateSOP = DateSOP.AddDays(+5);

    if (Strings.Format(DateSOP, "ddd") == "6")
        DateSOP = DateSOP.AddDays(+2);
    else if (Strings.Format(DateSOP, "ddd") == "7")
        DateSOP = DateSOP.AddDays(+1);

    DateSOPFormat = Strings.Format(DateSOP, "MM") + "-" + Strings.Format(DateSOP, "dd") + "-" + Strings.Format(DateSOP, "yyyy");

    TxtBx_ETD.Text = DateSOPFormat;
}
Which is poor quality C#, but it at least going to do the same job as the input code...
 
Share this answer
 
Comments
Member 13318869 30-Jul-18 22:49pm    
I use Telerik also to convert vb to c# or c# to vb but sometimes it works, sometimes got error.. thanks for idea btw..
OriginalGriff 31-Jul-18 1:20am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900