Solved

VARIABLE IN MANY CLASS IN VB CODE FILE

Posted on 2011-09-21
8
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 1

Author Comment

by:emi_sastra
ID: 36572886
Hi RolandDeschain,

It works.

Thank you very much for your help.
0
 
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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 …
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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