Initial Exercise C #

I came up with a different method, trying to solve the exercise in the book "Head First C #" than this solution. I am new to C # and programming in general, and I want to understand if and why the book method is better than mine.

I will talk in detail about the problem, then I will provide an answer and an answer. Please give me a detailed and simple / clear answer as you can!

Create a program with an elephant. Make two instances of Elephant and then change the reference values ​​pointing to them without getting all instances of the Elephant collecting garbage. Make a form (Windows Forms Application) with 3 buttons. The first button says Lloyd, the second says Lucinda, and the third says Swap. By clicking on the Lucinda button or the Lloyd button, a new window opens that displays the message "My ears are inches high." The name of the popup is "(Name) says ...", with a name that is either Lucinda or Lloyd.

It is intended to create an Elephant class with an integer field called EarSize, and a string field Name. WhoAmI () is the one that displays the message popup. Create two elephant examples and links; Elephant fields are added to Form1 and are called Lloyd and Lucinda. They are initialized;

lucinda = new Elephant() { Name = "Lucinda", EarSize = 33 }; lloyd =
new Elephant() { Name = "Lloyd", EarSize = 40 };

Make the Lloyd and Lucinda buttons. A call to the Lloyd lloyd.WhoAmI()button and a Lucinda button call lucinda.WhoAmI().

. , , swap, Lloyd Lucinda " ". , "Swap", Lucinda Lloyd , Lloyd, Lucinda . "", "" . , ... .

- , :

using System.Windows.Forms;

class Elephant 
{
  public int EarSize;
  public string Name;

  public void WhoAmI()
  {
     MessageBox.Show("My ears are " + EarSize + " inches tall.", Name  + " says...");
  }
}

**************************** ***************** ************

public partial class Form1 : Form 
{
  Elephant lucinda;
  Elephant lloyd;

  public Form1()
  {
    InitializeComponent();
    lucinda = new Elephant() { Name = "Lucinda", EarSize = 33 };
    lloyd = new Elephant() { Name = "Lloyd", EarSize = 40 };
  }

  private void button1_Click(object sender, EventArgs e)
  {
    lloyd.WhoAmI();
  }

  private void button2_Click(object sender, EventArgs e)
  {
    lucinda.WhoAmI();
  }

  private void button3_Click(object sender, EventArgs e)
  {
    Elephant holder;
    holder = lloyd;
    lloyd = lucinda;
    lucinda = holder;
    MessageBox.Show("Objects Swapped");
  }
}

************************ ******************** *********

- , . , . My Elephant , , Form1 , , .

public partial class Form1 : Form
{
    Elephant lucinda;
    Elephant lloyd;
    public int counter = 0;

    public Form1()
    {
        InitializeComponent();
        lucinda = new Elephant() { Name = "Lucinda", EarSize = 33 };
        lloyd = new Elephant() { Name = "Lloyd", EarSize = 40 };
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (counter % 2 == 1)
        {
            lucinda.WhoAmI();
        }
        else
        {
            lloyd.WhoAmI();
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (counter % 2 == 1)
        {
            lloyd.WhoAmI();
        }
        else
        {
            lucinda.WhoAmI();
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        counter += 1;
        MessageBox.Show("Objects Swapped");           
    }
}

, , , .

, !

+4
1

, , , :

:

Elephant, , , Elephant,

, , , , , .

, :

  private void button3_Click(object sender, EventArgs e)
  {
    Elephant holder;
    holder = lloyd;
    lloyd = lucinda;
    lucinda = holder;
    MessageBox.Show("Objects Swapped");
  }

. , , , . , , holder, , .

, , , . , holder.

, , . , , , , , , if (counter % 2 == 1). , 10. !

, , , !

+4
source

Source: https://habr.com/ru/post/1671487/


All Articles