Val Function and overload notice-VBForums
Results 1 to 13 of 13

Thread: Val Function and overload notice

  1. #1

    Thread Starter
    Lively Member Mahdi Jazini's Avatar
    Join Date
    Feb 2014
    Location
    Iran / Tehran
    Posts
    86

    Post 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?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,605

    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:
    1. Public Sub DoSomething(arg As String)
    2.     '...
    3. End Sub
    4.  
    5. Public Sub DoSomething(arg As Integer)
    6.     '...
    7. End Sub
    8.  
    9. Public Overloads Sub DoSomethingElse(arg1 As String)
    10.     '...
    11. End Sub
    12.  
    13. Public Overloads Sub DoSomethingElse(arg1 As String, arg2 As String)
    14.     '...
    15. End Sub
    Note that the `Overloads` keyword is optional but, if you use it on one overload, you must use it on all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member Mahdi Jazini's Avatar
    Join Date
    Feb 2014
    Location
    Iran / Tehran
    Posts
    86

    Re: Val Function and overload notice

    Quote Originally Posted by jmcilhinney View Post
    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:
    1. Public Sub DoSomething(arg As String)
    2.     '...
    3. End Sub
    4.  
    5. Public Sub DoSomething(arg As Integer)
    6.     '...
    7. End Sub
    8.  
    9. Public Overloads Sub DoSomethingElse(arg1 As String)
    10.     '...
    11. End Sub
    12.  
    13. Public Overloads Sub DoSomethingElse(arg1 As String, arg2 As String)
    14.     '...
    15. 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) ?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,119

    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
    * 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??? *

  5. #5

    Thread Starter
    Lively Member Mahdi Jazini's Avatar
    Join Date
    Feb 2014
    Location
    Iran / Tehran
    Posts
    86

    Re: Val Function and overload notice

    Quote Originally Posted by techgnome View Post
    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

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,119

    Re: Val Function and overload notice

    So what was it about jmc's post that wasn't sufficient?

    -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??? *

  7. #7

    Thread Starter
    Lively Member Mahdi Jazini's Avatar
    Join Date
    Feb 2014
    Location
    Iran / Tehran
    Posts
    86

    Re: Val Function and overload notice

    Quote Originally Posted by techgnome View Post
    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

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    4,948

    Re: Val Function and overload notice

    Quote Originally Posted by Mehdi Jazini View Post
    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

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,605

    Re: Val Function and overload notice

    Quote Originally Posted by Mehdi Jazini View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member Mahdi Jazini's Avatar
    Join Date
    Feb 2014
    Location
    Iran / Tehran
    Posts
    86

    Re: Val Function and overload notice

    Quote Originally Posted by ident View Post
    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

  11. #11
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,941

    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,605

    Re: Val Function and overload notice

    Quote Originally Posted by Mehdi Jazini View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,119

    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
    * 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??? *

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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.