Results 1 to 11 of 11

Thread: If statement problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    76

    If statement problem

    This should be easy but, I have a shipping system, where the user picks a shipment method, and a shipment weight must be based off the shipment method chosen. i.e If the user chooses 'Standard Air', which is on a drop down list in a combo box, the consignment weight must be =>1 and <1000.

    I have made a variable, dConsignWeight as Double, and converted the textbox to a double with Cdbl(txtConsignWeight.Text).

    Then I try to create the If statement.

    If cmbShipmentMethod.Text = "Standard Air" Then
    dConsignWeight >=1

    This is where I get an error? What am I doing wrong? thanks.

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: If statement problem

    The error?
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    76

    Re: If statement problem

    If I try dConsignWeight >= 1 it says expression not a method

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: If statement problem

    Yes just saw it.
    Then
    dConsignWeight >=1 you are trying to assign >=1 to dConsignWeight.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    76

    Re: If statement problem

    so how do I code it that dConsignWeight must be >=1 o.o

  6. #6
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: If statement problem

    You cannot assign a value that you don't know exactly what value it is.
    A value has a value it cannot be >= or <= .
    You can evaluate the value only.P.E. saying
    If cmbShipmentMethod.Text = "Standard Air" and dConsignWeight >=1
    but you cannot assign dConsignWeight >=1.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  7. #7
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: If statement problem

    I am assuming you have different rates based on weights,

    >=1 and <1000 is a certain amount ?
    >=1000 and <2000 is a certain amount ? etc....

    If the way I have explained it is true, then assign the amount to the range

    if dConsignWeight >=1 and <1000 then
    amount = 50.00

    then refer to amount

  8. #8
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: If statement problem

    if dConsignWeight >=1 and dConsignWeight <1000
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  9. #9
    Hyperactive Member nagasrikanth's Avatar
    Join Date
    Nov 2004
    Location
    India,Hyderabad.
    Posts
    420

    Re: If statement problem

    Just to be more clear.. you have to place if inside an if (in short.. nested if..)
    Code:
    If cmbShipmentMethod.Text = "Standard Air" Then
    if dConsignWeight >=1 and dConsignWeight <1000
      '-- do bla bla...
    end if
    end if
    The Difference between a Successful person and others is not a Lack of Knowledge,
    But rather a Lack of WILL

  10. #10
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: If statement problem

    Actually it's better to have the "and's" instead of another if.
    That way you will not have to also parse the second if.
    In this example it is ok but if you had many conditions to check then i wouldn't recommend nested if's.It will slow your program down.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  11. #11
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: If statement problem

    Quote Originally Posted by sapator View Post
    Actually it's better to have the "and's" instead of another if.
    That way you will not have to also parse the second if.
    In this example it is ok but if you had many conditions to check then i wouldn't recommend nested if's.It will slow your program down.
    What you say is just the opposite of what actually is.

    Nested IFs won't slow down your program. However a single IF with many conditions with AND operator would definitely slow down the program. This is because nexted IFs don't cause all conditions to be evaluated while a single IF with many conditions joined with AND operator must evaluate all conditions irrespective.

    Nested IFs make the code lines longer. But some people find it easier to read and maintain that way. So I won't comment on that aspect, as it is a matter of personal preference.

    If you want to use a single IF with multiple conditions, consider using the new ANDALSO / ORELSE operators to join the conditions. They cause short-circuiting between the conditions and act same as nested IFs.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

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



Click Here to Expand Forum to Full Width