11 Replies - 408 Views - Last Post: 03 October 2012 - 08:06 PM Rate Topic: -----

#1 Varlisk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-September 12

Need a little help with, what I think should be, else or elseif

Posted 03 October 2012 - 12:50 PM

To practice if/and/or, we were told to make a program that accepts an area code and phone number prefix and tell if it's local or long distance or intra-LATA.

We were given a few combinations to use, this part works just fine
However, everything else should produce "Long Distance", which is the part I'm having a little trouble with.

Public Class Form1

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtACode.Text = ""
        txtPrefix.Text = ""
        lblCallIs.Text = ""
    End Sub

    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click


        If txtACode.Text = 123 And txtPrefix.Text = 455 Or 456 Then lblCallIs.Text = "Local"
        If txtACode.Text = 234 And txtPrefix.Text = 566 Or 567 Or 568 Then lblCallIs.Text = "Local"
        If txtACode.Text = 234 And txtPrefix.Text = 112 Or 113 Or 114 Then lblCallIs.Text = "Intra-LATA"
        Else lblCallist.text = "Long Distance"
    End Sub
End Class


I just noticed I used the wrong label for the else part, that wasn't the problem but it's fixed now

Is This A Good Question/Topic? 0
  • +

Replies To: Need a little help with, what I think should be, else or elseif

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Need a little help with, what I think should be, else or elseif

Posted 03 October 2012 - 12:51 PM

In the first 3 you're setting lblCallIs.Text but in the else your set lblCallist.text

Is this by design or a typo?


Sorry, didn't see the remark at the bottom of the post.

I'm pretty sure I see the problem, but it looks like _HAWK_ is reading it and I don't want to ninja his post via an edit.

This post has been edited by CharlieMay: 03 October 2012 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Need a little help with, what I think should be, else or elseif

Posted 03 October 2012 - 12:56 PM

Lets not confuse data types - string and Integers.
txtACode.Text = 123 should be txtACode.Text = "123"

When testing expressions with And/Or you need to evaluate the expression in each time: txtACode.Text = "123" is an {expression}

If {expression} AndAlso {expression} OrElse {expression} Then ...


I prefer AndAlso and OrElse for logic statements. I also would consider making arrays of prefixes and then you can check if the array .Contains the prefix.
Was This Post Helpful? 1
  • +
  • -

#4 Varlisk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-September 12

Re: Need a little help with, what I think should be, else or elseif

Posted 03 October 2012 - 01:38 PM

Specifically, the problem i'm having is with elseif...if keeps telling me it must be preceded by a matching if statement, despite being directly below an if statement.
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Need a little help with, what I think should be, else or elseif

Posted 03 October 2012 - 01:48 PM

1. Did you fix the expressions as _HAWK_ pointed out?
2. An If statement when broken between lines must have a matching End If
3. Any ElseIf conditions are contained within an If...End If block or separated by : if done on a single line
If this = that or this = those Then
 Do something
ElseIf this = somethingelse Then
 Do somethingelse
End If


Or
If this = that or this = those then Do Something  Else If this = somehtingelse then do somethingelse


Actually, I was mistaken on the colon.

This post has been edited by CharlieMay: 03 October 2012 - 01:55 PM

Was This Post Helpful? 2
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,411
  • Joined: 29-May 08

Re: Need a little help with, what I think should be, else or elseif

Posted 03 October 2012 - 01:52 PM

vb.net is generally speaking line-oriented statements.
Where the end of the line indicate the end of the statement.

In your code you are using the first variant of the If statement below


If {boolean_Condition} Then {Truth_Statement}
{rest of code}


If {boolean_Condition} Then
 {Truth_Statement}
End If
{rest of code}


If {boolean_Condition} Then
 {Truth_Statement}
Else
 {False_Statement}
End If
{rest of code}


If {boolean_Condition} Then
 {Truth_Statement}
ElseIf {boolean_Condition} Then
 {Truth_Statement}
Else
 {False_Statement}
End If
{rest of code}



You need to put the statements onto a separate line, for it to recognize it as an a complete IF Else End If

This post has been edited by AdamSpeight2008: 03 October 2012 - 01:55 PM

Was This Post Helpful? 2
  • +
  • -

#7 Varlisk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-September 12

Re: Need a little help with, what I think should be, else or elseif

Posted 03 October 2012 - 07:08 PM

I have an update now.

 Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click


        If txtACode.Text = "847" And txtPrefix.Text = "395" Or "589" Then
            lblCallIs.Text = "Local"

        ElseIf txtACode.Text = "262" And txtPrefix.Text = "857" Or "843" Or "889" Then
                lblCallIs.Text = "Local"

        ElseIf txtACode.Text = "262" And txtPrefix.Text = "287" Or "484" Or "564" Then
            lblCallIs.Text = "Intra-LATA"

        Else
            lblCallIs.Text = "Long Distance"

        End If






    End Sub
End Class


I no longer get elseif errors, but it doesn't work correctly either. It produces "Local" for anything I type in.
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,411
  • Joined: 29-May 08

Re: Need a little help with, what I think should be, else or elseif

Posted 03 October 2012 - 07:22 PM

Then this is good point to introduce you to some debugging skills.
Set a breakpoint at the start of the method.
Step through the execution, examine the values of the variables you use.
Do they match what you expect them to be, at that point.

This section of code is not doing what you think it doing.
txtPrefix.Text = "395" Or "589"


Spoiler

Was This Post Helpful? 1
  • +
  • -

#9 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 55
  • View blog
  • Posts: 284
  • Joined: 22-August 11

Re: Need a little help with, what I think should be, else or elseif

Posted 03 October 2012 - 07:30 PM

If txtACode.Text = "847" And txtPrefix.Text = "395" Or "589" Then


This isn't correct syntax.
it should be
If txtACode.Text = "847" Andalso txtPrefix.Text = "395" Orelse txtPrefix.Text = "589" Then

I think personally, I'd probably write it like

If txtPrefix.Text = "395" Orelse txtPrefix.Text = "589" Andalso txtACode.Text = "847" Then

this way, it checks to see if txtPrefix is either 395 or 589, if it is then it check to see if txtAcode is 847, if all this is true, then it does whatever it's supposed to, else it goes to is false path.
Was This Post Helpful? 0
  • +
  • -

#10 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Need a little help with, what I think should be, else or elseif

Posted 03 October 2012 - 07:30 PM

Your still not checking the expression with your Or operation.

This post has been edited by _HAWK_: 03 October 2012 - 07:31 PM

Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,411
  • Joined: 29-May 08

Re: Need a little help with, what I think should be, else or elseif

Posted 03 October 2012 - 07:36 PM

@torind_2000: There is nothing wrong with that syntax it is allowed.
For the OP it is just not doing what they think it does.

And and Or could be the bitwise variants.
Eg
 13 Or 2 => 15    1101 OR  0010 => 1111
 13 And 2 => 2    1101 And 0010 => 0010 


Then doing a implicit conversion to a string.

Also it is good idea to enclose the comparison inside brackets, like you do in mathematics to make the order of evaluation of the expression explicit.

For example
If ((txtPrefix.Text = "395") OrElse (txtPrefix.Text = "589")) AndAlso (txtACode.Text = "847") Then



Or write yourself an extension method
<Extension>
Public Function IsAnyOfThese(of T)(IsThisValue As T, These As T() ) As Boolean
  Return AnyOfThese.Contains(IsThisValue)
End Function


for example
If Prefix.IsAnyOf(These:= {"589","395"}) AndAlso ( AreaCode = "847")) Then


This post has been edited by AdamSpeight2008: 03 October 2012 - 07:47 PM

Was This Post Helpful? 1
  • +
  • -

#12 Varlisk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-September 12

Re: Need a little help with, what I think should be, else or elseif

Posted 03 October 2012 - 08:06 PM

I got it working!
Thanks everyone that helped, and thanks for that debugging link, I've bookmraked it in Chrome and Firefox as I'm sure it will prove invaluable in the future.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1