Access variables from other namespaces

I am new to C # and programming in general, my question is: what do you call a variable that is in a different namespace? if i have this code

public void servicevalues(string _servicename)
{
  string servicename = _servicename;
  string query = string.Format("SELECT * FROM Win32_Service WHERE Name ='{0}'", servicename);
  ManagementObjectSearcher moquery = new ManagementObjectSearcher(query);
  ManagementObjectCollection queryCollection = moquery.Get();
  foreach (ManagementObject service in queryCollection)
  {
    string serviceId = Convert.ToString(service["DisplayName"]);
    bool serviceResult = Convert.ToBoolean(service["Started"]);
  }

and I pass the name of the service, how would I call one or more variable values ​​from another namespace?

+3
source share
2 answers

, , . ( ), - . (, ) .

namespace My.Namespace
{
    public class MyClassA
    {
        public void MyMethod()
        {
            // Use value from MyOtherClass
            int myValue = My.Other.Namespace.MyOtherClass.MyInt;
        }
    }
}

namespace My.Other.Namespace
{
    public class MyOtherClass
    {
        private static int myInt;
        public static int MyInt
        {
            get {return myInt;}
            set {myInt = value;}
        }

        // Can also do this in C#3.0
        public static int MyOtherInt {get;set;}
    }
}
+8

Andy, MyInt, My.Namespace:

using My.Other.Namespace

, MyInt :

int MyValue = MyOtherClass.MyInt
+3

Source: https://habr.com/ru/post/1706286/


All Articles