When to use Shared vs Instance References

Element6

Well-known member
Joined
Feb 16, 2010
Messages
85
Programming Experience
5-10
As I stated before in my Sorting questions.

When is it advisable to use Shared Functions vs Instance Functions References.

The problem I posed was that in a threaded application if I was sorting an array of 999999999 elements I typically want to call a shared function that handles this; however in .NET the way to sort is through a .Sort method which you pass an array to be sorted and a function handler; or Comparisson(t of).

My impression is that if in order to do a ClosestToN Sort I would have to calculate the value of N in the array first; then sort and then subtract the value of N from the sorted array.

JohnH suggested passing the value of N as a Property in a Class that contained a reference to a sort Algorithm.

The benchmark suggested in a 1 thread model it would work faster (which I knew that it would through math, but the accessors I was speculative on; I turned out to be wrong it worked fine either way).

My issue is in a Multi-Thread application creating instances for an accessor may cause memory to fill quickly if I am dealing with 1000's of threads.

Is it better in this case to use a slower Shared function, then a faster Instance Function?
 
VB.NET:
Public Shared Function ClosestToNSort(ByVal AnArrayOfNumbers As Decimal(), ByVal n As Decimal) As Decimal()
        ' Optimized for speed against 0 based N
        Dim Sorte As New Sorter
        Sorte.n = n
        Array.Sort(AnArrayOfNumbers, AddressOf Sorte.ClosestToN)

        Return AnArrayOfNumbers
    End Function

Is this the optimal solution? It's alot cleaner then a global variable but I still think there is something inheirantly wrong with creating an instance of a object just to run a function. I could be wrong, just looking for input here.
 
Last edited:
MSDN states Shared members are thread safe, but does not guarranty thead safety with instance members.
 
Hmm. That seems to be a problem; in this case does that mean that this is guarenteed? Since the instance is inside the Shared function? I don't see an issue with it; but I am curious if there is another solution that might be more elegant.
 
Wouldn't you put both ClosestToNSort and ClosestToN in the same class/dll?
 
No ClosestToN sort is a Caller from a "Global Functions" module and ClosestToN is the Callee (the addressOf refered in a different class); in this case, which I don't like, the function is called through an instance.

Dim Sorte As New Sorter

I do not make it a habit as a matter of principal of adding things to classes for arbitary reasons; I rather have a seperate class so at a later date I can just remove the reference when the solution decides to be invalid because I did it as a hack to solve some weird technical issue/ or some weird business issue.

In this case it's a technical issue. I know there has to be a better way of doing this. This is a much better solution then what I was doing; but there is something still fundamentally wrong with having to declare an instance of an object to run a function so you can pass a value. If not I have to live with it and I'll get over it; it'll just be a thorn on the rose stem if you will.
 
Back
Top