473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Q re Shared Methods

Hi

I'm pretty new to vb.net and have a question because I'm not sure when to
use Shared as part of the method declaration. I understand, from my reading,
that this applies the method to the class rather than each object created
from that class, but I don't understand the applicability of this.

So for example, I have a business object working in the middle tier. I have
a collection class, MyBusinessObjects, which has a Function GetData and
returns a MyBusinessObject.

e.g.

Public Class MyBusinessObjects

Public Function GetData(KeyList() as String) as MyBusinessObject
....

or

Public Shared Function GetData(KeyList() as String) as
MyBusinessObject
...
End Class

So when I'm using this collection in my code, I could

Dim _MyBusinessObjects as New MyBusinessObjects
For Each _MyBusinessObject as MyBusinessObject in
_MyBusinessObjects.GetData(Keylist())
...
Next

or using the Shared declaration I could

For each _MyBusinessObject as MyBusinessObject in
MyBusinessObjects.GetData(Keylist())
...
Next

Perhaps my example isn't a very good one, but when should I use Shared and
when not?

Thanks

Simon
May 22 '07 #1
3 1022
>Perhaps my example isn't a very good one, but when should I use Shared and
>when not?
You can use Shared whenever the method doesn't depend on any instance
data or method.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 22 '07 #2
Simon Woods wrote:
I'm pretty new to vb.net and have a question because I'm not sure when to
use Shared as part of the method declaration. I understand, from my reading,
that this applies the method to the class rather than each object created
from that class, but I don't understand the applicability of this.
Consider the String class.

Dim s As String = "a,b,c,d,e"
Dim array() as String _
= s.Split( "," )

Here, you're using an Instance method (Split) that relies on the data
held in the /specific/ String object that it's called on.

s = String.Format( "{0},{1},{2}" _
array(0), array(1), array(2) )

This is using the Shared method, Format. Format doesn't operate on any
specific String, but it's "related" to other String-like operations so
the logical place to put it is in the String class.

HTH,
Phill W.
May 22 '07 #3
Thanks Phill and Mattias

Phill W. wrote:
Simon Woods wrote:
>I'm pretty new to vb.net and have a question because I'm not sure
when to use Shared as part of the method declaration. I understand,
from my reading, that this applies the method to the class rather
than each object created from that class, but I don't understand the
applicability of this.

Consider the String class.

Dim s As String = "a,b,c,d,e"
Dim array() as String _
= s.Split( "," )

Here, you're using an Instance method (Split) that relies on the data
held in the /specific/ String object that it's called on.

s = String.Format( "{0},{1},{2}" _
array(0), array(1), array(2) )

This is using the Shared method, Format. Format doesn't operate on
any specific String, but it's "related" to other String-like
operations so the logical place to put it is in the String class.

HTH,
Phill W.

May 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
7
by: Mark Kamoski | last post by:
Hi Everyone-- Please help. What are the implications, (in terms of memory, application footprint, resource use, threading, and so forth), of using Shared methods? These Shared classes raise...
14
by: Joe Fallon | last post by:
I am trying to build a Data Access Layer for a SQL Server back end. My class has a number of methods in it. The constructor takes a connection string. I currently have to instantiate an object...
33
by: Joe Fallon | last post by:
1. I have a Base class that has a certain amount of functionality. 2. Then I have a CodeSmith generated class that inherits from the Base class and adds functionality. 3. Since I want to be able...
5
by: Erik Cruz | last post by:
Hello! I have read some threads discussing the fact that a module is in reality a shared class. If I try to create a Public Shared Class in vb.net I receive a compile error. Why? If I can't...
2
by: Aaron Cutlip | last post by:
I have been looking all over and have seen many possible ways to create a Syncronized Shared Function in VB.NET, but I would like some advice that will make my life easier. Given the following...
8
by: gemel | last post by:
I have been reading sime material in .NET that throws some doubt on my understanding of shared procedures. With regard to object programming I assumed that variables declared within a class were...
2
by: Elephant | last post by:
Hello, question, I want to make a COM-compatible .NET DLL (Visual Basic). For this I need an interface. The DLL has a class that must contain methods that can be used without making an instance...
15
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.