Use object instantiated in a class in a different one

0

Good morning everyone! Can I use methods or properties of an object that I have instantiated in a namespace / class in another class that is inside another namespace?

    
asked by Edulon 15.06.2017 в 19:43
source

2 answers

1

If the class is public you only need to call it using namespace and the constructor

 X.B b = new X.B(5);

for example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            X.B b = new X.B(5);
            b.printSize();
            Console.ReadKey();
        }
    }
}

namespace X
{
    class B
    {
        int a;

        public B(int a)
        {
            this.a=a;
            Console.WriteLine("hola soy x");         
        }

        public int printSize(){
            Console.WriteLine("hola soy x y mi tamaño es " + this.a);
            return this.a;
        }
    }
}
    
answered by 15.06.2017 в 20:10
0

Of course you can use the instance of an object in another class, it all depends on how you handle the relationship between the objects.

Here I leave you a link on how to call methods in other objects.

And to handle the NameSpace part, here a Microsoft documentation and the copy of the example they put.

using System;

// Using alias directive for a class.
using AliasToMyClass = NameSpace1.MyClass;

// Using alias directive for a generic class.
using UsingAlias = NameSpace2.MyClass<int>;

namespace NameSpace1
{
    public class MyClass
    {
        public override string ToString()
        {
            return "You are in NameSpace1.MyClass.";
        }
    }

}

namespace NameSpace2
{
    class MyClass<T>
    {
        public override string ToString()
        {
            return "You are in NameSpace2.MyClass.";
        }
    }
}

namespace NameSpace3
{
    // Using directive:
    using NameSpace1;
    // Using directive:
    using NameSpace2;

    class MainClass
    {
        static void Main()
        {
            AliasToMyClass instance1 = new AliasToMyClass();
            Console.WriteLine(instance1);

            UsingAlias instance2 = new UsingAlias();
            Console.WriteLine(instance2);

        }
    }
}
// Output: 
//    You are in NameSpace1.MyClass.
//    You are in NameSpace2.MyClass.

And to use it as a normal class, simply instantiate it, as explained Here :

MyClass objeto = new MyClass();

In this case you have to make a constructor for the class:

        class MyClass<T>
    {

        public MyClass()
        {

        }

        public override string ToString()
        {
            return "You are in NameSpace2.MyClass.";
        }
    }

You can consult more Here .

NOTE: In the case of WPF, to be able to use functions of another class it is necessary that the class be static class , therefore, the attributes and functions should be static see .

    
answered by 15.06.2017 в 20:15