Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format Function not working - VB 2008

Status
Not open for further replies.

cdck

Programmer
Nov 25, 2003
281
US
I'm updating a project created by someone who is not with our company currently, and I'm attempting to add a section which formats input before spitting it out. I'm using the Format() function, but it is not applying correctly. Here's testing code I created to try to debug this issue:
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strText As String, intNum As Integer, strDate As String

        strText = Me.txtText.Text
        intNum = Me.txtNum.Text
        strDate = Me.txtDate.Text

        Dim MsgText As String

        MsgText = "Text = " & strText & "; Number = " & intNum & "; Date = " & strDate

        MsgBox(MsgText)

        strText = Format(strText, "@@@@")
        intNum = Format(intNum, "##0.0000")
        strDate = Format(strDate, "MM d yyyy")

        MsgText = "Text = " & strText & "; Number = " & intNum & "; Date = " & strDate

        MsgBox(MsgText)


    End Sub

the first message box displays the values entered in the textboxes correctly; the second code displays @@@@, 0, and MM d yyyy, respectively, instead of displaying formatted values.

I wondered if there is a reference library that may not have been included with the original project, since it was converted from Visual Basic 2006. If so, which library should I look for? If not, can anyone see something wrong with the way I am using the Format() function?

Cheryl dc Kern
 
While the Format function takes an object as its first argument, it expects them to be either a number or a date value to do the conversion correctly. See for more information.

I would also recommend you use the ToString methods exposed by each of the different objects instead of the Format function. Each object type has a ToString method that can be used to convert the value to a string and this is more in line with .Net practices.

While some VB6 code practices can be used in VB.Net, it is in your best interest to convert your code to current .Net coding instead of just trying to upgrade your VB6 code.

(Typed, NOT TESTED)
Code:
Dim strText As String
Dim dteDate Ad DateTime
Dim intNumber As Integer

strText = Me.txtText.Text
dteDate = Convert.ToDate(Me.txtDate.Text)
intNumber = Convert.ToInt32(Me.txtNum.Text)

Dim strMessage As String
strMessage = String.Format("Text = {0}; Number = {1}; Date = {2}", strText, intNumber.ToString("##0.0000"), dteDate.ToString("MM d yyyy"))

MessageBox.Show(strMessage)

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer
 
After adjusting my code based on your answer, I've determined that this is not the correct path to follow to get the result I need (a text string which will always be the same number of characters even if the numbers used to generate it are low). I've adjusted my plan to work this from another angle; this thread can close.

Cheryl dc Kern
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top