63634

Checking variable from a different class in C#

Greetings-

I have 2 classes. One is called "Programs" and the other is called "Logs". The class called Programs has public const string m_sEnviron = ""; near the top and I need to check what the m_sEnviron variable is set to through my class called Logs. The variable m_sEnviron will get set from a scheduler called Tidal so how can I check its value from a different class. If this is not the best to do this then please let me know what the better ways are.

Thanks in advance.

Regards,

Namespace NightScripts { class Program { public static string m_sEnviron {get; set;} static void Main(string[] args) { } //Lots of other functions... } class Logs { //I try to get access to m_sEnviron but it will not show after I type Program. } }

Answer1:

Well, m_sEnviron <strong>isn't</strong> a variable (/field) - it is a const; it is <strong>always</strong> "".

If it was a static property (or field), then Programs.m_sEnviron. If it was an instance property (or field) then someInstance.m_sEnviron should work, since it is public - but I would rename it.

I expect you mean it to be a static field; which can work, but you should at least be a little cautious that this doesn't necessarily play nicely if you start using multiple threads, etc. And public fields are generally best avoided (prefer private fields and public properties).

For example:

public static string Environ {get;set;}

would be a public, static property easily accessible as Program.Environ.

Answer2:

const basically makes the variable static and readonly. So public const string m_sEnviron = ""; means that m_sEnviron will <strong>ALWAYS</strong> be the empty string. If you try and change it, you will get an error.

However, to access it from a method in the Logs class, you just access it just like a static variable:

string foo = Programs.m_sEnviron;

Answer3:

If I understand your question correctly, you could specify the class where the variable is located as a static class which would therefore not require instantiation.

Recommend

  • How can I include the Ivy dependency and none of its dependencies?
  • CSS - how to trim text output?
  • Bootstrap collapse within a foreach loop
  • C++ Coin flip program error
  • string.IsNullOrEmpty() Doesn't Seem to Work on a String within a Class within a Class
  • Can you pass an array from javascript to asp.net mvc controller action without using a form?
  • WPF Template Binding in ToggleButton UserControl
  • Hash Code in SQL Server?
  • How to autopopulate a field in SugarCRM form
  • Memory error in python- how to use more memory
  • Login not working in Firefox in Meteor
  • Differences in dis-assembled C code of GCC and Borland?
  • how to find common suffix in java by using method
  • Display java JPanel in a JFrame
  • Inline R code in YAML for rmarkdown doesn't run
  • Thread safety of a fluent like class using clone() and non final fields
  • Retrieve list of sent friend requests from friend_request FQL table
  • MongoDb aggregation
  • How to Cache Real-time Data?
  • Database structure design with variable amounts of fields
  • Algorithm for a smudge tool?
  • Using variable in a value field in jMeter
  • Linq Objects Group By & Sum
  • Spring Data JPA custom method causing PropertyReferenceException
  • Retrieving value from sql ExecuteScalar()
  • How do I fake an specific browser client when using Java's Net library?
  • How to get a value (ex: baseURL) in every Karate feature?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • output of program is not same as passed argument
  • sending/ receiving email in Java
  • Akka Routing: Reply's send to router ends up as dead letters
  • How to delete a row from a dynamic generate table using jquery?
  • KeystoneJS: Relationships in Admin UI not updating
  • Proper way to use connect-multiparty with express.js?
  • Hits per day in Google Big Query
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?