// ---------------------------------------------------------------------------------------- /// <!-- IntValue --> /// <summary> /// returns an integer or the default if it can not parse one /// </summary> /// <param name="xPath"></param> /// <param name="defaultValue"></param> /// <returns></returns> public int IntValue(string xPath, int defaultValue) { string str = StrValue(xPath, ""); int num = TreatAs.IntValue(str, defaultValue); return(num); }
// ---------------------------------------------------------------------------------------- /// <!-- Value --> /// <summary> /// A more powerful version like SelectedValue.ToString() /// </summary> /// <param name="form">allows no-op if the control is not on the form</param> /// <param name="control"></param> /// <param name="textOK">is it OK to use thetext value of the Dropdown if the SelectedValue isn't found?</param> /// <param name="defaultValue"></param> /// <returns></returns> public static string Value(Form form, Control control, bool textOK, string defaultValue) { string str = defaultValue; if (Active(form, control)) { str = "XXYYZZ"; Type type = control.GetType(); switch (type.Name.ToString()) { case "ComboBox": ComboBox drop = (ComboBox)control; str = TreatAs.StrValue(drop.SelectedValue, "XXYYZZ"); if (textOK) // is it OK to use thetext value of the Dropdown if the SelectedValue isn't found? { if (str == "XXYYZZ") { str = WithoutSqlInjection(drop.SelectedText); } if (str == "") { str = WithoutSqlInjection(drop.Text); } } if (str == "XXYYZZ") { str = defaultValue; } break; default: str = WithoutSqlInjection(control.Text); break; } } return(str); }
//public string _RootTag { get { return Regex.Replace(_doc.InnerXml, "^<([^< >]+) .+$", "$1"); } } // ---------------------------------------------------------------------------------------- /// <!-- NullableDateTimeValue --> /// <summary> /// /// </summary> /// <param name="xPath"></param> /// <param name="defaultValue"></param> /// <returns></returns> public DateTime?NullableDateTimeValue(string xPath, DateTime?defaultValue) { string str = StrValue(xPath, ""); DateTime?time = TreatAs.DateValue(str, defaultValue); return(time); }
public override int GetHashCode() { return(Clsid.GetHashCode() ^ Name.GetSafeHashCode() ^ TreatAs.GetHashCode() ^ AppID.GetHashCode() ^ TypeLib.GetHashCode() ^ Servers.Values.GetEnumHashCode() ^ Elevation.GetSafeHashCode() ^ ActivatableFromApp.GetHashCode() ^ TrustedMarshaller.GetHashCode() ^ Source.GetHashCode() ^ PackageId.GetSafeHashCode()); }
// ---------------------------------------------------------------------------------------- // Overrides // ---------------------------------------------------------------------------------------- public override string ToString() { string canOrCant = ""; if (IsTrue == null) { canOrCant = ""; } else if (IsTrue.Value) { canOrCant = "Has "; } else if (!IsTrue.Value) { canOrCant = "No "; } string action = TreatAs.StrValue(Operation, ""); string determiner = ""; if (string.IsNullOrWhiteSpace(TargetLabel) && (TargetId == null || TargetId < 0)) { determiner = " All"; } else { determiner = ""; } string typeOf = ""; if (string.IsNullOrWhiteSpace(TargetLabel) && (TargetId == null || TargetId < 0)) { typeOf = " " + TreatAs.StrValue(TargetType); } else { typeOf = " " + TreatAs.StrValue(TargetType) + " " + "'" + TreatAs.StrValue(TargetLabel, TreatAs.StrValue(TargetId, "NULL")) + "'"; } string noun = ""; if (string.IsNullOrWhiteSpace(TargetLabel) && (TargetId == null || TargetId < 0)) { noun = " " + __.Pluralize(TreatAs.StrValue(Perview, "?")); } else { noun = " " + TreatAs.StrValue(Perview, "?"); } return(canOrCant + action + determiner + typeOf + noun); }
// ---------------------------------------------------------------------------------------- /// <!-- FindDsmCodesLike --> /// <summary> /// /// </summary> /// <param name="keyword"></param> /// <param name="all"></param> /// <returns></returns> private static RichDataTable FindDsmCodesLike(string keyword, RichDataTable all) { int id = TreatAs.IntValue(keyword, 0); string whereClause = " ConditionName LIKE '%" + keyword + "%'" + " OR ConditionCode4TR LIKE '" + keyword + "%'" + " OR ConditionCode5 LIKE '" + keyword + "%'" ; return(all._Select(whereClause)); }
// ---------------------------------------------------------------------------------------- // Short methods and properties /* ------------------------------------------------------------------------------------- */ /// <summary>Determines whether a particular file exists in the location identified by a folder path in a string member</summary> //public static bool FileExistsAt(object inObject, string folderMemberName, string fileName) { return FileAt(inObject, folderMemberName, fileName).Exists ; } //public static void DeleteFileAt(object inObject, string folderMemberName, string fileName) { FileAt(inObject, folderMemberName, fileName).Delete(); } // ---------------------------------------------------------------------------------------- /// <!-- BoolValueSet --> /// <summary> /// Changes a boolean member value /// </summary> /// <param name="inObject">the object containing the member</param> /// <param name="memberName">the name of the member</param> /// <param name="value"></param> /// <returns>the original boolean value or false if it was null or not boolean</returns> public static bool BoolValueSet(object inObject, string memberName, bool value) { FieldInfo member = inObject.GetType().GetField(memberName , BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); object before = member.GetValue(inObject); member.SetValue(inObject, value); return(TreatAs.BoolValue(before, false)); }
// ---------------------------------------------------------------------------------------- /// <!-- _StoreValue --> /// <summary> /// /// </summary> /// <param name="xpath"></param> /// <param name="namespacePrefix"></param> /// <param name="value"></param> /// <returns></returns> public XmlNode _StoreValue(PathSlicer xpath, string namespacePrefix, object value) { string strValue = TreatAs.StrValue(value, ""); XmlNode node = _GetNode(namespacePrefix, xpath.Path); if (node == null) { node = _CreateNodes(xpath, strValue); } else { node.InnerText = strValue; } return(node); }
// ---------------------------------------------------------------------------------------- /// <!-- StrValue --> /// <summary> /// Grabs the string value of a private member, regardless of its type /// </summary> /// <param name="inObject">the object containing the member</param> /// <param name="memberName">the name of the member</param> /// <returns></returns> public static string StrValue(object inObject, string memberName) { FieldInfo member = inObject.GetType().GetField(memberName , BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); if (member == null) { return(""); } else { return(TreatAs.StrValue(member.GetValue(inObject), "")); } }
// ---------------------------------------------------------------------------------------- /// <!-- Bool --> /// <summary> /// /// </summary> /// <param name="form">allows no-op if the control is not on the form</param> /// <param name="control"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static Boolean Bool(Form form, Control control, bool defaultValue) { Boolean value = defaultValue; if (Active(form, control)) { if (control.GetType() == typeof(RadioButton)) { value = ((RadioButton)control).Checked; } else { value = TreatAs.BoolValue(control.Text, defaultValue); } } return(value); }
// ---------------------------------------------------------------------------------------- /// <!-- Time --> /// <summary> /// /// </summary> /// <param name="form">allows no-op if the control is not on the form</param> /// <param name="control"></param> /// <param name="defaultTime"></param> /// <returns></returns> public static TimeSpan Time(Form form, Control control, TimeSpan defaultTime) { TimeSpan time = defaultTime; if (Active(form, control)) { if (control.GetType() == typeof(DateTimePicker)) { DateTimePicker picker = ((DateTimePicker)control); time = picker.Value.TimeOfDay; } else { time = TreatAs.Time(OnlyTime(control.Text), defaultTime); } } return(time); }
// ---------------------------------------------------------------------------------------- /// <!-- OldestFileDateByName --> /// <summary> /// Identifies the most recent date based on the names of the files in the list /// </summary> /// <param name="fileList">file names have YYYYMMDDHHMMDD in them somewhere</param> /// <returns></returns> public static DateTime OldestFileDateByName(FileInfo[] fileList) { DateTime oldest = DateTime.UtcNow; foreach (FileInfo fi in fileList) { string fileName = fi.Name; string strDate = Regex.Replace(fileName, "[^0-9]", ""); strDate = Regex.Replace(strDate, "^(....)(..)(..)(..)(..)(.*)$", "$1/$2/$3 $4:$5:$6"); DateTime?fileNameTime = TreatAs.DateValue(strDate, null); if (fileNameTime != null) { if (fileNameTime < oldest) { oldest = (DateTime)fileNameTime; } } } return(oldest); }
// ------------------------------------------------------------------------------ // Constructor // ------------------------------------------------------------------------------ // ---------------------------------------------------------------------------------------- /// <!-- NewestFileDateByName --> /// <summary> /// Identifies the most recent date based on the names of the files in the list /// </summary> /// <param name="fileList">file names have YYYYMMDDHHMMDD in them somewhere</param> /// <returns></returns> public static DateTime NewestFileDateByName(FileInfo[] fileList) { DateTime newest = TimeDate_old.MinSqlValue.CLRFormat; foreach (FileInfo fi in fileList) { string fileName = fi.Name; string strDate = Regex.Replace(fileName, "[^0-9]", ""); strDate = Regex.Replace(strDate, "^(....)(..)(..)(..)(..)(.*)$", "$1/$2/$3 $4:$5:$6"); DateTime?fileNameTime = TreatAs.DateValue(strDate, null); if (fileNameTime != null) { if (fileNameTime > newest) { newest = (DateTime)fileNameTime; } } } return(newest); }
private static void TreatAs_BoolValue_testcase(object obj, bool defaultValue, bool target) { bool value = TreatAs.BoolValue(obj, defaultValue); Assert.That(value, Is.equal_to, target); }
private static void TreatAs_IntValue_testcase(object obj, int defaultValue, int target) { int value = TreatAs.IntValue(obj, defaultValue); Assert.That(value, Is.equal_to, target); }
// ---------------------------------------------------------------------------------------- // Custom Methods // ---------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------- /// <!-- _Bool --> /// <summary> /// /// </summary> /// <param name="xdoc"></param> /// <param name="namespacePrefix"></param> /// <param name="defaultValue"></param> /// <returns></returns> public bool _Bool(PathSlicer xpath, string namespacePrefix, bool defaultValue) { return(TreatAs.BoolValue(_Str(xpath, namespacePrefix, ""), defaultValue)); }
// ---------------------------------------------------------------------------------------- /// <!-- Value --> /// <summary> /// Given a combo box and a value select the item in the drop list that matches if any /// </summary> /// <remarks> /// This gets complicated because SelectedValue can not be set or retrieved until after /// the ComboBox is displayed to the user /// </remarks> /// <param name="form">allows no-op if the control is not on the form</param> /// <param name="drop"></param> /// <param name="value"></param> public static void Value(Form form, ref ComboBox drop, object value) { if (Scrape.Active(form, drop)) { if (Is.Null(value)) { drop.Text = ""; } else { string str = TreatAs.StrValue(value, ""); int oldIndex = drop.SelectedIndex; drop.SelectedIndex = -1; string oldText = drop.Text; drop.Text = ""; // ---------------------------------------------------------------------90 // Try to select by value (this should work under optimal conditions) // ---------------------------------------------------------------------90 try { drop.SelectedValue = value; } catch { } if (drop.SelectedValue == null || drop.SelectedValue.ToString() == "") { try { drop.SelectedValue = (object)str; } catch { } } if (drop.SelectedValue == null || drop.SelectedValue.ToString() == "") { try { drop.SelectedValue = TreatAs.IntValue(value, -1); } catch { } } // ---------------------------------------------------------------------90 // Try to select item by value, by item and by a sequential search // ---------------------------------------------------------------------90 if (drop.SelectedIndex < 0) { SetByIndex(form, drop, drop.Items.IndexOf(value)); } if (drop.SelectedIndex < 0) { try { drop.SelectedItem = value; } catch { } } if (drop.SelectedIndex < 0) { try { drop.SelectedItem = (object)str; } catch { } } if (drop.SelectedIndex < 0) { SetByIndex(form, drop, Scrape.FindIndexOf(form, drop, value)); } // ---------------------------------------------------------------------90 // Give up and just set the text to the value // ---------------------------------------------------------------------90 if (drop.SelectedIndex < 0) { SetByIndex(form, drop, drop.FindStringExact(str)); } if (drop.SelectedIndex < 0 && str.Length > 6) { SetByIndex(form, drop, drop.FindString(str)); } if (drop.SelectedIndex < 0) { if (drop.SelectedText == null || drop.SelectedText == "") { try { drop.SelectedText = str; } catch { } } if (drop.Text == null || drop.Text == "") { drop.Text = str; } } } } }
// ---------------------------------------------------------------------------------------- /// <!-- XdY --> /// <summary> /// /// </summary> /// <param name="xdy"></param> /// <returns></returns> /// <remarks>production ready</remarks> public static int XdY(string xdy) { string[] hi = xdy.Split("d".ToCharArray()); return(XdY(TreatAs.IntValue(hi[0], 1), TreatAs.IntValue(hi[1], 1))); }
private static void TreatAs_LongValue_testcase(object obj, long defaultValue, long target) { Int64 value = TreatAs.LongValue(obj, defaultValue); Assert.That(value, Is.equal_to, target); }
public override int GetHashCode() { return(Clsid.GetHashCode() ^ Name.GetSafeHashCode() ^ TreatAs.GetHashCode() ^ AppID.GetHashCode() ^ TypeLib.GetHashCode() ^ Servers.Values.GetEnumHashCode() ^ Elevation.GetSafeHashCode()); }
// ---------------------------------------------------------------------------------------- /// <!-- AsciiArt --> /// <summary> /// /// </summary> /// <param name="cellHeight"></param> /// <param name="cellWidth"></param> /// <returns></returns> public string AsciiArt(int cellHeight, int cellWidth) { Array2dChar cha = new Array2dChar(Height * (cellHeight + 1) + 1, Width * (cellWidth + 1) + 1); // -------------------------------------------------------------------------- // Horizontal lines // -------------------------------------------------------------------------- for (int y = 0; y < cha.Height; ++y) { int mod = y % (cellHeight + 1); if (mod == 0) { for (int x = 0; x < cha.Width; ++x) { cha[y, x] = '-'; } } else { for (int x = 0; x < cha.Width; ++x) { cha[y, x] = '|'; } } } // -------------------------------------------------------------------------- // Vertical lines // -------------------------------------------------------------------------- for (int y = 0; y < cha.Height; ++y) { for (int x = 0; x < cha.Width; ++x) { int mod = y % (cellHeight + 1) + x % (cellWidth + 1); if (mod == 0) { cha[y, x] = '+'; } } } // -------------------------------------------------------------------------- // Endeme array letters // -------------------------------------------------------------------------- for (int y = 0; y < this.Height; ++y) { for (int x = 0; x < this.Width; ++x) { Endeme en = this[y, x]; int i = y * (cellHeight + 1) + 1; int j0 = x * (cellHeight + 1) + 1; int j = j0; cha[i, j] = TreatAs.CharValue(en[0], ' ', ' '); j++; cha[i, j] = TreatAs.CharValue(en[1], ' ', ' '); j = j0; i++; cha[i, j] = TreatAs.CharValue(en[2], ' ', ' '); j++; cha[i, j] = TreatAs.CharValue(en[3], ' ', ' '); } } string str = ""; string line = "+"; for (int j = 0; j < this.Width; ++j) { line += "--+"; } for (int i = 0; i < this.Height; ++i) { str += line; string delim = "\r\n|"; for (int j = 0; j < this.Width; ++j) { str += delim + "AB"; delim = "|"; } str += delim; line = "\r\n+"; for (int j = 0; j < this.Width; ++j) { line += "--+"; } } str += line; //return str; return(cha.AsciiArt("", ' ')); }
// ---------------------------------------------------------------------------------------- /// <!-- EndemeValue_test --> /// <summary> /// /// </summary> private void EndemeValueAddStrValue_test() { Assert.ThingsAbout("EndemeList", "Add"); EndemeReference enRef = new EndemeReference(); enRef.Add(new EndemeSet("Identity")); enRef["Identity"].Add('A', "Alias", ""); enRef["Identity"].Add('B', "Birthday", ""); enRef["Identity"].Add('C', "Card number", ""); enRef["Identity"].Add('D', "DL number", "driver's license"); enRef["Identity"].Add('E', "Email", ""); enRef["Identity"].Add('F', "First name", ""); enRef["Identity"].Add('G', "Gender", ""); enRef["Identity"].Add('H', "Honorific", "prefix (Mr, Dr...)"); enRef["Identity"].Add('I', "Id", ""); enRef["Identity"].Add('J', "Judicial number", ""); enRef["Identity"].Add('K', "King", "father's X"); enRef["Identity"].Add('L', "Last name", ""); enRef["Identity"].Add('M', "Middle name", ""); enRef["Identity"].Add('N', "Nick name", ""); enRef["Identity"].Add('O', "Oceanic number", ""); enRef["Identity"].Add('P', "Postfix", "suffix"); enRef["Identity"].Add('Q', "Queen", "mother's X"); enRef["Identity"].Add('R', "Race", ""); enRef["Identity"].Add('S', "Social number", ""); enRef["Identity"].Add('T', "Tag, userid", ""); enRef["Identity"].Add('U', "Unmarried name", "maiden name"); enRef["Identity"].Add('V', "Visa number", "passport number"); enRef.Add(new EndemeSet("Demographic")); enRef["Demographic"].Add('A', "Age", "Stage of life"); enRef["Demographic"].Add('B', "Body Shape", "Robustness/Weight/Physique/Girth"); enRef["Demographic"].Add('C', "Country", "Continent/Region"); enRef["Demographic"].Add('D', "Damage", "Natural Damage"); enRef["Demographic"].Add('E', "Experience", ""); enRef["Demographic"].Add('F', "Flying", "Air Speed"); enRef["Demographic"].Add('G', "Gender", "Sex"); enRef["Demographic"].Add('H', "Hit Points", ""); enRef["Demographic"].Add('I', "Insanity", "madness/Sanity"); enRef["Demographic"].Add('J', "Jesus", "Gift/Grace/Faith/Religion"); enRef["Demographic"].Add('K', "Kingdom", "Ethnicity/Nation/State/People"); enRef["Demographic"].Add('L', "Level", ""); enRef["Demographic"].Add('M', "Movement", "Speed/Ground/Land"); enRef["Demographic"].Add('N', "Nature", "Personality/Temperment"); enRef["Demographic"].Add('O', "Organization", "School/Business/Guild"); enRef["Demographic"].Add('P', "Position", "Social Status/class"); enRef["Demographic"].Add('Q', "Quester", "Player"); enRef["Demographic"].Add('R', "Race", "Species"); enRef["Demographic"].Add('S', "Swimming", "Sailing"); enRef["Demographic"].Add('T', "Tallness", "Height/Size/Length"); enRef["Demographic"].Add('U', "Underwater move", ""); enRef["Demographic"].Add('V', "", ""); EndemeList field = new EndemeList("test", enRef, 0.73); field.Add("HeroName", "Identity", "A", ""); field.Add("Species", "Demographic", "R", ""); field.Add("Gender", "Demographic", "G", ""); field.Add("First Name", "Identity", "F", ""); field.Add("Last Name", "Identity", "L", ""); field.Add("Full Name", "Identity", "LF", ""); //field.SetFieldExactly("Identity:F" , "Jon" ); // field.SetField(field.EnRef[EndemeField.Part(0,"Identity:F" )], EndemeField.Part(1,"Identity:F" ), "Jon" ); // field["Identity:F"] = "Jon" ; //field.SetFieldExactly("Identity:L" , "Grover" ); // field.SetField(field.EnRef[EndemeField.Part(0,"Identity:L" )], EndemeField.Part(1,"Identity:L" ), "Grover" ); // field["Identity:L"] = "Grover" ; //field.SetFieldExactly("Identity:LF", "Jon Grover"); // field.SetField(field.EnRef[EndemeField.Part(0,"Identity:LF")], EndemeField.Part(1,"Identity:LF"), "Jon Grover"); // field["Identity:LF"] = "Jon Grover"; EndemeValue elFirst = field.GetField("Identity:F").Item; // field["Identity:F" ]; EndemeValue elLast = field.GetField("Identity:L").Item; // field["Identity:L" ]; EndemeValue elFull = field.GetField("Identity:LF").Item; // field["Identity:LF"]; string first = TreatAs.StrValue(field.GetField("Identity:F"), ""); string last = TreatAs.StrValue(field.GetField("Identity:L"), ""); string full = TreatAs.StrValue(field.GetField("Identity:LF"), ""); //string first = field.StrValue("Identity:F" , ""); //string last = field.StrValue("Identity:L" , ""); //string full = field.StrValue("Identity:LF", ""); Assert.That(first, Is.equal_to, "Jon"); Assert.That(last, Is.equal_to, "Grover"); Assert.That(full, Is.equal_to, "Jon Grover"); _result += Assert.Conclusion; }
private static void TreatAs_StrValue_testcase(object obj, string defaultValue, string target) { string value = TreatAs.StrValue(obj, defaultValue); Assert.That(value, Is.equal_to, target); }