Advertisement
GlobalAccessSoftware

Getting around HybridDictionary Not Being Serializable.

Jun 5th, 2017
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.13 KB | None | 0 0
  1.  
  2. #region AppCode5 Part-3 v4.7.5.10 Jun 05 17
  3.  
  4.     /* This was way easier than I thought it would be,
  5.      * (Except finally learning that a HybridDictionary
  6.      * is not Serializable to XML and storing it as a
  7.      * String Collection, flipping between Key & Value on the fly.)
  8.      * Sure shows what you can do with situational awareness
  9.      * of and reuse of existing code and so forth.
  10.      * v4.7.4.07 Jun 04 2017
  11.      * (c) 2011 - 2031 by John P. Edwards
  12.      * Final Debug and Test v4.7.5.10 Jun 05 2017
  13.      * ******************************************
  14.      */
  15.  
  16.     //private static HybridDictionary Dic { get; set; }
  17.     //// This ^ Property is in the Header as Class Global.
  18.  
  19.     /// <summary> Load HybridDictionary string, int
  20.     /// from StringCollection Setting.
  21.     /// v4.7.5.10 Jun 05 2017
  22.     /// </summary>
  23.     private static void LoadDic()
  24.     {
  25.       var flip = false;
  26.       Object key  = "";
  27.       var raw  = OPS.Default.RecNumDic;
  28.       // Actually ^ a StringCollection.
  29.       if (raw == null || raw.Count < 1) return;
  30.  
  31.       try
  32.       {
  33.         Dic = new HybridDictionary();
  34.         foreach (Object str in raw)
  35.         {
  36.           flip = !flip; // Alternate between Key & Value.
  37.           if (flip)
  38.           { // key is already a string doesn't
  39.             // need to be "unBoxed" in this example.
  40.             key = str;
  41.             continue;
  42.           }
  43.           int value;
  44.           try
  45.           { // The int32 Value needs to be
  46.             // "unBoxed" thusly from a string.
  47.             // But it's the same principle as with Objects,
  48.             // & Boxing & Unboxing thereof.
  49.             value = Convert.ToInt32(str);
  50.           }
  51.           catch (FormatException)
  52.           {
  53.             const string
  54.               except = "FormatException",
  55.               sender = "Inner Try Block";
  56.               TestMessager(sender, except);
  57.             value = -1;
  58.           }
  59.           if ( Dic.Contains(key)) Dic[key] = value;
  60.           else Dic.Add(key, value);
  61.         }
  62.       }
  63.       catch (ArgumentException)
  64.       {
  65.         const string
  66.           except = "System.ArgumentException",
  67.           sender = "Outter Try Block";
  68.         TestMessager(sender, except);
  69.       }
  70.     }
  71.  
  72.     private static void TestMessager(string sender, string e)
  73.     {
  74.       if (Testing) MessageBox.Show(string.Format(
  75.         "{0}\nException in LoadDic:\n\n{1}\n\n", sender, e));
  76.     }
  77.  
  78.     /// <summary> Save the HybridDictionary
  79.     /// as StringCollection.
  80.     /// Called only from FormClosing.
  81.     /// v4.7.5.10 Jun 05 2017
  82.     /// </summary>
  83.     private static void SaveDic()
  84.     {
  85.       SaveRecNum(true); // It should never happen after <-= but...
  86.       if (Dic == null || Dic.Count < 1) return;
  87.  
  88.       var i = Dic.GetEnumerator();
  89.       OPS.Default.RecNumDic = new StringCollection();
  90.       while (i.MoveNext())
  91.       {
  92.         OPS.Default.RecNumDic.Add(i.Key.ToString());
  93.         OPS.Default.RecNumDic.Add(i.Value.ToString());
  94.         // Adding them two at a time because it's a KeyValue pair.
  95.         // NOTE: The StringCollection will always be twice the size
  96.         // of the HybridDictionary because of this. (Alternate Pairs)
  97.       }
  98.       OPS.Default.Save();
  99.     }
  100.  
  101.     // Called only from TestButtonClick.
  102.     private static void ViewDictionary()
  103.     {
  104.       if (Dic == null) return;
  105.       var e = Dic.GetEnumerator();
  106.       var n = 1;
  107.       const string
  108.         t = "Contents of Dictionary:";
  109.       var x = "";
  110.       while (e.MoveNext())
  111.       {
  112.         x += string.Format(
  113.           "Entry #{0})\nFile: {1}\n\n" +
  114.           "Which has a corresponding" +
  115.           " RecNum of {2}\n\n",
  116.             n++, e.Key, e.Value);
  117.       }
  118.       using (var j = new ShowMeForm(x,t))
  119.       {
  120.         j.Size     = AUS.Default.ViewDic_Size;
  121.         j.Location = AUS.Default.ViewDic_Loca;
  122.         j.ShowDialog();
  123.         AUS.Default.ViewDic_Size = j.Size;
  124.         AUS.Default.ViewDic_Loca = j.Location;
  125.       }
  126.       AUS.Default.Save();
  127.     }
  128.  
  129.     /// <summary> Gets the Previous Record Number
  130.     /// for the current data file. Defaults to first
  131.     /// Record when none found. (Gets added in SaveRecNum)
  132.     /// Called from AppCode1.cs from AppStart() and in the
  133.     /// OptionsBackend.cs from LoadNewPreferences();
  134.     /// </summary>
  135.     private void GetRecNum()
  136.     {
  137.       // Never gets here unless UsePreferences is Checked.
  138.       if (Dic == null || Dic.Count < 1) LoadDic();
  139.       // if still null then initialize the HybridDictionary.
  140.       if (Dic == null || Dic.Count < 1)
  141.         Dic = new HybridDictionary(false);      
  142.       #region Lesson Learned Here:
  143.       // why did it reset each run as HybridDictionary?
  144.       // Because Hybrid Dictionary is not Serializable to XML
  145.       // so I just had to convert it to a String Collection and
  146.       // flip them 2 at a time into the HybridDictionary on FormLoad
  147.       // and Then convert it back to the String Collection on FormClose
  148.       // with LoadDic() and SaveDic();
  149.       #endregion
  150.       if (Dic == null || Dic.Count < 1) return;
  151.       var f = AUS.Default.RootDir +
  152.         "\\" + AUS.Default.FileName;
  153.       _wasAtNum =
  154.         AUS.Default.RecordNum =
  155.           Dic.Contains(f) ? (int)Dic[f] : -1; // v4.7.5.10
  156.       CheckDic();
  157.       AUS.Default.Save();
  158.     }
  159.  
  160.     private static bool UsePreferences(bool which)
  161.     {
  162.       var title = ARS.IF_UsePrefTitle1;
  163.         //"Turn on Preferences?";
  164.       var text  = ARS.IF_UsePrefText1;
  165.         //"This feature is only available when\n" +
  166.         //"the Folder Preference Setting is ON.\n\n" +
  167.         //"Would you like to turn it on Now?";
  168.       if (!which)
  169.       {
  170.         title = ARS.IF_UsePrefTitle2;
  171.         text  = ARS.IF_UsePrefText2;
  172.         //"Turning on the Alternate Folder\n" +
  173.         //"Finder in Options is a Key feature too.\n\n" + todo remnants.
  174.         //"Would you like to turn it on now as well?\n" +
  175.         //"(Recomended)\n";
  176.       }
  177.       return
  178.         AppStatic.YesNoDialog(text, title, 1);
  179.     }
  180.  
  181.     /// <summary> Either add or change existing value.
  182.     /// Checks for Object in HybridDictionary and changes
  183.     /// the value or adds the key and value to the Collection.
  184.     /// Called from SaveDic(); here (Which is called from FormClosing();)
  185.     /// and from OptionsBackend.cs from LoadNewPreferences();
  186.     /// </summary>
  187.     private static void SaveRecNum(bool auto)
  188.     {
  189.       if (!OPS.Default.UsePreferences)
  190.       {
  191.         if (auto) return;
  192.         if (!UsePreferences(true)) return;
  193.         OPS.Default.UsePreferences = true;
  194.         if (!OPS.Default.AlternateFolderFinder
  195.           && UsePreferences(false))
  196.             OPS.Default.AlternateFolderFinder = true;
  197.         OPS.Default.Save();
  198.       }
  199.       if (Dic == null)
  200.       {
  201.         Dic = new HybridDictionary(false);
  202.         if (Dic == null)
  203.         {
  204.           if (Testing && !auto)
  205.             MessageBox.Show(ORS.IF_SaveRecNum_NULL);
  206.           return;
  207.         }
  208.       }
  209.       var key   = AUS.Default.RootDir +
  210.         "\\"  +   AUS.Default.FileName;
  211.       var index = AUS.Default.RecordNum =
  212.         CurrentRecord - 1;
  213.       if (Dic.Contains(key)) Dic[key] = index;
  214.       else Dic.Add(key, index);
  215.       AUS.Default.Save();
  216.       CheckDic();
  217.     }
  218.  
  219.     /// <summary> Check Dictionary and Verify Data there.
  220.     /// </summary>
  221.     private static void CheckDic()
  222.     {
  223.       foreach (string ffn in Dic.Keys)
  224.       {
  225.         var dir  = Path.GetDirectoryName(ffn);
  226.         if (dir == null) return;
  227.         if (Directory.Exists(dir)
  228.           && HasDataFiles(dir)
  229.           && File.Exists(ffn))
  230.         {
  231.           if (!Dic.Contains(ffn))   Dic.Add(ffn, 0);
  232.         }
  233.         else if (Dic.Contains(ffn)) Dic.Remove(ffn); // OUCH!
  234.       }
  235.     }
  236.  
  237.     /// <summary> Local Call that Calls OptionsForm Method.
  238.     /// Mostly created so this shows up in this Class.
  239.     /// </summary>
  240.     /// <param name="dir"></param>
  241.     /// <returns></returns>
  242.     private static bool HasDataFiles(string dir)
  243.     {
  244.       return OptionsForm.HasDataFiles(dir);
  245.     }
  246. #endregion AppCode5 Part-3 v4.7.5.10 Jun 05 17
  247.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement