20141219-constructor, passing values between classes, 20141219 Constructor

Source: Internet
Author: User

20141219-constructor, passing values between classes, 20141219 Constructor

Constructor: The function executed during initialization,

Use constructors to pass values between two classes (can be any type, string, int, class can regard them as objects)

Constructors can carry parameters and have multiple overload methods.

Function parameters can only be used inside the function. Therefore, an external variable is required to accept the value to be passed.

See the following code,

Main function:

Class Program {static void Main (string [] args) {// you can directly include parameters during initialization. Class1 a1 = new Class1 (); // initialize Class1 Class2 a2 = new Class2 (a1); // initialize Class2 with the parameter a1 (Class1 class) Console. writeLine (a2.Mingzi); // print the variable Mingzi in Class2. // Print the result: Zhang San Class1 b1 = new Class1 (); b1.gaizhi ("Li Si"); // call the gaizhi function. The value of the variable name in Class1 is Class2 b2 = new Class2 (b1); // initialize Class2 with the parameter b1 (Class1 class) Console. writeLine (b2.Mingzi); // print the result: Li Si Class1 c1 = new Class1 (); c1.gaizhi ("Wang Wu"); // call the gaizhi function. The value of the variable name in Class1 is Class2 c2 = new Class2 (b1); // The included parameter is still b1 Console. writeLine (c2.Mingzi); // print the result: Li Si Class2 c3 = new Class2 (c1); // The parameter c1 Console. writeLine (c3.Mingzi); // print the result: Wang Wu Console. readLine ();}}

Class1 class:

Class Class1 {public string name; // external variable public Class1 () // constructor {name = "James";} // function gaizhi (string type parameter) public void gaizhi (string ming) {name = ming ;}}

Class2 class:

Class Class2 {public string Mingzi; // external variable public Class2 () // constructors {} public Class2 (string name) // other overload methods of the constructor, the parameter is of the string type {// The parameter in the function can only be used in the function. An external variable is required to accept the parameter before Mingzi = name can be used externally; // the value of an external variable is assigned, point Mingzi to name. } Public Class2 (Class1 classname) // other overload methods of the constructor. The parameter is of the class (Class1) type {Mingzi = classname. name; // assign a value to an external variable and point Mingzi to Class1.name. }}

Object 1 = Object 2 point object 1 to object 2

Through constructors, we can operate on objects in another class in other classes.

For example, text search and replacement.

The following code describes how to search for a specified text and select a specified text,

Here is part of the code in the Search dialog box:

Public partial class chazhao: Form {public chazhao () {InitializeComponent ();} private jsb chuangti; public chazhao (string ss, jsb chuangti00) {InitializeComponent (); detail = ss; // write the selected text in the main window to the query input box cz_czshuru chuangti = chuangti00; // upload the main form. } Private void czxiayige_Click (object sender, EventArgs e) {int chang, weizhi; string czsr; czsr = cz_czshuru.Text; // search for text chang = cz_czshuru.Text.Length in the input box; // text length: weizhi = chuangti. shurukuang. text. indexOf (czsr); // find the text-compliant index chuangti in the main form. shurukuang. select (weizhi, chang); // Select the text chuangti that matches the main form. focus (); // obtain the Focus of the main form} // load private void chazhao_Load (object sender, EventArgs e) {}// cancel the private void cz_quxiao_Click (object sender, EventArgs e) {this. close ();}}

Here is part of the code in the main form

Public partial class jsb: Form {public jsb () {InitializeComponent ();} private void search ToolStripMenuItem_Click (object sender, EventArgs e) {// when initializing the Search dialog box, the parameter shurukuang is the name of the text box in the main form.
// Two parameters in the brackets. The text content in the main form is selected in the front, and the "this" parameter is followed by the main form. Chazhao cz = new chazhao (this. shurukuang. SelectedText, this );
Cz. Owner = this; // The Setting dialog box is its subwindow (the searching form is the subwindow of the notepad form) cz. Show (); // display the searching dialog box }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.