user2447032
user2447032

Reputation: 49

call a string to a class then call that class?

I need to call a private string from one class & put it within a second class. In my main method call on the second class I then need to print the strings for the first and second class... I know the second class call on the first class, I just don't know the code line to do that, then take that to the main class.

class nameFirst
{
    private string name_1= "zach";

    public string name_1 {
        get { return name_1; }
    }
}

class lastName
{
    nameFirst name1 = new nameFirst();
    private string lastName_1 = "clare";

    public string lastName_1 {
        get { return lastName_1; }
    }
}

class fullName
{
    public static void main(string [] args)
    {
        lastName last1 = new lastName();
        Console.WriteLine("Full name is {0} {1}", )
    }
}

Upvotes: 0

Views: 72

Answers (1)

Nate222
Nate222

Reputation: 886

class lastName
{
    nameFirst name1 = new nameFirst();
    private string lastName_1 = "clare";

    public string firstName_2 {
        get { return name1.name_1; }
    }
    public string lastName_1 {
        get { return lastName_1; }
    }
}

You can expose the name_1 property from the nameFirst class in the lastName class.

class fullName
{
    public static void main(string [] args)
    {
        lastName last1 = new lastName();
        Console.WriteLine("Full name is {0} {1}", last1.firstName_2, last1.lastName_1);
    }
}

It's certainly ugly, but it gets the job done.

Upvotes: 2

Related Questions