简体   繁体   English

将对象传递给另一个类?

[英]Passing an object to another class?

I've recently been looking into constructors, Im currently trying to pass a object to another class file, The way im doing it is like this: 我最近一直在研究构造函数,Im目前正在尝试将一个对象传递给另一个类文件,Im的执行方式如下所示:

class Program
{
    static void Main(string[] args)
    {
        Class1 objPls = new Class1();

        objPls.nameArray[0] = "jake";
        objPls.nameArray[1] = "tom";
        objPls.nameArray[2] = "mark";
        objPls.nameArray[3] = "ryan";

        Echodata form2 = new Echodata(objPls);
    }
}

class Class1
{
    public string[] nameArray = new string[3];
}

class Echodata
{
    public Class1 newobject = new Class1();

    public Echodata(Class1 temp)
    {
        this.newobject = temp;
    }

    // so now why cant i access newobject.namearray[0] for example?
}

Problem is i cant access the object to get into the array.. 问题是我无法访问该对象以进入阵列。

What methods of passing objects are there? 有哪些传递对象的方法? I was told this is roughly a way to do it and have been experimenting for a while to no avail. 有人告诉我,这是一种大致的实现方式,并且进行了一段时间的尝试无济于事。

Not sure what it is you cannot do. 不知道这是什么,你不能做。 For example your code with this modification works, or at least compiles. 例如,经过此修改的代码可以运行,或者至少可以编译。

   class echodata
    {
        public Class1 newobject = new Class1();

        public echodata(Class1 temp)
        {
            this.newobject = temp;
        }

        // so now why cant i access newobject.namearray[0] for example?
        // What kind of access do you want?

        public void method1()
        {
            newobject.nameArray[0] = "Jerry";
        }

    }

You have an issue where your code will throw an error when trying to set the "ryan" string on the fourth index of the array. 您有一个问题,尝试在数组的第四个索引上设置“ ryan”字符串时,代码将引发错误。 You initially set the array to be of length 3. 您最初将数组设置为长度3。

In your EchoData class you can access the nameArray object without an issue but you must be accessing it within a method or in the constructor. 在EchoData类中,您可以毫无问题地访问nameArray对象,但是您必须在方法或构造函数中对其进行访问。 You cannot be manipulating it's content outside of these. 您不能操纵这些内容以外的内容。

Keep in mind that within your EchoData class you will not see the values you set inside of your Main method. 请记住,在EchoData类中,您将看不到在Main方法中设置的值。

It's hard to tell since you haven't included a complete, compilable sample, and you haven't explained exactly what "can't access" means (do you get an error? what is it?) 很难说,因为您还没有提供完整的,可编译的示例,也没有确切解释“无法访问”的含义(出现错误了吗?是什么?)

However, my guess is that you are attempting to access the passed in objects fields from the class level based on your code. 但是,我的猜测是您正在尝试根据代码从类级别访问传入的对象字段。

ie, you are trying to do this: 即,您正在尝试这样做:

class Echodata
{
    public Class1 newobject; // you don't need to initialize this

    public Echodata(Class1 temp)
    {
        this.newobject = temp;
    }

    newobject.newArray[0] = "Can't do this at the class level";
}

You can only access nameArray from within a member method. 您只能从成员方法中访问nameArray。

class Echodata
{
    public Class1 newobject; // you don't need to initialize this

    public Echodata(Class1 temp)
    {
        this.newobject = temp;
    }

    public void DoSOmething() {
        newobject.newArray[0] = "This works just fine";
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM