public static void Main(string[] args) { // real-world variables, keep them typed as base Message // to be able to silently replace them with different objects Message original1; Message original2; // let's construct some one-time readable messages { var tmp1 = new OneTimeMessage(); tmp1.data["mom"] = "dad"; tmp1.data["cat"] = "dog"; original1 = tmp1; var tmp2 = new OneTimeMessage(); tmp2.data["mom"] = "dad"; tmp2.data["cat"] = "dog"; original2 = tmp2; } // test1 - can't read twice Console.WriteLine("test0A:" + original1.GetData("mom")); //Console.WriteLine("test0B:" + original1.GetData("mom")); // fail // test2 - can read twice with Reader's help var backup1 = original2; using (var rd1 = new Reader(ref original2)) { Console.WriteLine("test1A:" + rd1.ReadSomething("mom")); } var backup2 = original2; using (var rd2 = new Reader(ref original2)) { Console.WriteLine("test1A:" + rd2.ReadSomething("mom")); //^ ok - becase Reader replaced 'original2' with SafeMessage } // test3: Reader's ctor is intelligent // so no more SafeMessages created during future usage var backup3 = original2; using (var rd3 = new Reader(ref original2)) { } var backup4 = original2; using (var rd4 = new Reader(ref original2)) { } Console.WriteLine("checking for copies:" + (original2 == backup1)); Console.WriteLine("checking for copies:" + (original2 == backup2)); Console.WriteLine("checking for copies:" + (original2 == backup3)); Console.WriteLine("checking for copies:" + (original2 == backup4)); }
private void SubmitBtn_Click(object sender, EventArgs e) { try { CnC_DatabaseDataContext DataBase = new CnC_DatabaseDataContext(); var Count = (from rows in DataBase.OneTimeMessages select rows).Count(); OneTimeMessage OneMessages = new OneTimeMessage(); OneMessages.ID = Count + 1; OneMessages.Message = Messages.Text; OneMessages.UserID = UserID.Text; DataBase.OneTimeMessages.InsertOnSubmit(OneMessages); DataBase.SubmitChanges(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }