📄 IrbyImaging.BusinessLogic/DataTypeConversion.cs Documentation


🗂️ Table of Contents

  1. 🎯 Overview & Purpose
  2. 🔧 Dependencies & Technologies
  3. 🚀 Key Functional Areas
  4. 💡 Usage Examples & Code Snippets
  5. 📊 UML Class Diagram
  6. ✅ Conclusion

🎯 Overview & Purpose

DataTypeConversion is a static utility class in the IrbyImaging.BusinessLogic namespace that provides a rich set of extension methods for:

  • String manipulation & validation
  • Numeric parsing & formatting
  • Date/time parsing & conversions
  • Data structure transformations
  • Cobol overpunch handling

These methods streamline common data‐cleaning and conversion tasks throughout the business logic layer, reducing boilerplate and improving consistency.


🔧 Dependencies & Technologies

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Globalization;
using System.Data;
using System.Xml;
using Newtonsoft.Json;
using IrbyImaging.DomainClasses;
using IrbyImaging.Utility;
using System.Text.RegularExpressions;
  • .NET Standard / .NET Framework
  • Newtonsoft.Json for JSON ↔ XML conversion
  • Reflection, DataTable, Regex, CultureInfo

🚀 Key Functional Areas

📝 String Utilities

Method Signature Description
RemoveSpaces string RemoveSpaces(this string value) Trims leading/trailing whitespace
ZeroIfEmpty string ZeroIfEmpty(this string s) Returns "0" if null/empty
SpaceIfEmpty string SpaceIfEmpty(this string s) Returns " " if null/empty
Like bool Like(this string toSearch, string toFind) SQL‐style pattern matching (%, _)
SafeToInteger int SafeToInteger(this string value) Parses to int or returns 0
SafeToNullableInteger int? SafeToNullableInteger(this string value) Parses to int? or null
ToBool bool ToBool(this string value) Flexible boolean parse ("yes", "true", "x")
ToYN string ToYN(this bool value) "Y" / "N"
FromYN bool FromYN(this string value) true if non‐empty
UpperCase string UpperCase(this string str) ToUpper() if non‐empty
Truncate string Truncate(this string source, int length) Cuts to max length
Right string Right(this string sValue, int maxLength) Returns rightmost N characters

🔢 Numeric Conversions & Formatters

Method Signature Description
ToInteger int ToInteger(this string value) Alias to SafeToInteger
ToLong long ToLong(this string value) Parses to long
ToDecimal decimal ToDecimal(this string value, bool implied = false) Parses to decimal, opt. implied /100
ToImpliedDecimal decimal ToImpliedDecimal(this string value) Handles "implied decimal" & Cobol O/P
ToImpliedNullableDecimal decimal? ToImpliedNullableDecimal(this string value) Nullable version
AddAll decimal AddAll(params string[] args) Sum of string decimals
AddAllString string AddAllString(params string[] args) Sum formatted to "0.00"
AddDecimal decimal AddDecimal(this string v1, string v2) Sum two strings as decimals
Digits string Digits(this decimal source, int digits=2, bool comma) Format with nX or fixed (F2)
ToDollar string ToDollar(this decimal source) "$1,234.56"
EmptyIfZero string EmptyIfZero(this decimal source, int digits=2) "" if zero, else nX
DecimalLeadingWithZero string DecimalLeadingWithZero(this decimal num,int len,bool) Pad decimal digits as string
IntLeadingWithZero string IntLeadingWithZero(this int number,int length) Zero‐pad integer

📅 Date & Time Conversions

Method Signature Description
ToIrbyDate DateTime ToIrbyDate(this string date) Parses yyyyMMdd or yyMMdd, handles invalids
ToNullableIrbyDate DateTime? ToNullableIrbyDate(this string date) Nullable Irby‐style date
ToDate DateTime ToDate(this string date) TryParseExact("yyyyMMdd"/"yyMMdd"), defaults 1900-01-01
ToNullableDate DateTime? ToNullableDate(this string date) Nullable version
IsZero bool IsZero(this string date) true if all zeros
IsValidDate bool IsValidDate(this DateTime date) false if 1900-01-01
DateToString string DateToString(this DateTime? date, string format="yyyyMMdd") Formats nullable date
ToDateTime DateTime ToDateTime(this string inputDateTime) Parses 14‐char "yyyyMMddHHmmss"
StringToDate DateTime StringToDate(string cent, string year, string mon, …) Build DateTime from cent/year/month/day/hour/min
StringToNullableDate DateTime? StringToNullableDate(string cent, string year, string mon, …) Nullable multi‐part date
ToIrbyTime string ToIrbyTime(this string value) Normalizes to 4‐digit HHmm, corrects overflow
ToAge int ToAge(this DateTime fromDate, DateTime toDate=default) Years difference
ToMonths int ToMonths(this DateTime fromDate, DateTime toDate=default) Months difference

📂 Data Structure Utilities

Method Signature Description
TrimmingRecordValues void TrimmingRecordValues(object record) Trim all public property string values
TrimmingRecordFields void TrimmingRecordFields(object record) Trim all public field string values
ToCSV string ToCSV(this DataTable table, string delimiter) Convert DataTable → CSV
JsonToCsv string JsonToCsv(string json) JSON array → CSV via XmlNode/DataSet
ConvertToDataTableWithRowInfo DataTable ConvertToDataTableWithRowInfo<T>(this IEnumerable<T> data) Project list to DataTable + audit columns
ToIntList List<int> ToIntList(this string[] stringList) Parse array of string → List<int>

💾 Cobol Overpunch Conversions

Cobol “overpunch” encodes the sign in the last digit's character.

Method Signature Description
DecimalToCobolOverPunch string DecimalToCobolOverPunch(decimal number,int len=0) Encode decimal → overpunch
IntToCobolOverPunch string IntToCobolOverPunch(int number,int len=0) Encode integer → overpunch
CobolOverPunchToInt int CobolOverPunchToInt(string value) Decode overpunch → int
CobolOverPunchToImpliedDecimal decimal CobolOverPunchToImpliedDecimal(string value) Decode → implied decimal

⚙️ Miscellaneous Utilities

Method Signature Description
ToPhoneFormat string ToPhoneFormat(this string value) (###)###-#### → "###-###-####"
IsValidIdNumber bool IsValidIdNumber(this string value) >0 check
ToAlpha string ToAlpha(this string value) Leaves letter/digit, whitespace, -
ToAlphaOnly string ToAlphaOnly(this string value) Leaves only [A-Z][a-z]
ToDelimitedAlpha string ToDelimitedAlpha(this string, string delimiter=",") Clean tokens in delimited string
ToShortZip string ToShortZip(this string value) Pad/truncate to 5 chars
ToSsnFormat string ToSsnFormat(this string value) "123456789" → "123-45-6789", pad with 9
IsSSNFormat bool IsSSNFormat(this string value) Validates "999-99-9999" pattern
HideSSN string HideSSN(this string ssn) "XXX-XXX-1234"
IsAnyValidString bool IsAnyValidString(params string[] args) Any non‐empty/non‐whitespace
RemoveDecimal string RemoveDecimal(this decimal number) Strip .
RemoveCurrency string RemoveCurrency(this decimal number) Strip $,,,.
ConvertNumberToText bool ConvertNumberToText(int num, out string result) 1–100000 → English words
CompareProperties List<string> CompareProperties(this object left, object right, List<string> ignoreProps=null) Diff two objects by public value‐type props
Between (long) bool Between(this long testing, long first, long last) Inclusive range check
Between (DateTime) bool Between(this DateTime testing, DateTime first, DateTime last) Date range check
StripPhoneFormat string StripPhoneFormat(this string value) Remove '(' , ')' , '-'
ToPic9String string ToPic9String(this String value, int length) PIC9 placeholder

💡 Usage Examples & Code Snippets

using IrbyImaging.BusinessLogic;

// String cleaning
string raw = "  Hello World  ";
string clean = raw.RemoveSpaces();         // "Hello World"
string zeroed = "".ZeroIfEmpty();          // "0"
bool matched = "abcd".Like("ab%");         // true

// Numeric parsing
int i = "123".ToInteger();                 // 123
int safe = "abc".SafeToInteger();          // 0
decimal d = "12345".ToDecimal(implied:true);// 123.45

// Date conversions
DateTime dt1 = "20220506".ToIrbyDate();    // 2022-05-06
DateTime? dt2 = "050606".ToNullableDate(); // 2005-06-06
string dtStr = dt1.DateToString("MM/dd/yyyy"); // "05/06/2022"

// Phone & SSN
string phone = "(123)456-7890".StripPhoneFormat().ToPhoneFormat(); 
// "123-456-7890"
string ssn  = "123456789".ToSsnFormat();  // "123-45-6789"

// Cobol overpunch
string punch =  (-321).IntToCobolOverPunch(5); 
// "0003z"  ('z' = -9)
int   normal= "0003z".CobolOverPunchToInt(); // -9

// DataTable CSV
DataTable tbl = new DataTable();
tbl.Columns.Add("Name");
tbl.Columns.Add("Age");
tbl.Rows.Add("Alice", 30);
string csv = tbl.ToCSV(",");
// "Name,Age\nAlice,30"

📊 UML Class Diagram

classDiagram
    direction LR
    class DataTypeConversion {
        <<static>>
        + string RemoveSpaces(string)
        + string ZeroIfEmpty(string)
        + string SpaceIfEmpty(string)
        + int ToInteger(string)
        + int? SafeToNullableInteger(string)
        + int SafeToInteger(string)
        + long ToLong(string)
        + bool ToBool(string)
        + string ToYN(bool)
        + string ToValidYN(string)
        + bool FromYN(string)
        + string ToDelimitedAlpha(string, string)
        + string ToAlpha(string)
        + string ToAlphaOnly(string)
        + string ToPhoneFormat(string)
        + bool IsValidIdNumber(string)
        + string ToSsnFormat(string)
        + bool IsSSNFormat(string)
        + decimal ToImpliedDecimal(string)
        + decimal? ToImpliedNullableDecimal(string)
        + decimal ToDecimal(string, bool)
        + decimal AddAll(params string[])
        + string AddAllString(params string[])
        + string ToShortZip(string)
        + string ToIrbyTime(string)
        + DateTime ToIrbyDate(string)
        + DateTime? ToNullableIrbyDate(string)
        + DateTime ToDate(string)
        + DateTime? ToNullableDate(string)
        + int ToAge(DateTime, DateTime)
        + int ToMonths(DateTime, DateTime)
        + DateTime ToDateTime(string)
        + string UpperCase(string)
        + void TrimmingRecordValues(object)
        + void TrimmingRecordFields(object)
        + string Truncate(string, int)
        + string Digits(decimal, int, bool)
        + string ToDollar(decimal)
        + string EmptyIfZero(decimal, int)
        + bool IsAnyValidString(params string[])
        + string HideSSN(string)
        + string RemoveDecimal(decimal)
        + string RemoveCurrency(decimal)
        + string DecimalLeadingWithZero(decimal, int, bool)
        + string IntLeadingWithZero(int, int)
        + string DecimalToCobolOverPunch(decimal, int)
        + string IntToCobolOverPunch(int, int)
        + int CobolOverPunchToInt(string)
        + decimal CobolOverPunchToImpliedDecimal(string)
        + string ToCSV(DataTable, string)
        + string JsonToCsv(string)
        + bool IsValidDate(DateTime)
        + string DateToString(DateTime?, string)
        + string ToPic9String(string, int)
        + string StripPhoneFormat(string)
        + DataTable ConvertToDataTableWithRowInfo<T>(IEnumerable<T>)
        + List<int> ToIntList(string[])
        + string Right(string, int)
        + bool ConvertNumberToText(int, out string)
        + List<string> CompareProperties(object, object, List<string>)
        + bool Between(long, long, long)
        + bool Between(DateTime, DateTime, DateTime)
    }

✅ Conclusion

The DataTypeConversion class centralizes a comprehensive suite of extension methods that simplify routine tasks—ranging from trimming and parsing to advanced scenarios like Cobol overpunch decoding or flexible date/time conversions. By leveraging these helpers, developers can:

  • Minimize repetitive code
  • Improve consistency across the codebase
  • Safely handle invalid or malformed data

💡 Tip: Remember to include using IrbyImaging.BusinessLogic; at the top of your file to access these extensions seamlessly!