Solved

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

Posted on 2012-03-24
8
856 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 46

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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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
 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

808 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