web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Finance forum / How can i use static v...
Finance forum

How can i use static variables

(0) ShareShare
ReportReport
Posted on by

i need to use static variable to get it's value in any class in my solution .

i created a class named Myclass 

public class Myclass {

public static str myVar ;

}

an other class where i need to set a value to my variable 

public class MyOtherClass {

Myclass myclass;

myclass.myVar = "valeur";

}

but i get errors .

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,166 Moderator on at

    Here's how you should use it: blogs.msdn.microsoft.com/.../x-in-ax7-static-members

  • Verified answer
    Community Member Profile Picture
    on at
    Hi Amine,
    Static variables are shared variables that can be accessed via tha double colon (::) notation. The syntax is ClassName::StaticVariableName. Be aware to use static variable as they are shared between classes and can alter that state of the program in an unstable manner if multiple users are using the application.
    public class MyOtherClass 
    { Myclass::myVar = "value"; }
  • Verified answer
    Martin Dráb Profile Picture
    238,286 Most Valuable Professional on at

    Next time, please tell us what errors you got.

    The first bug it that your code in MyOtherClass  isn't inside the method.

    Then you're accessing the variable incorrectly. Static variables are associated with the class, not with an instance, therefore you need to use the type and not the instance. And you need to use :: to access static members. Therefore the assignment should look like this:

    Myclass::myVar = "valeur";

    But I wouldn't ever do it and it wouldn't pass my code review. Public variables are problematic and I strongly recommend that you never use them. Forget that this option exists; it's not wise. For example, what if you later realize that you want to run some extra logic when the value is changed? This can be easily done if you keep the variable non-public and allow its changes only through a (public) method.

    And be very careful with static variables. Are you sure it's absolutely necessary in your scenario? And even if you absolutely need something static, shouldn't you create a singleton object wrapping all the related logic?

  • Community Member Profile Picture
    on at

    thank you for your help but my code in MyOtherClass have to be Inside a method

  • Community Member Profile Picture
    on at

    Thank you for your help.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

News and Announcements

Season of Giving Solutions is Here!

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans