Code Snippets
ArrayList_SortRecordsSingle
public static ArrayList ArrayList_SortRecordsSingle(
ArrayList arArrayList,
int iNumberOfColumns,
int iSortByColumn,
bool bAscending = true)
{
try
{
if (gbEND_GENERAL == true)
return null;
bool bsorted;
float compare1;
float compare2;
string[] tempArray = new string[iNumberOfColumns];
// Repeat until the array list is sorted correctly
do
{
bsorted = true;
// Loop through records starting from the second record
for (int i = 1; i < (arArrayList.Count / iNumberOfColumns); i++)
{
compare1 = Convert.ToSingle(
arArrayList[((i - 1) * iNumberOfColumns) + (iSortByColumn - 1)]);
compare2 = Convert.ToSingle(
arArrayList[(i * iNumberOfColumns) + (iSortByColumn - 1)]);
bool swapNeeded = bAscending
? compare1 > compare2
: compare1 < compare2;
if (swapNeeded)
{
// Copy record (i-1) into temp
for (int col = 0; col < iNumberOfColumns; col++)
{
tempArray[col] = (string)arArrayList[((i - 1) * iNumberOfColumns) + col];
}
// Move record i into (i-1)
for (int col = 0; col < iNumberOfColumns; col++)
{
arArrayList[((i - 1) * iNumberOfColumns) + col] =
arArrayList[(i * iNumberOfColumns) + col];
}
// Move temp into record i
for (int col = 0; col < iNumberOfColumns; col++)
{
arArrayList[(i * iNumberOfColumns) + col] = tempArray[col];
}
bsorted = false;
}
}
} while (bsorted == true);
return arArrayList;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ArrayList_SortRecordsSingle",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
return null;
}
ArrayList_SortRecordsString
public static ArrayList ArrayList_SortRecordsString(
ArrayList arArrayList,
int iNumberOfColumns,
int iSortByColumn,
bool bAscending = true)
{
try
{
if (gbEND_GENERAL == true)
return null;
bool bsorted;
string compare1;
string compare2;
string[] tempArray = new string[iNumberOfColumns];
// Repeat until the array list is sorted correctly
do
{
bsorted = true;
// Loop through records starting from the second record
for (int i = 1; i < (arArrayList.Count / iNumberOfColumns); i++)
{
compare1 = (string)arArrayList[((i - 1) * iNumberOfColumns) + (iSortByColumn - 1)];
compare2 = (string)arArrayList[(i * iNumberOfColumns) + (iSortByColumn - 1)];
int cmp = String.Compare(compare1, compare2, StringComparison.Ordinal);
bool swapNeeded = bAscending ? (cmp > 0) : (cmp < 0);
if (swapNeeded)
{
// Copy record (i-1) into temp
for (int col = 0; col < iNumberOfColumns; col++)
{
tempArray[col] = (string)arArrayList[((i - 1) * iNumberOfColumns) + col];
}
// Move record i into (i-1)
for (int col = 0; col < iNumberOfColumns; col++)
{
arArrayList[((i - 1) * iNumberOfColumns) + col] =
arArrayList[(i * iNumberOfColumns) + col];
}
// Move temp into record i
for (int col = 0; col < iNumberOfColumns; col++)
{
arArrayList[(i * iNumberOfColumns) + col] = tempArray[col];
}
bsorted = false;
}
}
} while (bsorted == true);
return arArrayList;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ArrayList_SortRecordsString",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
return null;
}
ArrayList_ToArrayString
public static string[] ArrayList_ToArrayString(
ArrayList alArrayList)
{
int iarraycount;
string[] astemp;
try
{
if (gbEND_GENERAL == true)
return null;
astemp = new string[alArrayList.Count];
for (iarraycount = 0; iarraycount < alArrayList.Count; iarraycount++)
{
astemp[iarraycount] = Convert.ToString(alArrayList[iarraycount]);
}
return astemp;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"Array_ToArrayString",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
return null;
}
ArrayList_ToArrayStringUnique
public static string[] Array_ToArrayStringUnique(ArrayList alArrayList)
{
int iarraycount;
try
{
if (gbEND_GENERAL == true)
return null;
alArrayList.Sort();
for (iarraycount = alArrayList.Count - 1; iarraycount >= 1; iarraycount--)
{
if (Convert.ToString(alArrayList[iarraycount]) ==
Convert.ToString(alArrayList[iarraycount - 1]))
{
alArrayList.Remove(alArrayList[iarraycount]);
}
}
return clsArrayList.ToArrayString(alArrayList);
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"Array_ToArrayStringUnique",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
return null;
}
Dictionary_Counting_Add
public static Dictionary_Counting_Add(
System.Collections.Generic.DictionaryMyDictionary,
string myItem)
{
int counter;
try
{
if (MyDictionary.ContainsKey(myItem) == false)
{
counter = 1;
MyDictionary.Add(myItem, counter);
}
else
{
counter = MyDictionary[myItem];
MyDictionary[myItem] = counter++;
}
}
catch (System.Exception ex)
{
}
}
Dictionary_Counting_FindMax
public static Dictionary_Counting_FindMax(
System.Collections.Generic.DictionaryMyDictionary)
{
int occurrences_max;
int occurrences;
string myTopItem;
try
{
for (int icount = 0; icount < MyDictionary.Count; icount++)
{
occurrences = MyDictionary.ElementAt(icount).Value
if (occurrences > occurrences_max)
{
occurrences_max = occurrences;
myTopItem = MyDictionary.ElementAt(icount).Key
}
}
return myTopItem;
}
catch (System.Exception ex)
{
}
}
Dictionary_ToArrayMultiResults
System.Collections.Generic.DictionaryMyDictionary;
MyDictionary = new System.Collections.Generic.Dictionary();
occurrences = MyDictionary.ElementAt(icount).Value
MyDictionary.Values.ToArray()
Dictionary_ToStringConCat
public static string Dictionary_ToStringConCat(
DictionarydicItems,
string itemDelimiter)
{
try
{
if (Properties.Settings.Default.ERROR_OCCURRED)
return "";
if (dicItems.Count == 0)
return "";
string concat = "";
foreach (var kvp in dicItems)
concat += kvp.Value + itemDelimiter;
return concat.Substring(0, concat.Length - 1);
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message,
"",
false);
return "";
}
}
DictionaryArray_ItemExistsInArray
public bool DictionaryArray_ItemExistsInArray(
System.Collections.Generic.DictionarydicItemsArray,
string sFindKey,
string sFindItem)
{
try
{
if (My.Settings.ERROR_OCCURRED == true)
return false;
if (dicItemsArray == null)
return false;
if (dicItemsArray.Count == 0)
return false;
foreach (var kvp in dicItemsArray)
{
if (sFindKey == kvp.Key)
{
return modGeneral.Array_ItemExists(kvp.Value, sFindItem);
}
}
return false;
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message,
"",
false);
return false;
}
}
DictionaryArray_ToStringConCat
public string DictionaryArray_ToStringConCat(
System.Collections.Generic.DictionarydicItems,
int iArrayItem,
string sItemDelimiter)
{
string svalue = "";
string sconcat = "";
try
{
if (My.Settings.ERROR_OCCURRED == true)
return "";
if (dicItems == null)
return "";
if (dicItems.Count == 0)
return "";
foreach (var kvp in dicItems)
{
svalue = kvp.Value[iArrayItem];
sconcat += svalue + sItemDelimiter;
}
// Remove final delimiter
return sconcat.Substring(0, sconcat.Length - 1);
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message,
"",
false);
return "";
}
}
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top