-
Nov 18th, 2014, 02:56 AM
#1
Thread Starter
Lively Member
Val Function and overload notice
in the MSDN, this is the description of VAL() function:
This member is overloaded
What's overload property? why part of functions or methods in .NET have this property?
-
Nov 18th, 2014, 03:27 AM
#2
Re: Val Function and overload notice
You really should have been able to look this up for yourself but here goes. Overloading simply means that you have multiple methods or properties that have the same name but different numbers and/or types of parameters. Here are two examples of overloaded methods:
vb.net Code:
Public Sub DoSomething(arg As String) '... End Sub Public Sub DoSomething(arg As Integer) '... End Sub Public Overloads Sub DoSomethingElse(arg1 As String) '... End Sub Public Overloads Sub DoSomethingElse(arg1 As String, arg2 As String) '... End Sub
Note that the `Overloads` keyword is optional but, if you use it on one overload, you must use it on all.
-
Nov 18th, 2014, 05:23 AM
#3
Thread Starter
Lively Member
Re: Val Function and overload notice
Originally Posted by jmcilhinney
You really should have been able to look this up for yourself but here goes. Overloading simply means that you have multiple methods or properties that have the same name but different numbers and/or types of parameters. Here are two examples of overloaded methods:
vb.net Code:
Public Sub DoSomething(arg As String) '... End Sub Public Sub DoSomething(arg As Integer) '... End Sub Public Overloads Sub DoSomethingElse(arg1 As String) '... End Sub Public Overloads Sub DoSomethingElse(arg1 As String, arg2 As String) '... End Sub
Note that the `Overloads` keyword is optional but, if you use it on one overload, you must use it on all.
thank you very much for your comprehensive info.
but can you tell me why they said: VAL() function is (overloaded) ?
-
Nov 18th, 2014, 06:51 AM
#4
Re: Val Function and overload notice
Because it is... it can accept a string, or an object, or.... it can accept parameter of different types... result, it needs to be overloaded.
-tg
-
Nov 18th, 2014, 07:11 AM
#5
Thread Starter
Lively Member
Re: Val Function and overload notice
Originally Posted by techgnome
Because it is... it can accept a string, or an object, or.... it can accept parameter of different types... result, it needs to be overloaded.
-tg
exactly...
now i have received a good example on the stackoverflow.com about it:
Code:
Public Overloads Function Val(ByVal InputStr As String) As Double
' -or-
Public Overloads Function Val(ByVal Expression As Object) As Double
' -or-
Public Overloads Function Val(ByVal Expression As Char) As Integer
thank you
-
Nov 18th, 2014, 11:22 AM
#6
Re: Val Function and overload notice
So what was it about jmc's post that wasn't sufficient?
-tg
-
Nov 18th, 2014, 01:07 PM
#7
Thread Starter
Lively Member
Re: Val Function and overload notice
Originally Posted by techgnome
So what was it about jmc's post that wasn't sufficient?
-tg
yes thank you jmc but in the new example the base of the example was val function...
For beginners like me the 2nd example was better
-
Nov 18th, 2014, 02:29 PM
#8
Re: Val Function and overload notice
Originally Posted by Mehdi Jazini
yes thank you jmc but in the new example the base of the example was val function...
For beginners like me the 2nd example was better
Beginners should not be learning Val as it has no place in vb.net
-
Nov 18th, 2014, 04:48 PM
#9
Re: Val Function and overload notice
Originally Posted by Mehdi Jazini
yes thank you jmc but in the new example the base of the example was val function...
For beginners like me the 2nd example was better
It doesn't matter what method it is. An overloaded method is an overloaded method. What's the difference between your Val example and my DoSomething example? There really isn't any. It really seems like the word "example" has stopped meaning something that illustrates a principle that can then be applied elsewhere and now means the exact bit of code that you want to write.
-
Nov 19th, 2014, 01:17 AM
#10
Thread Starter
Lively Member
Re: Val Function and overload notice
Originally Posted by ident
Beginners should not be learning Val as it has no place in vb.net
it seems you don't know about val benefits... when the entered value is not a number then the val function returns 0 instead of error but in cint and clng it returns error... cint(val(value)) is so better than cint(value) so val has place in vb.net as it had in vb6
-
Nov 19th, 2014, 02:09 AM
#11
Re: Val Function and overload notice
As has been said, Val should be avoided. If you want to cast a value to an integer then you can check if it is a number or not before casting it.
-
Nov 19th, 2014, 04:05 AM
#12
Re: Val Function and overload notice
Originally Posted by Mehdi Jazini
it seems you don't know about val benefits... when the entered value is not a number then the val function returns 0 instead of error but in cint and clng it returns error... cint(val(value)) is so better than cint(value) so val has place in vb.net as it had in vb6
That's bad advice. It's not true that Val returns zero when the input is not a number. I would say that "1x2" is not a number and yet Val would return 1.0 when fed that input. If you actually want to use zero when the input is not numeric then you should be using the TryParse method of the appropriate numeric type. The only thing Val is actually good for is when you want to pick the number off the front of a String that is, or may not be, completely numeric. That's rare but you might perhaps want to take the house number off the front of an address. Again though, if you do that and the address doesn't begin with a number then you get zero, which might not be what you want.
-
Nov 19th, 2014, 06:42 AM
#13
Re: Val Function and overload notice
The only reason Val exists is because it is something that has been part of the BASIC language lexicon since... well since the days of BASIC.
-tg
Tags for this Thread
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
|