Skip to main content

Notifications

Announcements

No record found.

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 Microsoft Employee

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 .

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How can i use static variables

    Thank you for your help.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How can i use static variables

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

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    RE: How can i use static variables

    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?

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How can i use static variables
    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"; }
  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: How can i use static variables

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

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

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans