locked
Scope of Public variables defined in a module of the startup project. RRS feed

  • Question

  • Hello,

         I have a code module in the startup project that declares public variables and an initialization sub that uses those variables which is called in the Main form load event. The initialization sub reads a text file and uses a streamreader object to assign values read from the file to those public variables. The variables work fine in the project where they are defined but I can't seam to access them in other projects of the same solution. I tried using the imports statement in the other projects in hopes of importing the code module...no luck. I tried adding a reference of the code module to the other projects but I get a circular dependencies error. How do I use those variables defined in the module in all projects of the solution? Should the module be outside of the solution in it's own class library and then use the Imports statement in the projects using those variables? What is the correct way? These variables are used for setting paths/filenames. I know there is topics similar on scope of public variables but it seems like one never fits your scenario. Any help is greatly appreciated.

    Thanks, JCDFCM


    JCDFCM

    Wednesday, March 2, 2016 1:35 AM

Answers

  • auto properties in and class/object

    public class someclass

    Public Property Price As String

    end class

    https://msdn.microsoft.com/en-us/library/dd293589.aspx?f=255&MSPPError=-2147217396

     on the same ticket...

    public class someclass

    Public Property Price As String

    Public Sub New()
        LoadMe()

     End Sub

    private sub LoadMe()

    //stream reader logic 

    //and load object's properties

    End Sub

    End Class

    in a form load

    dim theobj = new someclass()

    The sub New in the someclass executes the code to load the object LoadMe()

    http://www.dotnetperls.com/class-vbnet

    You just created theobj, and you executed code to make the object load itself by code defined by the someclass that is the blueprint for the object that has behavior/methods in the class to load the object. The object takes care of itself. The object is self sufficient and it can take care of itself and its own needs if you program the object to do so.

    Some other class in some other project that has project reference to someclass and knows what someclass is about.

    public class someotherclass

    Public theobj as someclass

    Public Sub New(obj as someclass)
        theobj = obj

     End Sub

    Public sub somepublicmethod()

    dim price = theobj.Price

    end sub

    end class

    back to some form load.

    dim theobj = new someclass()

    dim so = new someotherclass(theobj)

    so.somepublicmethod()

    OR

    new someotherclass(theobj).somepublicmethod() 

    You are setting up a public constructor in all the classes in a projects that need the passed in object.

    You can even do this at the form load.

    new someoterclass(new someclass()).somepublicmethod() 

    Capiche?

    • Marked as answer by jcdfcm Tuesday, March 8, 2016 5:02 AM
    Thursday, March 3, 2016 4:53 AM

All replies

  • If you need to access these variables in multiple projects then to avoid the circular references you could move the module into its own project and add a reference to that project to each project that needs access to it (with appropriate imports).

    Alternatively don't use global variables, define them all in your startup project and pass them to other projects in the class constructors to where they are needed.

    Wednesday, March 2, 2016 1:51 AM
  • Hello,

    Here are some examples done in VS2013.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, March 2, 2016 1:53 AM
  • How about this:
     1) create a new Class in Solution
     2) define public variables in this Class
     3) refer to them from each Projects

    Sorry, this is an idea in my head. I will try later.
    Wednesday, March 2, 2016 1:56 AM
  • I have a code module in the startup project that declares public variables and an initialization sub that uses those variables which is called in the Main form load event. The initialization sub reads a text file and uses a streamreader object to assign values read from the file to those public variables. The variables work fine in the project where they are defined but I can't seam to access them in other projects of the same solution.

    You create a class/object that has public properties and methods that basically loads  the properties/the object it loads itself when it is instantiated New on the Main form load.  All projects have reference know about this class/object and you pass the object around in the class constructor of all classes in all projects that need access the object's public properties.

    Wednesday, March 2, 2016 2:50 AM
  • Hello DA924x,

      Though there have been a few replies, this answer best fit my scenario as I already have a class library that I import into the other projects. You mentioned public properties, are you referring to the get and set format like below?

    Public Property Price() As String
            Get
                Return m_Price
            End Get
            Set(ByVal value As String)
                m_Price = value
            End Set
        End Property

    A private field was declared up front and there is a  Public Shared Function that instantiates the new object then sets the values of all the properties and returns an instance of the object with all the default values.

    Then you import the class library into all the projects that will use this object and instantiate the object.

    I'm thinking of having a Public Shared Function that has a streamreader object that reads a text file and sets the properties representing filename/path variables which will be used in each project, similar to an initialization routine. I'm thinking the app.config settings will store the location of the text file that the streamreader will read and the properties will represent user defined filename/path locations.

    Am I in the ballpark?

    Thanks, JCDFCM


    JCDFCM

    Thursday, March 3, 2016 3:54 AM
  • auto properties in and class/object

    public class someclass

    Public Property Price As String

    end class

    https://msdn.microsoft.com/en-us/library/dd293589.aspx?f=255&MSPPError=-2147217396

     on the same ticket...

    public class someclass

    Public Property Price As String

    Public Sub New()
        LoadMe()

     End Sub

    private sub LoadMe()

    //stream reader logic 

    //and load object's properties

    End Sub

    End Class

    in a form load

    dim theobj = new someclass()

    The sub New in the someclass executes the code to load the object LoadMe()

    http://www.dotnetperls.com/class-vbnet

    You just created theobj, and you executed code to make the object load itself by code defined by the someclass that is the blueprint for the object that has behavior/methods in the class to load the object. The object takes care of itself. The object is self sufficient and it can take care of itself and its own needs if you program the object to do so.

    Some other class in some other project that has project reference to someclass and knows what someclass is about.

    public class someotherclass

    Public theobj as someclass

    Public Sub New(obj as someclass)
        theobj = obj

     End Sub

    Public sub somepublicmethod()

    dim price = theobj.Price

    end sub

    end class

    back to some form load.

    dim theobj = new someclass()

    dim so = new someotherclass(theobj)

    so.somepublicmethod()

    OR

    new someotherclass(theobj).somepublicmethod() 

    You are setting up a public constructor in all the classes in a projects that need the passed in object.

    You can even do this at the form load.

    new someoterclass(new someclass()).somepublicmethod() 

    Capiche?

    • Marked as answer by jcdfcm Tuesday, March 8, 2016 5:02 AM
    Thursday, March 3, 2016 4:53 AM
  • Thanks DA924x,

       I defined a new class that initializes the public properties via a public initialization sub defined in the class definition. The sub uses a StreamReader object that reads a text file defined in the app.config and sets the variables. I then import the new class into whatever project requiring the path variables and then instantiate a new object that carries over those variables in the form load event. Did not use a module. No more hard coded paths....got it.

    Thanks, JCDFCM


    JCDFCM

    Tuesday, March 8, 2016 5:02 AM
  • Thanks DA924x,

       I defined a new class that initializes the public properties via a public initialization sub defined in the class definition. The sub uses a StreamReader object that reads a text file defined in the app.config and sets the variables. I then import the new class into whatever project requiring the path variables and then instantiate a new object that carries over those variables in the form load event. Did not use a module. No more hard coded paths....got it.

    Thanks, JCDFCM


    JCDFCM

    Outstanding,  you make me proud and it's OOP 101. :)

    An object can be like a little independent machine that can take of its own needs with its own methods/behavior that can take care of itself. The class is the blueprint and defines the object's behavior.

    You can send a mobile object across the Web, and it can take care of itself and its own needs on the other side in client/server type applications both directions.

    You should learn about local, anchored and mobile objects, starting at page 29. You don't ever need to use module.vb for anything, you really don't, you  use OOP and objects. :)

    https://www.tgh.org/PDFs/CSLA-chapter1.pdf

    Tuesday, March 8, 2016 5:44 AM