Solved

VARIABLE IN MANY CLASS IN VB CODE FILE

Posted on 2011-09-21
8
232 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Crystal reports login after closing form 3 56
Need help with a query 14 53
VB.net Is there code behind PowerPivot 2 70
Web page design problem 3 41
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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