Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VARIABLE IN MANY CLASS IN VB CODE FILE

Posted on 2011-09-21
8
Medium Priority
?
250 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 2000 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
Independent Software Vendors: 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!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

636 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