-
Nov 22nd, 2014, 04:34 PM
#1
Thread Starter
Junior Member
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
-
Nov 22nd, 2014, 04:44 PM
#2
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
-
Nov 22nd, 2014, 04:57 PM
#3
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
-
Nov 22nd, 2014, 05:31 PM
#4
Thread Starter
Junior Member
Re: Funtion Question
not to familiar with Throw Exceptions or how to use them to display a message box
-
Nov 22nd, 2014, 05:59 PM
#5
Re: Funtion Question
Try
NumberIsValid("")
Catch Ex As Exception
Messagebox.Show(Ex.Message.ToString())
End Try
-
Nov 23rd, 2014, 08:38 AM
#6
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.
-
Nov 23rd, 2014, 10:01 AM
#7
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
-
Nov 23rd, 2014, 10:54 AM
#8
Re: Funtion Question
Originally Posted by techgnome
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.
-
Nov 23rd, 2014, 12:06 PM
#9
Re: Funtion Question
Yeah that's why I gave two suggestions, to use an enum or throw exceptions.
-
Nov 23rd, 2014, 12:19 PM
#10
Re: Funtion Question
Originally Posted by Toph
Yeah that's why I gave two suggestions, to use an enum or throw exceptions.
Enumerator is different than Enum.
-
Nov 23rd, 2014, 12:25 PM
#11
Re: Funtion Question
Really? I thought Enum was just a shorter name for Enumerator..
I guess I was taught wrong.
-
Nov 23rd, 2014, 03:37 PM
#12
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|