?
Solved

VARIABLE IN MANY CLASS IN VB CODE FILE

Posted on 2011-09-21
8
Medium Priority
?
241 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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