Solved

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

Posted on 2012-03-24
8
882 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 48

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

 

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month6 days, 18 hours left to enroll

622 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