Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 915
  • Last Modified:

VB6 Format , DateDiff , DateAdd emulation functions (VB.net)

Hi,

I need to create in VB.net an exact duplicate of some old VB6 functions:

1) Format
2) DateAdd
3) DateDiff

Actually my VB.net project call these functions many times and I like to replace these function with other functions that use only VB.net instructions.

Someone can write 3 functions that emulate 100% these vb6 old functions ?

Thank you !
0
luca345
Asked:
luca345
  • 4
  • 3
1 Solution
 
Martin LissRetired ProgrammerCommented:
What version of VB.Net are you using? All of them are available in VS2003 and 1 and 3 are available in VS2008.
0
 
luca345Author Commented:
Vb.net 2010
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
For Format(), you call ToString() against many things and simply pass in the desired format:

    Dim decAmount As Decimal = 5436.92
    Dim strNumber as String = dec.ToString("$#,##0.00")

For DateAdd(), you can simply use one of the AddXXX() methods of the DateTime instance:

    Dim dt As DateTime = DateTime.Now
    dt = dt.AddHours(5)

For DateDiff(), you subtract two DateTime instances to get a TimeSpan:

    Dim dtA As DateTime = DateTime.Now    
    Dim dtB As DateTime = DateTime.Now.AddMinutes(32)
    Dim ts As TimeSpan = dtB.Subtract(dtA)
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
luca345Author Commented:
Hi , Idle_Mind ,

Thank you for your response.

I use 'Format' with date , time and number and I need a function that work fine both situations.

Can you create an equivalent function of 'format' in Vb.net called for example S_Format ?

Can you do the some for the other 2 functions please ?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can just overload your own Format() to receive different types and then call the VB.Net ToString() equivalent...
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Label1.Text = Format(DateTime.Now, "dd MMM yy @ hh:mm:ss")
        Label2.Text = Format(9500.25, "$#,##0.00")
    End Sub

    Public Function Format(ByVal dt As DateTime, ByVal frmt As String) As String
        Return dt.ToString(frmt)
    End Function

    Public Function Format(ByVal dec As Decimal, ByVal frmt As String) As String
        Return dec.ToString(frmt)
    End Function

End Class

Open in new window

0
 
luca345Author Commented:
Hi Idle_Mind ,

I have replaced in my project these functions but now the project get errors when these fucntions call.

I don't think is possible emulate this function with only 1 or 2 rows ... probably there are other factors ..
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Personally, I wouldn't try to replicate the old Format() function like this.  I'd learn the new equivalents and change the code so it uses the new .Net approaches.
0
 
luca345Author Commented:
Very good !
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now