/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // tm2.cs - demonstrates a custom constructor function for a structure // Compile this program using the following command line: // D:>csc tm2.cs // namespace nsStructure { using System; using System.Globalization; struct tm { public tm (DateTime tmVal) { tm_sec = tmVal.Second; tm_min = tmVal.Minute; tm_hour = tmVal.Hour; tm_mday = tmVal.Day; tm_mon = tmVal.Month - 1; tm_year = tmVal.Year - 1900; tm_wday = (int) tmVal.DayOfWeek; tm_yday = tmVal.DayOfYear; TimeZone tz = TimeZone.CurrentTimeZone; tm_isdst = tz.IsDaylightSavingTime (tmVal) == true ? 1 : 0; } public int tm_sec; // Seconds after the minute public int tm_min; // Minutes after the hour public int tm_hour; // Hours since midnight public int tm_mday; // The day of the month public int tm_mon; // The month (January = 0) public int tm_year; // The year (00 = 1900) public int tm_wday; // The day of the week (Sunday = 0) public int tm_yday; // The day of the year (Jan. 1 = 1) public int tm_isdst; // Flag to indicate if DST is in effect public override string ToString() { const string wDays = "SunMonTueWedThuFriSat"; const string months = "JanFebMarAprMayJunJulAugSepOctNovDec"; return (String.Format ("{0} {1} {2,2:00} " + "{3,2:00}:{4,2:00}:{5,2:00} {6} ", wDays.Substring (3 * tm_wday, 3), months.Substring (3 * tm_mon, 3), tm_mday, tm_hour, tm_min, tm_sec, tm_year + 1900)); } } public class tm2Demo { static public void Main() { DateTime timeVal = DateTime.Now; tm tmNow = new tm (timeVal); Console.WriteLine (tmNow); } } }
Class Interface
Structures are good when grouping data
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Structures are good when grouping data. using System; // Define a packet structure. struct PacketHeader { public uint packNum; // packet number public ushort packLen; // length of packet } // Use PacketHeader to create an e-commerce transaction record. class Transaction { static uint transacNum = 0; PacketHeader ph; // incorporate PacketHeader into Transaction string accountNum; double amount; public Transaction(string acc, double val) { // create packet header ph.packNum = transacNum++; ph.packLen = 512; // arbitrary length accountNum = acc; amount = val; } // Simulate a transaction. public void sendTransaction() { Console.WriteLine("Packet #: " + ph.packNum + ", Length: " + ph.packLen + ", Account #: " + accountNum + ", Amount: {0:C} ", amount); } } // Demonstrate Packet public class PacketDemo { public static void Main() { Transaction t = new Transaction("31243", -100.12); Transaction t2 = new Transaction("AB4655", 345.25); Transaction t3 = new Transaction("8475-09", 9800.00); t.sendTransaction(); t2.sendTransaction(); t3.sendTransaction(); } }
Copy a struct
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Copy a struct. using System; // Define a structure. struct MyStruct { public int x; } // Demonstrate structure assignment. public class StructAssignment { public static void Main() { MyStruct a; MyStruct b; a.x = 10; b.x = 20; Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x); a = b; b.x = 30; Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x); } }
Demonstrate a structure
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstrate a structure. using System; // Define a structure. struct Book { public string author; public string title; public int copyright; public Book(string a, string t, int c) { author = a; title = t; copyright = c; } } // Demonstrate Book structure. public class StructDemo1 { public static void Main() { Book book1 = new Book("Herb Schildt", "C# A Beginner's Guide", 2001); // explicit constructor Book book2 = new Book(); // default constructor Book book3; // no constructor Console.WriteLine(book1.title + " by " + book1.author + ", (c) " + book1.copyright); Console.WriteLine(); if(book2.title == null) Console.WriteLine("book2.title is null."); // now, give book2 some info book2.title = "Brave New World"; book2.author = "Aldous Huxley"; book2.copyright = 1932; Console.Write("book2 now contains: "); Console.WriteLine(book2.title + " by " + book2.author + ", (c) " + book2.copyright); Console.WriteLine(); // Console.WriteLine(book3.title); // error, must initialize first book3.title = "Red Storm Rising"; Console.WriteLine(book3.title); // now OK } }
Define struct and use it
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: */ using System; namespace Client.Chapter_3___Structs__Enums__Arrays_and_Classes { public struct MyStruct { public int MyInt; public long MyLong; public string MyString; } public class StructsChapter_3___Structs__Enums__Arrays_and_Classes { static void Main(string[] args) { MyStruct TheStruct; TheStruct.MyInt = 0; TheStruct.MyLong = 0; TheStruct.MyString = "Hello World"; } } }
Structs And Enums
using System; using System.Collections.Generic; using System.Text; struct Date { public Date(int ccyy, Month mm, int dd) { this.year = ccyy - 1900; this.month = mm; this.day = dd - 1; } public override string ToString() { return this.month + " " + (this.day + 1) + " " + (this.year + 1900); } private int year; private Month month; private int day; } enum Month { January, February, March, April, May, June, July, August, September, October, November, December } class Program { static void Entrance() { Month first = Month.December; Console.WriteLine(first); first++; Console.WriteLine(first); Date defaultDate = new Date(); Console.WriteLine(defaultDate); Date halloween = new Date(2008, Month.October, 31); Console.WriteLine(halloween); } static void Main() { try { Entrance(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
Use static method to initialize field
using System; internal class MyClass { public int iField1 = FuncA(); public int iField2 = FuncC(); public int iField3 = FuncB(); public static int FuncA() { Console.WriteLine("MyClass.FuncA"); return 0; } public static int FuncB() { Console.WriteLine("MyClass.FuncB"); return 1; } public static int FuncC() { Console.WriteLine("MyClass.FuncC"); return 2; } } public class Starter { public static void Main() { MyClass obj = new MyClass(); } }