dcsimg
Results 1 to 7 of 7

Thread: Need Help With If Statement Nesting

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Location
    Connecticut, USA
    Posts
    8

    Need Help With If Statement Nesting

    I'm having an issue with one of my programs. My goal is to add the prices of whatever variable the user checks. For example, if the user checks off the option "Leather Interior" I want to add $x to the accessories variable, then if they check another box on top of that, it ADDS to the existing variable. What it's currently doing is just taking one of the values, not adding them. Here's what I currently have:
    Code:
    If chStereoSystem.Checked = True Then
                accessoriesHold = accessories + stereoSystem
            ElseIf chLeatherInterior.Checked = True Then
                accessoriesHold = accessories + leatherInterior
            ElseIf chComputerNav.Checked = True Then
                accessoriesHold = accessories + computerNav
            End If
    While we're on the topic, I'm also having trouble formatting decimals into my outputs, an issue I haven't had before, and this is what I have:
    Code:
    lblAccessoriesOut.Text = accessoriesHold.ToString("D2")
    It's throwing the error:
    "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

    Additional information: Format specifier was invalid."

    Any input would be greatly appreciated!

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,525

    Re: Need Help With If Statement Nesting

    You don't want elseif, that will only allow a single condition to be true. Instead you want a series of ifs:-
    Code:
            If chStereoSystem.Checked = True Then
                accessoriesHold = accessories + stereoSystem
            End If
    
            If chLeatherInterior.Checked = True Then
                accessoriesHold = accessories + leatherInterior
            End If
    
            If chComputerNav.Checked = True Then
                accessoriesHold = accessories + computerNav
            End If
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: Need Help With If Statement Nesting

    If you don't like a large series of If's and / or you want your code a little more flexible to add in new accessories, you may want to loop through all of the CheckBoxes and add their value to a running total, using each accessory's .Tag property to hold the value for that particular accessory.

    As to your 2nd problem, you are using the wrong format specifier... 'N' specifies how many fractional positions a decimal / floating point number should contain. Thus if accessoriesHold = 111, then accessoriesHold.ToString("N2") = 111.00.

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Location
    Connecticut, USA
    Posts
    8

    Re: Need Help With If Statement Nesting

    FunkyDexter, that solved the problem! Thank you!

    Pyth, I was wondering why it kept saying format date to me, but I checked online and found that D was the decimal formatter. Anywho, I fixed it by simply using Format(accessories, "0.00"), my programs just about finished, just have to run testing!

  5. #5
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Need Help With If Statement Nesting

    Format() is the old Microsoft Visual Basic legacy function.
    You really should be using the newer VB.NET tools instead.

    messagebox.show(accessories.tostring("0.00"))
    or if you want currancy
    messagebox.show(accessories.tostring("C"))
    Last edited by Gruff; Apr 21st, 2015 at 01:52 PM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,569

    Re: Need Help With If Statement Nesting

    I agree with Pyth007 about using the Tag property of the Checkbox to allow for a bit more flexibility. Take a look at this example for an idea of what he meant:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
    	'Controls
    	Private CheckBox4WheelDrive, CheckBoxPremiumPaint, CheckBoxAlloyWheels, CheckBox22InchWheels, CheckBoxAutomaticTransmission As CheckBox
    	Private FlowLayoutPanelOptions As FlowLayoutPanel
    	Private ButtonCheckout As Button
    	
    	Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    		Dim GroupBoxOptions As GroupBox = New GroupBox With {.Dock = DockStyle.Top, .Size = New Size(1, 100) .Text = "Options"}
    		ButtonCheckout = New Button With {.Location = New Point(5, GroupBoxOptions.Bottom + 5), .Text = "Checkout"}
    		Me.Controls.AddRange({GroupBoxOptions, ButtonCheckout})
    		
    		AddHandler ButtonCheckout.Click, AddressOf ButtonCheckout_Click
    		
    		FlowLayoutPanel = New FlowLayoutPanel With {.Dock = DockStyle.Fill}
    		GroupBoxOptions.Controls.Add(FlowLayoutPanel)
    		
    		CheckBox4WheelDrive = New CheckBox With {.Tag = 250.00, .Text = "4 Wheel Drive"}
    		CheckBoxPremiumPaint = New CheckBox With {.Tag = 150.00, .Text = "Premium Paint"}
    		CheckBoxAlloyWheels = New CheckBox With {.Tag = 150.00, .Text = "Alloy Wheels"}
    		CheckBox22InchWheels = New CheckBox With {.Tag = 50.00, .Text = "22in Wheels"}
    		CheckBoxAutomaticTransmission = New CheckBox With {.Tag = 1000.00, .Text = "Automatic Transmission"}
    		
    		FlowLayoutPanelOptions.Controls.AddRange({CheckBox4WheelDrive, CheckBoxPremiumPaint, CheckBoxAlloyWheels, CheckBox22InchWheels, CheckBoxAutomaticTransmission})
    	End Sub
    	
    	Private Sub ButtonCheckout_Click(ByVal sender As Object, ByVal e As EventArgs)
    		Dim totalOptions As Double = 0
    		
    		For Each cb As CheckBox In FlowLayoutPanelOptions.Controls.OfType(Of CheckBox)
    			If cb.Checked Then
    				totalOptions += Convert.ToDouble(cb.Tag)
    			End If
    		Next
    		
    		MessageBox.Show(totalOptions.ToString("C"))
    	End Sub
    
    End Class

  7. #7
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: Need Help With If Statement Nesting

    Although D is called the "decimal" format, its name is somewhat misleading in that the "decimal" just refers to the digits 0-9 and not to a Decimal datatype. In fact the D specifier won't even work with numbers with a fractional portion, just integer types:

    Quote Originally Posted by From MSDN
    The "D" (or decimal) format specifier converts a number to a string of decimal digits (0-9), prefixed by a minus sign if the number is negative. This format is supported only for integral types.

    The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. If no precision specifier is specified, the default is the minimum value required to represent the integer without leading zeros.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width