Solved

VARIABLE IN MANY CLASS IN VB CODE FILE

Posted on 2011-09-21
8
191 Views
Last Modified: 2012-05-12
Hi All,

I add a class to a project.
In that class I add several public class.

The problem when I add some variables to one of the class.

Error      1      Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.       

I want all the variables visible to inside all of the class.

Could it be possible ?

What should I do ?

Thank you.
0
Comment
Question by:emi_sastra
  • 4
  • 4
8 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 36572770
When inside a class you declare something (property, function, sub, field...) as "shared", it means that the shared object is not associated with a specific instance of the class, but it's associated with the class itself. Therefore, you don't need to create a new instance object of the class to access that member, but you can access the member without creating class instance objects.

Let's see an example:
Public Class MyClass
    Public Shared Function SharedHelloWorld() As String
        Return "Hello, world!"
    End Function

    Public Function NotSharedHelloWorld() As String
        Return "Hello, world!"
    End Function
End Class

Later, in your code:
Dim s As String = MyClass.SharedHelloWorld() 'You CAN do this
Dim s2 As String = MyClass.NotSharedHelloWorld() 'You CAN'T do this, it will throw an exception
'To access NotSharedHelloWorld(), you need to do this:
Dim myObject As MyClass = New MyClass
Dim s3 As String = myObject.NotSharedHelloWorld() 'All right

Hope that helps.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 36572805
'Variables declares
Dim strVarA as string
etc...

Public Class Finance

   Public shared Function A() as string
         strVarA not visible here.
   End Function

End Class

Public Class Inventory

End Class
0
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 36572853
As A() Is a shared function, to access strVarA it must be also shared.

So change
Dim strVarA As String

For
Private Shared strVarA As String

Hope that helps.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 36572886
Hi RolandDeschain,

It works.

Thank you very much for your help.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:emi_sastra
ID: 36572945
Hi RolandDeschain,

One problem, the strVarA is not visible at Class Inventory ?

Thank you.
0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 36573240
It must be perfectly visible.

Look at this:

Public Class Class1
    Private Shared m_Var As String = String.Empty

    Public Class Class2
        Public Shared Property Var As String
            Get
                Return m_Var
            End Get
            Set(value As String)
                m_Var = value
            End Set
        End Property
    End Class

    Public Class Class3
        Public Shared Property Var As String
            Get
                Return m_Var
            End Get
            Set(value As String)
                m_Var = value
            End Set
        End Property
    End Class
End Class

With this code, you can do this without any problem:
Class1.Class2.Var = "new value"
MsgBox(Class1.Class3.Var) 'The MsgBox shows "new value"

Hope that helps.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 36573376
Yes, it works.

Thank you very much for your help.
0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 36573471
You're welcome... and we're here to help.

Greetings!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now