Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

memory Var with global scope


  • Please log in to reply
5 replies to this topic

#1 Ellen

Ellen

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts
  • LocationRotterdam, Netherlands

Posted 02 October 2012 - 07:16 AM

hello you all, How do I get a memory var so that I can "see" it everywhere in the program I want intThis_Var in class a and class b It's so basic but without the results i want. of course i have a workaround with a public function but example: imports system.io ........ intTHIS_Var as integer = 0 sub main() ....prog code end sub class a end class class b end class

#2 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 02 October 2012 - 08:16 AM

Hi! There are several possibilites for this. I usually do this by defining a static variable in some related class.

1. Basically like this:
public class MyClass
{
   public static int MyNumber;
}
You can then access the integer variable "MyNumber" by the MyClass type (not instances of it) from anywhere in your code like so:
MyClass.MyNumber = 3;
Debug.Print(MyClass.MyNumber);
2. If you need to make a whole instance of MyClass available, I'd write something like this:
public class MyClass
{
   public static MyClass myClass = new MyClass();
   public int MyNumber;
}
Then you can access the static instance of MyClass from anywhere in you code like this:
MyClass.myClass.MyNumber = 3;
Debug.Print(MyClass.myClass.MyNumber);
By adding more variables like "MyNumber" to the MyClass class, you can have several such variables accessible from all across your project.

3. You can accomplish pretty much the same thing by simply defining the whole class as static:
public static class MyClass
{
   public int MyNumber;
}
This way, you cannot create instances of the class but only use it as a globally accessible container for variables. I use this for storing run-time configuration and such.

EDIT: Sorry, I just realized that you're using VB while my example code is C#. The syntax is slightly different in VB but the pattern is the same. This is what number 3 would look like in VB:
Public Shared Class MyClass
        Dim MyNumer As Integer
End Class


#3 Ellen

Ellen

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts
  • LocationRotterdam, Netherlands

Posted 02 October 2012 - 07:09 PM

Thank you, but I must read a var in another namespace so: Namespace A dim strA as string sub main .....code end sub class a code... end class class b code... end class end namespace namespace b class a ........here i need the var strA end class end namespace

#4 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 02 October 2012 - 09:34 PM

You are allowed to reach out from one namespace and into another if that's what you mean. So from your class a of namespace b you should be able to refer to strA of namespace A, basically like this:

namespace b
    class a
 		A.strA = 3
    end class
end namespace
Your original question read How do I get a memory var so that I can "see" it everywhere in the program and I showed you a few examples of how you can do just that. I'm not really sure what you mean by "memory var" since all variables reside in memory one way or the other.

Maybe it's better if you submit an example of how you would like for it to work and point out where it doesn't - please apply code formatting using the the HTML editor button that looks like this "< >" and it gets more readable.

#5 Ellen

Ellen

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts
  • LocationRotterdam, Netherlands

Posted 03 October 2012 - 07:36 AM

Hello hanzibal, thank you for your help. I had tried all your examples without results but this example works:

Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.NetduinoPlus

Module Module1

    Public Public_String_Module1 As String

    Sub Main()

        Public_String_Module1 = "Solved"
        NameSpace_A.Class_A.Class_A_var_A = "This works"
        NameSpace_B.Class_B.Class_B_var_A = "This also"

    End Sub

End Module

Namespace NameSpace_A

    Class Class_A

        Public Shared Class_A_var_A As String

        Shared Sub Sub_Class_A_one()

            Module1.Public_String_Module1 = "This works also"

            NameSpace_B.Class_B.Class_B_var_A = Class_A.Class_A_var_A

        End Sub

    End Class

End Namespace


Namespace NameSpace_B

    Class Class_B

        Public Shared Class_B_var_A As String

        Shared Sub Sub_Class_B_one()

            Module1.Public_String_Module1 = "This works also"
            Class_B_var_A = NameSpace_A.Class_A.Class_A_var_A

        End Sub

    End Class

End Namespace


The magic word was "SHARED"
And the word "static" did not work, but why, i do not know.
Ellen

#6 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 03 October 2012 - 09:52 AM

Hi Ellen! The Static keyword has a whole different meaning in VB than in C# and that's why I used Shared in my VB example earlier. Shared is the VB equivalent of the C# static modifier. I find that you more or less did according to my last VB example. Anyway, good that you solved it! FYI: By using the Static modifier inside a class method (sub or function) in VB, you effectively get a variable that lives throughout the lifetime of the class instance in which it resides. I find this quite handy for sometimes implementing small caches in method scope.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.