A struct can be a member of another struct:
(1)
Struct phoneBook
{
Public string name;
Public uint age;
Public string phone;
Public struct address
{
Public string city;
Public string street;
Public uint no;
}
}
However, there is a problem:
How do I assign values to the members of the structure struct?
The following is an example:
PhoneBook M_1;
M_1.name = Console. ReadLine ();
......
M_1.address.city = Console. ReadLine ();
......
In this case, the compilation reports an error:
Error CS0572: "address": The type cannot be referenced through the expression. Please try "phoneBook. address ".
Here, M_1.address should be incorrectly referenced because address is only a type name rather than a member name of phoneBook, so it cannot be referenced.
(2)
Another way is to assign a value to him:
PhoneBook M_1;
M_1.name = Console. ReadLine ();
......
PhoneBook. address ad;
Ad. city = Console. ReadLine ();
......
It is okay to assign values in this way, but what is the relationship between ad and M_1? It is basically two objects that you don't want to do.
In this case, we cannot solve the problem at present. We can only find a solution in the struct itself.
(3)
So what is the definition of struct like this?
Struct phoneBook
{
Public string name;
Public uint age;
Public string phone;
Public struct address
{
Public string city;
Public string street;
Public uint no;
} Ad;
}
Note: When declaring the address type, declare its object ad as a member of phoneBook, which should be okay in C ++.
But it still cannot be compiled: the mark ";" in the class, structure, or interface member declaration is invalid.
I don't know why, maybe c # doesn't support
(4)
The last method is to take out the struct In the struct and use its object as a member of another struct:
Struct phoneBook
{
Public string name;
Public uint age;
Public string phone;
Public address ad;
}
Struct address
{
Public string city;
Public string street;
Public uint no;
}
This is certainly okay, but it is not relevant to the original problem.
(5)
After consultation, I learned that I can write like this:
Struct phoneBook
{
Public string name;
Public uint age;
Public string phone;
Public address ad;
Public struct address
{
Public string city;
Public string street;
Public uint no;
}
}
In fact, this is what method (3) has to do, But method (3) is that the c ++ method does not work in C #. In essence and method (4) but the same.
The following is all the code for reference:
Version 2 code
Using System;
Struct phoneBook
{
Public string name;
Public uint age;
Public string phone;
Public address ad;
}
Struct address
{
Public string city;
Public string street;
Public uint no;
}
Class Welcome
{
Static void Main ()
{
Try
{
PhoneBook M_1;
While (true)
{
Console. WriteLine ("enter your name :");
M_1.name = Console. ReadLine ();
If (M_1.name! = "")
Break;
Else
{
Console. WriteLine ("Enter it! ");
}
}
While (true)
{
Try
{
Console. WriteLine ("Enter your age :");
M_1.age = Convert. ToUInt32 (Console. ReadLine ());
Break;
}
Catch (FormatException e)
{
Console. WriteLine ("enter a number of-9! ");
}
}
While (true)
{
Console. WriteLine ("Enter your phone number :");
M_1.phone = Console. ReadLine ();
If (M_1.phone! = "")
Break;
Else
{
Console. writeline ("Enter it! ");
}
}
Console. writeline ("Enter your address :");
While (true)
{
Console. writeline ("Enter your city :");
M_1.ad.city = console. Readline ();
// M_1.address.city = console. Readline (); // you cannot directly access the members of the struct address in this way.
If (m_1.ad.city! = "")
Break;
Else
{
Console. writeline ("Enter it! ");
}
}
While (true)
{
Console. writeline ("Enter your street :");
M_1.ad.street = console. Readline ();
If (m_1.ad.street! = "")
Break;
Else
{
Console. WriteLine ("Enter it! ");
}
}
While (true)
{
Try
{
Console. WriteLine ("enter your house number :");
M_1.ad.no = Convert. ToUInt32 (Console. ReadLine ());
Break;
}
Catch (FormatException e)
{
Console. WriteLine ("enter a number of-9! ");
}
}
Console. WriteLine ("OK now ...... "); Console. WriteLine ("---------------------------------------------------------------------------");
Console. WriteLine ("your information is as follows: \ n ");
Console. WriteLine ("Name: {0}", M_1.name );
Console. WriteLine ("Age: {0}", M_1.age );
Console. WriteLine ("Tel: {0}", M_1.phone );
Console. WriteLine ("Address: No. {2}, {1} Street, {0} city", M_1.ad.city, M_1.ad.street, M_1.ad.no );
}
Catch (FormatException e/*, IOException I */)
{
String msg = "\ n --------------------------------------------------------------------------- \ n error: \ n Source:" + e. source + "\ n message:" + e. message + "\ n trace: \ n" + e. stackTrace;
Console. WriteLine (msg );
}
}
}
Version 3 code:
Using System;
Using System. IO;
Struct phoneBook
{
Public string name;
Public uint age;
Public string phone;
Public address ad;
Public struct address
{
Public string city;
Public String Street;
Public uint no;
}
}
Class welcome
{
Static phonebook Pb;
Static string info = "it means you are stupid to see this message ";
Static string write_info = "cug laike ";
Static string filename = "information record .txt ";
Static void outmsg (INT flag) // output information to the console
{
Console. foregroundcolor = consolecolor. Green;
Switch (FLAG)
{
Case 0:
{
Console. foregroundcolor = consolecolor. Red;
// Console. backgroundcolor = consolecolor. White;
Console. writeline ("\ n Address Book of the 2008 Century ");
Console. WriteLine ("______________________________________________________________________ \ n ");
Console. WriteLine ("input information (I ):");
Console. WriteLine ("view record (V ):");
Console. WriteLine ("Exit System (E ):");
Break;
}
Case 1:
{
Console. WriteLine ("Enter it! ");
Break;
}
Case 2:
{
Console. WriteLine ("enter a number of-9! ");
Break;
}
Case 3:
{
Console. ForegroundColor = ConsoleColor. Magenta;
Console. WriteLine ("---------------------------------------------------");
Console. WriteLine (info );
Break;
}
Case 4:
{
Console. WriteLine ("An error occurred while reading the file! ");
Break;
}
Case 5:
{
Console. WriteLine ("file writing error! ");
Break;
}
Case 6:
{
Console. WriteLine ("invalid character! ");
Break;
}
Case 7:
{
Console. WriteLine ("operation successful! ");
Break;
}
Default:
Break;
}
Console. BackgroundColor = ConsoleColor. Black;
Console. ForegroundColor = ConsoleColor. White;
}
Static void writeFile (phoneBook pb) // output the information to the file for saving
{
Try
{
StreamWriter w = File. AppendText (fileName );
/* FileStream f_stream = new FileStream (filename, FileMode. OpenOrCreate );
StreamWriter swd = new StreamWriter (f_stream );
Swd. WriteLine (info );*/
/* BinaryWriter bwd = new BinaryWriter (f_stream );
Bwd. WriteString (info );*/
W. writeLine (Environment. newLine); w. writeLine ("///////////////////////////////////// //////////////////////////////");
W. Write (write_info );
W. WriteLine (Environment. NewLine );
W. Close ();
OutMsg (7 );
}
Catch (IOException e)
{
Outmsg (4 );
String MSG = "\ n -------------------------------------------------------------------------
\ N error: \ n Source: "+ E. Source +" \ n message: "+ E. Message +" \ n trace: \ n "+ E. stacktrace;
Console. writeline (MSG );
}
}
Static void input (ref phonebook Pb) // enter information from the console
{
Try
{
While (true)
{
Console. writeline ("enter your name :");
PB. Name = console. Readline ();
If (PB. Name! = "")
Break;
Else
{
Outmsg (1 );
}
}
While (true)
{
Try
{
Console. writeline ("Enter your age :");
PB. Age = convert. touint32 (console. Readline ());
Break;
}
Catch
{
Outmsg (2 );
}
}
While (true)
{
Console. writeline ("Enter your phone number :");
PB. Phone = console. Readline ();
If (PB. Phone! = "")
Break;
Else
{
Outmsg (1 );
}
}
Console. writeline ("Enter your address :");
While (true)
{
Console. writeline ("Enter your city :");
PB. Ad. City = console. Readline ();
If (PB. Ad. City! = "")
Break;
Else
{
Outmsg (1 );
}
}
While (true)
{
Console. writeline ("Enter your street :");
PB. Ad. Street = console. Readline ();
If (PB. Ad. Street! = "")
Break;
Else
{
Outmsg (1 );
}
}
While (true)
{
Try
{
Console. writeline ("enter your house number :");
PB. Ad. No = convert. touint32 (console. Readline ());
Break;
}
Catch
{
OutMsg (2 );
}
}
Info = "your information is as follows: \ r \ n" + "name:" + pb. name + "\ r \ n age:" + pb. age + "\ r \ n
Tel: "+ pb. phone +" \ r \ n address: "+ pb. ad. city +" city "+ pb. ad. street +
"Street" + pb. ad. no + ";
Write_info = "name:" + pb. name + "\ r \ n age:" + pb. age + "\ r \ n Tel:" + pb. phone +
"\ R \ n address:" + pb. ad. city + "city" + pb. ad. street + "street" + pb. ad. no + ";
}
Catch (FormatException e/*, IOException I */)
{
String msg = "\ n --------------------------------------------------------------------------- \ n
Error: \ n Source: "+ e. Source +" \ n Message: "+ e. Message +" \ n trace: \ n "+ e. StackTrace;
Console. WriteLine (msg );
}
}
Static void readFile (phoneBook pb) // read information from the file
{
Try
{
StreamReader srd = File. OpenText (fileName );
String str;
Int count = 0; // number of records
While (srd. Peek ()> = 0)
{
Str = srd. ReadLine ();
If (str. Contains ("name") // process records with names as the marker
{
Count ++;
}
Console. WriteLine (str );
// Here, only the read string is output, and further processing can be performed to obtain a phoneBook
// Structure is used for other purposes, which is also the initial purpose, but it is time-based.
}
Console. WriteLine (Environment. NewLine );
Console. WriteLine ("----------------------------------------------------------");
Console. WriteLine ("A total of {0} records", count );
Srd. Close ();
}
Catch
{
OutMsg (6 );
}
}
Static void Main ()
{
Do
{
OutMsg (0 );
Char choice = Console. ReadKey (). KeyChar;
Console. WriteLine ("\ r \ n ");
Switch (choice)
{
Case 'I ':
Case 'I': // input information
{
InPut (ref PB );
OutMsg (3 );
WriteFile (PB );
Break;
}
Case 'V ':
Case 'V': // view records
{
// Console. WriteLine ("view records ");
Readfile (PB );
Break;
}
Case 'E ':
Case 'E': // exit the system
{
Console. writeline ("machine name: {0} \ r \ Nos version: {1} \ r \ n run time: {2} millisecond ",
Environment. machinename, environment. osversion, environment. tickcount );
Console. writeline ("exit system ");
Environment. Exit (0 );
// Application. Exit ();
Break;
}
Default:
{
Outmsg (6 );
Break;
}
}
} While (true );
}
}