Format string problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Format string problem

  1. #1
    Join Date
    Jan 2010
    Posts
    7

    Format string problem

    My teacher must be doing something I am not. I have followed her instructions and typed it all in as she said so, and I still cant get the boxes to function properly.

    This code doesn't display the text, it only displays "(0,-23)(1,-10:C) (2,-8:C)":
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim fmtstr As String = "(0,-23)(1,-10:C) (2,-8:C)"
    LSTRental.Items.Clear()
    LSTRental.Items.Add("Equipment Half Full")
    LSTRental.Items.Add(String.Format(fmtstr, "1. Rug Cleaner", 16, 24))
    LSTRental.Items.Add(String.Format(fmtstr, "2. Lawn Mower", 12, 18))
    LSTRental.Items.Add(String.Format(fmtstr, "3. Paint Sprayer", 20, 30))

    This section of code keeps giving my the Value of String Type Can't be converted:

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim num As Integer
    Dim amount = ", item="
    Dim cost As Double
    Dim fmtstr As String = "(0,-14)(1,-7:C)(2,-15:C)"
    LSTBill.Items.Clear()
    num = CInt(Item.Text)
    amount = (TLTime.Text)
    Select Case num
    Case 1
    Item = "Rug Cleaner"
    cost = 16
    Case 2
    Item = "Lawn Mower"
    cost = 12
    Case 3
    Item = ("Paint Sprayer")
    cost = 20
    Case Else
    Item = ("")


    Any help at all would be appreciated.
    Last edited by Phil Weber; 02-10-2010 at 01:53 AM.

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Take a look at the format strings on this page: http://msdn.microsoft.com/en-us/library/txafckwd.aspx and see if you can tell how they're different from yours.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    The markers for the format should be delimited by French brackets {}, not parenthesis (). Your code should thus define the string as

    Code:
    Dim fmtstr As String = "{0,-23}{1,-10:C}{2,-8:C}"
    (By the way, French brackets are not available on the standard French keyboards... why do you call them that way in English )

    For the second section, you do not tell us on what line you get the error, which makes it hard to pinpoint the problem. But I see a lot of bad things in that code.

    For the error specified, I suspect that it happens in

    Code:
    num = CInt(Item.Text)
    If the content of the Text property (which is a String) is not a number, say "12A", it cannot be converted to an Integer and you get that error. You might use something as the following to prevent the problem:

    Code:
    If IsNumeric(Item.Text) Then
      num = CInt(Item.Text)
    Else
      MessageBox.Show("Item does not contain a number")
    End If
    For an alternative, look for Integer.TryParse in Help.

    And I suspect that in the project properties, Option Strict is turned Off and Option Strict is Off. Unfortunately, this is the default because Visual Studio is configured for dummies when you use it with Visual Basic. But this is a very bad way to work. Although it makes life easier for amateurs, it lets the system decide on what to do with unknown words and variable names, so you lose a lot of control.

    If I am right, the following line might bring problems:

    Code:
    Dim amount = ", item="
    No type is given there, and since Option Strict is Off, the compiler creates a variable called amount and gives it the value , item=.

    I might be wrong, but I suspect that you were trying to create 2 variables, something as:

    Code:
    Dim amount="", item="" As String
    Declaring 2 variables on the same line often lead to problems of that kind, you should use 1 line for each variable. Also, creating a variable without telling the compiler what its type is can also bring strange problems, even if Option Infer is

    Because of the error in syntax, the variable Item is not declared, but is created on the fly then you use it in Item = "Rug Cleaner", which is usually not a good thing, for reasons I have no time to explain because I am hungry .

    Option Strict should always be on, and Option Infer should always be off. You will have to be a little more careful about your declarations and assignments, but this would give you warnings about potential problems such as what I described.

    Unless you thing that working 2 hours to correct a bug is better than taking 5 or 10 seconds to declare a variable properly.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Jacques: In the U.S., we call them "curly braces." ;-)
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  5. #5
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    Phil.

    Thanks for the info. I suppose that the French brackets I hear about all the time are only used by English canadians... who often have a strange relation with French canadians and tends to put the word French in strange places.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  6. #6
    Join Date
    Jan 2010
    Posts
    7
    Thanx for all the help, so far I fixed the first problem. The second problem is not functioning a little different.

    Updated Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim num As Integer
    Dim amount = ",item ="
    Dim cost As Double
    Dim Fmtstr As String = "{0,-14}{1,-7:C}{2,-15:C}"
    LSTBill.Items.Clear()
    num = CInt(mtxItemNum.Text)
    amount = mtxTLTime.Text
    Select Case num
    Case 1
    mtxItemNum.Text = "Rug Cleaner"
    cost = 16
    Case 2
    mtxItemNum.Text = "Lawn Mower"
    cost = 12
    Case 3
    mtxItemNum.Text = "Paint Sprayer"
    cost = 20

    End Select
    Select Case amount
    Case "H", "h"
    amount = "(Half)"
    Case "F", "f"
    amount = "(Full)"
    cost = cost * 1.5
    End Select
    If (mtxItemNum.Text <> "0") And (amount <> "") Then
    LSTBill.Items.Clear()
    LSTBill.Items.Add("***Receipt***")
    LSTBill.Items.Add(String.Format(Fmtstr, mtxItemNum, cost, amount))
    LSTBill.Items.Add(String.Format(Fmtstr, "Deposit", 30, ""))
    LSTBill.Items.Add("")
    LSTBill.Items.Add(String.Format(Fmtstr, "Total ", cost + 30, ""))
    End If

    End Sub



    The problem now is when I click on the second button to display the meal I get this after the First line of the list(***Receipt***): System.Windows.MaskedTextBox. I asked the professor and she said to use a Masked text box, and i did. I also placed the items in the exactly how she showed, and it doesn't function as her's does. In the First button, if I select a number of the equipment, then time, than click the second button in the first masked text box for the equipment it changes it to text instead of the number.

  7. #7
    Join Date
    Jan 2010
    Posts
    7
    not = now

  8. #8
    Join Date
    Jan 2010
    Posts
    7
    if you want the seminar for the professor I can give you the weblink for it

  9. #9
    Join Date
    Jan 2010
    Posts
    7
    Actually, nevermind I just fixed it myself. I forgot one stupid word in the second add line :P


    Thanx for all the help guys. Ya'all helped me fix the first problem, and gave me clues for the second. I am really learning more from you guys than my professor :P

Similar Threads

  1. select case vs. rtf
    By oscarjaime in forum .NET
    Replies: 23
    Last Post: 02-02-2008, 12:32 PM
  2. Databind Dropdown Asp.net
    By geo039 in forum ASP.NET
    Replies: 3
    Last Post: 08-06-2007, 04:46 PM
  3. multi language
    By imosha in forum VB Classic
    Replies: 7
    Last Post: 08-11-2005, 03:11 PM
  4. Select lower case
    By GARY in forum VB Classic
    Replies: 1
    Last Post: 12-16-2002, 07:53 PM
  5. How to save changes to a msflexgrid
    By Tommy in forum VB Classic
    Replies: 0
    Last Post: 06-23-2002, 04:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center