

InterviewSolution
Saved Bookmarks
1. |
Solve : help me..why my program get"expected class delegate enum interface or struct"??? |
Answer» USING System; using System.Collections.Generic; using System.Runtime.Serialization.Formatters .Binary; //namespace to use Serialization class Record { private string name; public string Name { get { return name; } SET { name = value; } } private double homeNumber; public double HomeNumber { get { return homeNumber; } set { homeNumber = value; } } private double mobNumber; public double MobNumber { get { return mobNumber; } set { mobNumber = value; } } private string address; public string Address { get { return address; } set { address = value; } } // Default Constructor public Record() { this.name = string.Empty; this.homeNumber = 0000; this.MobNumber = 0000; this.address = "Fake"; } // Parameterized Constructor public Record(string n, double hNum, double mobNum, string add) { this.name = n; this.homeNumber = hNum; this.mobNumber = mobNum; this.address = add; } public void print() { System.Console.WriteLine("The Name Is : " + this.name); System.Console.WriteLine(" Home Phone No. Is : " + this.homeNumber); System.Console.WriteLine(" Mobile Phone No. Is : " + this.mobNumber); System.Console.WriteLine(" Address Is : " + this.address); } } // Add a new Record first to ArrayList(recordList) and then in file public void addNewRecord() { recordList = new ArrayList(); records = new Record(); // Here Adding Record to ArrayList recordList.Add(new Record("osama", 36887676, 3333003212,"Lahore")); recordList.Add(new Record("saad", 4234234, 33330065656, "Ali Park")); recordList.Add(new Record("bilal", 2123456, 0456123232, "Multan")); recordList.Add(new Record("maryam", 1312312, 33330032121,"Karachi")); FILESTREAM fs = new FileStream("data.txt", FileMode.Create, FileAccess.Write); BinaryFormatter bf = new BinaryFormatter(); } // Loads the all reocrds from file and add to ArrayList(recordList) public void loadRecord() { recordList = new ArrayList(); FileStream fs = new FileStream("data.txt", FileMode.Open, FileAccess.Read); BinaryFormatter bf = new BinaryFormatter(); try { while (fs.CanRead)//Reading the FileStream { Record R = (Record)bf.Deserialize(fs);//De-Serialize the each object recordList.Add(r); //And Add to ArrayList r.print(); //Printing the each record from the file to console } } catch (Exception e) {} fs.Close(); } // Saving the record to file. public void saveRecord() { FileStream fs = new FileStream("data.txt", FileMode.Create, FileAccess.Write); BinaryFormatter bf = new BinaryFormatter(); for (int i = 0; i < recordList.Count; i++) { record = (Record)recordList; bf.Serialize(fs, record); fs.Flush(); } fs.Close(); } public void enterInPhoneDirectory() { record = new Record(); loadRecord(); Console.WriteLine("Enter Your Home Number/ Mobile Number : "); double number = double.Parse(Console.ReadLine()); // User to enter his/her Home/mobile no. for (int j = 0; j < recordList.Count; j++) { record = (Record)recordList[j]; if (record.HomeNumber == number || record.MobNumber == number) { Console.WriteLine("Welcome , " + record.Name.ToUpper()); record.print(); Console.WriteLine("Press 1 to Change Home Number"); Console.WriteLine("Press 2 to Change Mobile Number"); Console.WriteLine("Press 3 to Change Address"); Console.WriteLine("Press 4 to exit this menu"); int option = int.Parse(Console.ReadLine()); switch (option) { case 1: Console.WriteLine("Enter Your New Home Number"); double hNum = double.Parse(Console.ReadLine()); record.HomeNumber = hNum; break; case 2: Console.WriteLine("Enter Your New Mobile Number"); double mNum = double.Parse(Console.ReadLine()); record.MobNumber = mNum; break; case 3: Console.WriteLine("Enter Your New Address"); string add = Console.ReadLine(); record.Address = add; break; case 4: Console.Clear(); Main(new string[]{"s"}); break; } saveRecord(); } } } public static void Main(string[] args) { // Front end menu Console.WriteLine("----------Menu----------"); Console.WriteLine("Press 1 to enter number (home / mobile)"); Console.WriteLine("Press 2 to exit"); int input = int.Parse(Console.ReadLine()); switch (input) { case 1: enterInPhoneDirectory(); break; case 2: Environment.Exit(0); break; } } Move of the methods are instance methods that aren't declared inside a class definition (and are thus invalid). Several variables aren't declared anywhere, and the aforementioned methods are instance methods rather than static methods. Fixed VERSION: Code: [Select]using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace testapp { using System.Runtime.Serialization.Formatters.Binary; //namespace to use Serialization public static class Program { static ArrayList recordList; static Record records; class Record { private string name; public string Name { get { return name; } set { name = value; } } private double homeNumber; public double HomeNumber { get { return homeNumber; } set { homeNumber = value; } } private double mobNumber; public double MobNumber { get { return mobNumber; } set { mobNumber = value; } } private string address; public string Address { get { return address; } set { address = value; } } // Default Constructor public Record() { this.name = string.Empty; this.homeNumber = 0000; this.MobNumber = 0000; this.address = "Fake"; } // Parameterized Constructor public Record(string n, double hNum, double mobNum, string add) { this.name = n; this.homeNumber = hNum; this.mobNumber = mobNum; this.address = add; } public void print() { System.Console.WriteLine("The Name Is : " + this.name); System.Console.WriteLine(" Home Phone No. Is : " + this.homeNumber); System.Console.WriteLine(" Mobile Phone No. Is : " + this.mobNumber); System.Console.WriteLine(" Address Is : " + this.address); } } // Add a new Record first to ArrayList(recordList) and then in file public static void addNewRecord() { recordList = new ArrayList(); records = new Record(); // Here Adding Record to ArrayList recordList.Add(new Record("osama", 36887676, 3333003212, "Lahore")); recordList.Add(new Record("saad", 4234234, 33330065656, "Ali Park")); recordList.Add(new Record("bilal", 2123456, 0456123232, "Multan")); recordList.Add(new Record("maryam", 1312312, 33330032121, "Karachi")); FileStream fs = new FileStream("data.txt", FileMode.Create, FileAccess.Write); BinaryFormatter bf = new BinaryFormatter(); } // Loads the all reocrds from file and add to ArrayList(recordList) public static void loadRecord() { recordList = new ArrayList(); FileStream fs = new FileStream("data.txt", FileMode.Open, FileAccess.Read); BinaryFormatter bf = new BinaryFormatter(); try { while (fs.CanRead)//Reading the FileStream { Record r = (Record)bf.Deserialize(fs);//De-Serialize the each object recordList.Add(r); //And Add to ArrayList r.print(); //Printing the each record from the file to console } } catch (Exception e) { } fs.Close(); } // Saving the record to file. public static void saveRecord() { FileStream fs = new FileStream("data.txt", FileMode.Create, FileAccess.Write); BinaryFormatter bf = new BinaryFormatter(); for (int i = 0; i < recordList.Count; i++) { var record = (Record)recordList[i]; bf.Serialize(fs, record); fs.Flush(); } fs.Close(); } public static void enterInPhoneDirectory() { var record = new Record(); loadRecord(); Console.WriteLine("Enter Your Home Number/ Mobile Number : "); double number = double.Parse(Console.ReadLine()); // User to enter his/her Home/mobile no. for (int j = 0; j < recordList.Count; j++) { record = (Record)recordList[j]; if (record.HomeNumber == number || record.MobNumber == number) { Console.WriteLine("Welcome , " + record.Name.ToUpper()); record.print(); Console.WriteLine("Press 1 to Change Home Number"); Console.WriteLine("Press 2 to Change Mobile Number"); Console.WriteLine("Press 3 to Change Address"); Console.WriteLine("Press 4 to exit this menu"); int option = int.Parse(Console.ReadLine()); switch (option) { case 1: Console.WriteLine("Enter Your New Home Number"); double hNum = double.Parse(Console.ReadLine()); record.HomeNumber = hNum; break; case 2: Console.WriteLine("Enter Your New Mobile Number"); double mNum = double.Parse(Console.ReadLine()); record.MobNumber = mNum; break; case 3: Console.WriteLine("Enter Your New Address"); string add = Console.ReadLine(); record.Address = add; break; case 4: Console.Clear(); Main(new string[] { "s" }); break; } saveRecord(); } } } public static void Main(string[] args) { // Front end menu Console.WriteLine("----------Menu----------"); Console.WriteLine("Press 1 to enter number (home / mobile)"); Console.WriteLine("Press 2 to exit"); int input = int.Parse(Console.ReadLine()); switch (input) { case 1: enterInPhoneDirectory(); break; case 2: Environment.Exit(0); break; } } } } |
|