locked
Shared Functions RRS feed

  • Question

  • User1135810773 posted

    What's the advantage of declaring a function as "Shared"?

    I've noticed that it doesn't require you to instantiate an object to use the method when it declared as "Shared". Is that the only upside?

    Thanks!

    Tuesday, January 27, 2009 11:07 AM

Answers

  • User-504499122 posted

     Ya. Thats the biggest advantage and also, when you say it Shared in VB.net or Static in C#, the function becomes global. So, you can use this function to share or modify data at the global level.

    Look at this article, that describes some differences between shared and regular functions

    http://visualbasic.about.com/od/learnvbnet/a/sharedinstance.htm

    http://www.developerfusion.com/article/1047/new-objectoriented-capabilities-in-vbnet/5/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 27, 2009 11:18 AM
  • User-1995538749 posted

    I'd say that is a pretty big plus. There are many functions you use everyday that are Shared such as Math.Round or String.Format. These shared functions are handy because you DON'T need an instance of the Math or String class in order to use them. Shared functions are excellent to use when your functions doesn't need to access anything else within the class that they reside in.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 27, 2009 11:18 AM

All replies

  • User-504499122 posted

     Ya. Thats the biggest advantage and also, when you say it Shared in VB.net or Static in C#, the function becomes global. So, you can use this function to share or modify data at the global level.

    Look at this article, that describes some differences between shared and regular functions

    http://visualbasic.about.com/od/learnvbnet/a/sharedinstance.htm

    http://www.developerfusion.com/article/1047/new-objectoriented-capabilities-in-vbnet/5/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 27, 2009 11:18 AM
  • User-1995538749 posted

    I'd say that is a pretty big plus. There are many functions you use everyday that are Shared such as Math.Round or String.Format. These shared functions are handy because you DON'T need an instance of the Math or String class in order to use them. Shared functions are excellent to use when your functions doesn't need to access anything else within the class that they reside in.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 27, 2009 11:18 AM
  • User1135810773 posted

     Many thanks to the both of you!

    Tuesday, January 27, 2009 11:25 AM