Solved

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

Posted on 2012-03-24
8
872 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 47

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 86

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 86

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 86

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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…

710 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