简体   繁体   English

如何使用C#中另一个类的初始化实例对象?

[英]How can I use initialized instance objects from another class in C#?

I have Class1 for constants. 我有常量的Class1。 In Class2, two instances of Class1 are created and initialized by user. 在Class2中,Class1的两个实例由用户创建和初始化。 How can I use these instances (which their values were sat by user) in Program? 如何在Program中使用这些实例(其值由用户使用)?

I have searched the questions, but do not find any related post. 我已经搜索了问题,但没有找到任何相关的帖子。

Class1 which contains the variables: 包含变量的Class1:

namespace project1
{
    public class Class1
    {
        public int var1;
        public double var2;
    }
}

Class2 in which two instances of Class1 are initialized: Class2,其中初始化了Class1的两个实例:

namespace project2
{
    public class Class2
    {
        public void Method2_1()
        {
            var constants1 = new project1.Class1
            {
                var1 = 1,
                var2 = 1.1
            };
        }
        public void Method2_2()
        {
            var constants2 = new project1.Class1
            {
                var1 = 2,
                var2 = 2.2
            };
        }
    }
}

Program in which I want to use the two initialized instances of Class1: 我要在其中使用Class1的两个初始化实例的程序:

namespace Project3
{
    class Program
    {
        static void Main(string[] args)
        {
            //How can I use two instances "constants1" and "constants2" of Class1, separately, which are initialized in Class2 !?
        }
    }
}

Thanks for your time. 谢谢你的时间。

Here is one solution : 这是一种解决方案:

public class Class1
{
    public int var1;
    public double var2;

    public override string ToString(){
        return "var1 : " + var1 + " var2 : " + var2;
    }
}
public class Class2
{
    public Class1 Method2_1()
    {
        return new Class1
        {
            var1 = 1,
            var2 = 1.1
        };
    }
    public Class1 Method2_2()
    {
        return new Class1
        {
            var1 = 2,
            var2 = 2.2
        };
    }
}
public class Program
{
    public static void Main(string[] args)
    {
        //Your code goes here
        var c1 = new Class2();
        var testM1 = c1.Method2_1();
        var testM2 = c1.Method2_2();
        Console.WriteLine("testM1 : " + testM1.ToString());
        Console.WriteLine("testM2 : " + testM2.ToString());

    }
}

You have to know that calling object from another project isn't a real issue. 您必须知道从另一个项目中调用对象不是一个真正的问题。 You just have to include it to your using. 您只需要将其包含在您的使用中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C#中另一个命名空间的对象/类(Visual Studio 2013) - How can I use objects/classes from another namespace in C# (visual studio 2013) 如何正确使用C#Visual Studio 2010中另一个类中的一个类的方法 - How can I properly use methods from a class in another class in C# Visual Studio 2010 如何创建一个我可以使用的类的实例是c#中的多个位置 - How can I create an instance of a class that I can use is more than one place in c# 我可以在C#中从“此”获取子类实例吗? - Can I get child class instance from `this` in C#? C#.NET WinForms如何从一个列表复制类的实例 <class> 到另一个列表 <class> 使用剪贴板 - C# .NET WinForms How do I copy instance of class from one List<class> to another List<class> using Clipboard 如何从 C# 中的另一个类创建对象的 ArrayList - How do I create an ArrayList of objects from another class in C# 如何在另一个 class 方法中使用 class 和构造函数并将它们存储到我的列表中? C# - How can I use a class and constructor in a another class, method and store them to my list? C# C#如何在另一个类字符串中使用类名称 - c# How can i use class name in another class string 来自C#中另一个类的对象 - Objects from another Class In C# C#,Unity:如何为同一构造函数使用不同的类对象? - C#, Unity: How Can I use different class objects for the same constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM