[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Shared class - not sure why i get this warning

I have this class:

1.
Namespace Uni.Web.Data.UserData
    Public Class UserAccess

        Private Shared _DBType As DataProvider
        Public Shared Property DBType() As DataProvider
            Get
                Return _DBType
            End Get
            Set(ByVal value As DataProvider)
                _DBType = value

            End Set
        End Property


        Public Shared Function GetSystems(ByVal userLogin As String) As ArrayList
           .....
        End Function

    End Class
End Namespace

2. In my aspx page, why do I need to do this??
 U_DAL.Uni.Web.Data.UserData.UserAccess.DBType =  U_DAL.DataProvider.IBM  

****If I do this, I get a warning:
        Dim test As U_DAL.Uni.Web.Data.UserData.UserAccess
       test.DBType = U_DAL.DataProvider.IBM  

This is the warning : Access of shared member through an instance; qualifying expression will not be evaluated

3. I looked at this site and I'm not using "new" so, that should work:
http://msdn2.microsoft.com/en-us/library/y6t76186.aspx


0
Camillia
Asked:
Camillia
  • 5
  • 3
1 Solution
 
riyazthadCommented:
This is better practice to use shared function or property thru instance variable. It is good to use as class member rather than instance member.

So in your case better to use

U_DAL.Uni.Web.Data.UserData.UserAccess.DBType =  U_DAL.DataProvider.IBM

rather than creating a type variable (instance)
0
 
riyazthadCommented:
"This is NOT better practice to use shared function or property thru instance variable"

sorry for confusion
0
 
CamilliaAuthor Commented:
but the type variable (instance) should still work..right? or is it changed in 2.0?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
riyazthadCommented:
Farzadw,

I think this will make more clear.

http://msdn2.microsoft.com/en-US/library/4hbsxy95.aspx

0
 
CamilliaAuthor Commented:
it says " 
It is recommended that you do not write code in which you access a shared member through an instance of the class
"

but then in the example, it goes and creates an instance of of "item":
' Create two instances of the class.
    Dim part1 As New Item("keyboard")
    Dim part2 As New Item("monitor")

then calls the shared method : Item.ShareMethod()

0
 
riyazthadCommented:
Yes

But last line Item.SharedMethod , we are calling directly method without creating instance of type 'Item'.

But first two lines part1 and part2 are instances, but we are not calling its shared method, but calling instance method 'InstanceMethod', which is NOT shared method.

Item.ShareMethod()

is just calling its shared method.

So first two calling are recommenting
but not last one becos that is shared method.

in other words

part1.SharedMethod()
or
part2.sharedMethod()

are not recommenting.


0
 
CamilliaAuthor Commented:
ah, now I get it..

Good, i thought i was doing something wrong...

But the name is too long without creating an instance.
0
 
riyazthadCommented:
Yea, Its okay though.

:-)

Thad
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now