Solved

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

Posted on 2012-03-24
8
846 Views
Last Modified: 2012-06-21
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
Comment
Question by:luca345
  • 4
  • 3
8 Comments
 
LVL 45

Expert Comment

by:Martin Liss
ID: 37761134
What version of VB.Net are you using? All of them are available in VS2003 and 1 and 3 are available in VS2008.
0
 

Author Comment

by:luca345
ID: 37761189
Vb.net 2010
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37761419
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
 

Author Comment

by:luca345
ID: 37761455
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37761477
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
 

Author Comment

by:luca345
ID: 37761529
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 37761552
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
 

Author Closing Comment

by:luca345
ID: 37776640
Very good !
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now