문제

This is a pretty beginner question but I'm stumped and I can't figure out how to get what I want from this. I have my first class that obtains information (database/textfile/whatever) but I want it to relay that information into Class2.

For instance, the first:

 public class Class1
{
    private int first;
    private string firstString;
    private bool isTrue;

    public void SomeMethod() 
    {
        first = 1;
        firstString = "FirstString";
        isTrue = true;
    }
}

Here SomeMethod sets all the attributes that I need to pass into Class2.

ClassTwo looks like

  public class Class2
{
    private int first;
    private string FirstString;
    private bool isTrue;

    private int second;
    private string SecondString;
    private bool isFalse;

    public void SomeOtherMethod() 
    {

    }
}

Here what I want is for SomeOtherMethod() to set the first set of attributes with the values that were set in Class1's SomeMethod(). So that I can create an object of type Class2 and add what I want to it.

도움이 되었습니까?

해결책

As some other commentators stated, you really should reuse your data definitions. Something like this can get you started:

public class Class1
{
    private int _myInt;
    private string _myString;
    private bool _myBool;

    public void SomeMethod() 
    {
        _myInt = 1;
        _myString = "FirstString";
        _myBool = true;
    }
}

public Class2
{
    private Class1 _first = new Class1();
    private Class1 _second = new Class1();

    public void SetFirst(Class1 obj)
    {
       _first = obj;
    }
}

and then use the classes like this:

Class1 c1 = new Class1();
Class2 c2 = new Class2();

c1.SomeMethod();
c2.SetFirst(c1);

다른 팁

You have to define get accessors for the properties of Class1 because they are all unreachable from outside the class and Class2 needs to use their values. Defining public properties with get accessors can be useful:

private int first;
public int First
{
    get
    {
        return first;
    }
}

Having every property in Class1 defined like this, you can access the values. After calling SomeMethod, two objects' properties can be equalized in two simple ways (See also: Signatures and overloading):

public void SomeOtherMethod() 
{
    Class1 tempClass = new Class1();
    tempClass.SomeMethod();

    this.first = tempClass.first;
    this.FirstString = tempClass.firstString;
    this.isTrue = tempClass.isTrue;
}

public void SomeOtherMethod(Class1 myClass) // Overloaded method
{
    this.first = myClass.first;
    this.FirstString = myClass.firstString;
    this.isTrue = myClass.isTrue;
}

Even though the techniques above seem like to be what you asked for, the best is to initialize a class's properties using constructors. This way, you don't have to call SomeMethod each time you create a Class1 object, and you can also set its default values whenever a new one is created. Also, giving more general names to the properties will save you from duplicates. I write some code to provide you an understandable syntax that will prevent future problems of non-accessibility and repetition.

public class Class1
{
    private int number;
    public int Number
    {
        get { return number; }
    }

    private string name;
    public string Name
    {
        get { return name; }
    }

    private bool isTrue;
    public bool IsTrue
    {
        get { return isTrue; }
    }

    public Class1()
    {
        number = 1;
        name = "FirstString";
        isTrue = true;
    }

    public Class1(int value1, string value2, bool value3)
    {
        number = value1;
        name = value2;
        isTrue = value3;
    }
}

public class Class2
{
    private Class1 firstClass;
    private Class1 secondClass;

    public Class2()
    {
        firstClass = new Class1();
        secondClass = new Class1(2, "SecondString", false);
    }
}

If you're going to define many Class1 objects in Class2, then a solution such as an array or a list becomes must. I'll give a short example, see MSDN List page.

private List<Class1> class1List = new List<Class1>();
class1List.Add(new Class1());
class1List.Add(new Class1(2, "SecondString", false));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top