Results 1 to 12 of 12

Thread: Funtion Question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2014
    Posts
    18

    Funtion Question

    If I have function that uses If Then and ElseIF statements. Is there a way to have the result of each ElseIf called up in the main form so that I can have a separate message box displayed for each failed ElseIf? I cant have the messages boxes displayed in the function itself because I use the function for something else that doesnt need the messages boxes if they fail. So Here is my function:

    If FirstDigit.StartsWith("0") Xor FirstDigit.StartsWith("1") Then
    Return False
    ElseIf SecondDigit.StartsWith("9") Then
    Return False
    ElseIf FirstDigitExchange.StartsWith("0") Or FirstDigitExchange.StartsWith("1") Then
    Return False
    ElseIf ExchangeCodeMiddle.StartsWith("1") And ExchangeCodeEnd.StartsWith("1") Then
    Return False
    ElseIf AreaExchangeCode.StartsWith("800555") Then
    Return True
    ElseIf ExchangeCodeFull.StartsWith("555") Then
    Return False

    Else
    Return True

    End If

  2. #2
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Funtion Question

    The solution would be to return an enumerator. And in your main application act accordingly. Or you could throw like a custom exception

  3. #3
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Funtion Question

    so like this.

    If FirstDigit.StartsWith("0") Xor FirstDigit.StartsWith("1") Then
    Throw New Exception("Must start with 1 or 0")
    ElseIf SecondDigit.StartsWith("9") Then
    Throw New Exception("blah")
    ElseIf FirstDigitExchange.StartsWith("0") Or FirstDigitExchange.StartsWith("1") Then
    Throw New Exception("Toph")
    ElseIf ExchangeCodeMiddle.StartsWith("1") And ExchangeCodeEnd.StartsWith("1") Then
    Throw New Exception("lol")
    ElseIf AreaExchangeCode.StartsWith("800555") Then
    Return True
    ElseIf ExchangeCodeFull.StartsWith("555") Then
    Throw New Exception("hdhhdh")

    Else

    Return True

    End If

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2014
    Posts
    18

    Re: Funtion Question

    not to familiar with Throw Exceptions or how to use them to display a message box

  5. #5
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Funtion Question

    Try

    NumberIsValid("")
    Catch Ex As Exception
    Messagebox.Show(Ex.Message.ToString())

    End Try

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Funtion Question

    If the method is intended to convey more than true or false change the return type to something with multiple values, like integer or string. Each of your if's would return a different value.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Funtion Question

    Throwing an exception is probably the worst thing you can do. They are costly and then you have to handle them from your calling code. It is an option, but I wouldn't recommend it.

    I would either break each check into a different function, and then display the message as appropriate. Or create an enumeration type that the function would then return, with which you can then display different messages for.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Funtion Question

    Quote Originally Posted by techgnome View Post
    Throwing an exception is probably the worst thing you can do. They are costly and then you have to handle them from your calling code. It is an option, but I wouldn't recommend it.

    I would either break each check into a different function, and then display the message as appropriate. Or create an enumeration type that the function would then return, with which you can then display different messages for.

    -tg
    I concur. Enums have the advantage because the names of the enumerated values can be used for user notification.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Funtion Question

    Yeah that's why I gave two suggestions, to use an enum or throw exceptions.

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Funtion Question

    Quote Originally Posted by Toph View Post
    Yeah that's why I gave two suggestions, to use an enum or throw exceptions.
    Enumerator is different than Enum.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Funtion Question

    Really? I thought Enum was just a shorter name for Enumerator..
    I guess I was taught wrong.

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Funtion Question

    Enum - Declares an enumeration and defines the values of its members.
    Enumerator - Used to move through the members of a collection.

    An Enums values and names can be Enumerated.

    I thought Enum was just a shorter name for Enumerator..
    No

    I guess I was taught wrong.
    I guess so.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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