Code Snippets
Array_Combine2SingleArrays
public static float[] Array_Combine2SingleArrays(
string sArray1Name,
float[] sngArrayName1,
string sArray2Name,
float[] sngArrayName2,
int ilower = -1,
int iupper = -1)
{
try
{
if (gbEND_GENERAL == true)
return null;
if (ilower == -1)
ilower = 0;
if (iupper == -1)
iupper = sngArrayName1.Length - 1;
float[] sngArrayNameSum = new float[iupper + 1];
if (iupper == sngArrayName2.Length - 1)
{
for (int i = ilower; i <= iupper; i++)
{
sngArrayNameSum[i] = sngArrayName1[i] + sngArrayName2[i];
}
}
return sngArrayNameSum;
}
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_Combine2SingleArrays",
msCLASSNAME,
"combine the two single dimensional arrays '" +
sArray1Name + "' and '" + sArray2Name + "'" +
gsCRLF +
"and return the array which is the total of the two arrays.",
mobjCOMException,
mobjException);
}
}
return null;
}
Array_Combine2StringArrays
public static string[] Array_Combine2StringArrays(
string sArray1Name,
string[] asArrayName1,
string sArray2Name,
string[] asArrayName2)
{
try
{
if (gbEND_GENERAL == true)
return null;
int iarraycount;
int icombinationcount = 0;
// VB: ReDim asCombination(UBound1 + UBound2 - 1)
string[] asCombination =
new string[asArrayName1.GetUpperBound(0) + asArrayName2.GetUpperBound(0)];
// Copy non-empty items from array 1
for (iarraycount = 0; iarraycount < asArrayName1.Length; iarraycount++)
{
if (asArrayName1[iarraycount].Length > 0)
{
asCombination[icombinationcount] = asArrayName1[iarraycount];
icombinationcount++;
}
}
// Copy non-empty items from array 2
for (iarraycount = 0; iarraycount < asArrayName2.Length; iarraycount++)
{
if (asArrayName2[iarraycount].Length > 0)
{
asCombination[icombinationcount] = asArrayName2[iarraycount];
icombinationcount++;
}
}
return asCombination;
}
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_Combine2StringArrays",
msCLASSNAME,
"combine the two string dimensional arrays '" +
sArray1Name + "' and '" + sArray2Name + "'" +
gsCRLF +
"and return the array which is a concatenation of the two arrays.",
mobjCOMException,
mobjException);
}
}
return null;
}
Array_CountSingleMatch
Returns the total number of rows in a single dimensional array that match a particular string or value.public long Array_CountEntriesSingle(
string sArrayName,
object[] vArrayName,
string sText)
{
long larraycount;
long lnooftimes = 0;
try
{
// Loop from 1 to UBound - 1 (VB semantics)
for (larraycount = 1; larraycount <= vArrayName.Length - 2; larraycount++)
{
if (Convert.ToString(vArrayName[larraycount]) == sText)
{
lnooftimes++;
}
}
if (gbDEBUG == false)
return lnooftimes;
return lnooftimes;
}
catch (Exception)
{
// VB: On Error GoTo AnError
Error_Handle(
"Array_CountEntriesSingle",
msMODULENAME,
1,
"count the total number of entries in the single dimensional array " +
"\"" + sArrayName + "\"" + Environment.NewLine +
"that match \"" + sText + "\"");
return -1;
}
}
Array_DimensionExists
public bool Array_DimensionExists(
object vArray,
int iDimension)
{
try
{
// Attempt to get upper bound of the specified dimension
// If it fails, VB would jump to AnError
System.Array arr = (System.Array)vArray;
int ub = arr.GetUpperBound(iDimension - 1);
return true;
}
catch
{
// VB: On Error GoTo AnError
return false;
}
}
Array_ItemAddAfter
public static void Array_ItemAddAfter(
string sText,
string sArrayName,
ref System.Array vArrayName)
{
int iarraycount;
try
{
if (gbEND_GENERAL == true)
return;
if (vArrayName != null)
{
for (iarraycount = 0; iarraycount < vArrayName.Length; iarraycount++)
{
object current = vArrayName.GetValue(iarraycount);
vArrayName.SetValue(Convert.ToString(current) + sText, iarraycount);
}
}
}
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_ItemAddAfter",
msCLASSNAME,
"add the entry '" + sText + "'" +
"to end of the single dimensional array '" + sArrayName + "'",
mobjCOMException,
mobjException);
}
}
}
Array_ItemExists
Loops through an array to check if an item exists Could you use a SPLIT and a SEARCH instead of the loop?.public bool Array_ItemExists(
string[] arStringArray,
string sItemToFind)
{
try
{
bool bfound = false;
for (int icount = 0; icount <= arStringArray.GetUpperBound(0); icount++)
{
if (arStringArray[icount] == sItemToFind)
{
bfound = true;
break;
}
}
return bfound;
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);
return false;
}
}
Array_ItemGetLargestSingle
public static float Array_ItemGetLargestSingle(
string sArrayName,
System.Array sngArrayName,
float sngPreviousLargest = -1.0E+20f)
{
int iarrayno = 0;
float snglarge;
try
{
if (gbEND_GENERAL == true)
return 0f;
// Find first non-zero entry
while (Convert.ToSingle(sngArrayName.GetValue(iarrayno)) == 0f)
{
iarrayno++;
}
snglarge = Convert.ToSingle(sngArrayName.GetValue(iarrayno));
// Scan remaining entries
for (iarrayno = iarrayno + 1; iarrayno < sngArrayName.Length; iarrayno++)
{
float val = Convert.ToSingle(sngArrayName.GetValue(iarrayno));
if (val != 0f && val > snglarge)
{
snglarge = val;
}
}
// Apply previous-largest override
if (sngPreviousLargest != -1.0E+20f)
{
if (sngPreviousLargest > snglarge)
snglarge = sngPreviousLargest;
}
return snglarge;
}
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_ItemGetLargestSingle",
msCLASSNAME,
"return the largest number in the " +
"single dimensional array '" + sArrayName + "'",
mobjCOMException,
mobjException);
}
}
return 0f;
}
Array_ItemGetSmallestSingle
public static float Array_ItemGetSmallestSingle(
string sArrayName,
float[] sngArrayName,
float sngPreviousSmallest = 1.0E+20f,
int iupper = -1,
int ilower = -1)
{
int iarrayno = 0;
float sngsmall;
try
{
if (gbEND_GENERAL == true)
return 0f;
// Find first non-zero entry
while (sngArrayName[iarrayno] == 0f)
{
iarrayno++;
}
sngsmall = sngArrayName[iarrayno];
// Scan remaining entries
for (iarrayno = iarrayno + 1; iarrayno < sngArrayName.Length; iarrayno++)
{
float val = sngArrayName[iarrayno];
if (val != 0f && val < sngsmall)
{
sngsmall = val;
}
}
// Apply previous-smallest override
if (sngPreviousSmallest != 1.0E+20f)
{
if (sngPreviousSmallest < sngsmall)
sngsmall = sngPreviousSmallest;
}
return sngsmall;
}
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_ItemGetSmallestSingle",
msCLASSNAME,
"return the smallest number in the " +
"single dimensional array '" + sArrayName + "'",
mobjCOMException,
mobjException);
}
}
return 0f;
}
Array_OfArray
public bool Array_OfArray(
object vArray)
{
try
{
// VB: vvalue = vArray(0)(0)
// C#: cast to object[] first, then index again
var outer = (object[])vArray;
var inner = (object[])outer[0];
var vvalue = inner[0];
return true;
}
catch
{
// VB: On Error GoTo AnError ? Array_OfArray = False
return false;
}
}
Array_ParamArray1Dimension
public object Array_ParamArray1Dimension(
object vArray2Dimension)
{
try
{
// If it's not an array-of-arrays, return it unchanged
if (Array_OfArray(vArray2Dimension) == false)
{
return vArray2Dimension;
}
// VB: UBound(vArray2Dimension(0), 1)
// Means: length of first inner array - 1
var outer = (object[])vArray2Dimension;
var inner = (object[])outer[0];
object[] vtemp = new object[inner.Length];
for (int i = 0; i < inner.Length; i++)
{
vtemp[i] = inner[i];
}
return vtemp;
}
catch (Exception ex)
{
Error_Handle(
ex.Message,
"Array_ParamArray1Dimension");
return null;
}
}
Array_ReDim1
public object Array_ReDim1(
object vaArrayName,
long lUpper)
{
// VB: ReDim vaArrayName(lUpper)
// C#: create a new array of the same element type, size lUpper + 1
var arr = (Array)vaArrayName;
Type elementType = arr.GetType().GetElementType();
Array newArray = Array.CreateInstance(elementType, (int)lUpper + 1);
return newArray;
}
Array_RemoveChars
public static void Array_RemoveChars(
string sArrayName,
ref System.Array vArrayName,
int iNoOfCharsLeft,
int iNoOfCharsRight)
{
try
{
if (gbEND_GENERAL == true)
return;
for (int i = 0; i < vArrayName.Length; i++)
{
string stemp = Convert.ToString(vArrayName.GetValue(i));
// Remove left characters
stemp = stemp.Substring(iNoOfCharsLeft);
// Remove right characters
stemp = stemp.Substring(0, stemp.Length - iNoOfCharsRight);
vArrayName.SetValue(stemp, i);
}
}
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_RemoveChars",
msCLASSNAME,
"remove the '" + iNoOfCharsLeft + "' characters from the left" +
gsCRLF +
"and the '" + iNoOfCharsRight + "' characters from the right" +
" of every element in the single dimensional array '" + sArrayName + "'.",
mobjCOMException,
mobjException);
}
}
}
Array_RemoveCriteriaSingle
Removes all the entries from a single dimensional array that match a particular criteria. Normally used for filtering on numerical data. The slightly smaller single dimensional array is returned.public object Array_RemoveCriteriaSingle()
{
try
{
// VB: If gbDEBUG = False Then Exit Function
if (gbDEBUG == false)
return null;
// VB: 'Copy from Multi' — does nothing
return null;
}
catch (Exception ex)
{
Error_Handle(
"Array_RemoveCriteriaSingle",
msMODULENAME,
1,
"");
return null;
}
}
Array_RemoveCRsMulti
Removes all the carriage returns from the text entries in a multi dimensional array.public object Array_RemoveCRsMulti(
string sArrayName,
object vArrayName)
{
try
{
Array arr = (Array)vArrayName;
// VB: For icolumncounter = LBound(vArrayName,1) To UBound(vArrayName,1)
int colLower = arr.GetLowerBound(0);
int colUpper = arr.GetUpperBound(0);
// VB: For lrowcounter = LBound(vArrayName,2) To UBound(vArrayName,2)
int rowLower = arr.GetLowerBound(1);
int rowUpper = arr.GetUpperBound(1);
for (int col = colLower; col <= colUpper; col++)
{
for (int row = rowLower; row <= rowUpper; row++)
{
// Access element (col, row, 0)
string value = Convert.ToString(arr.GetValue(col, row, 0));
if (Str_Chars1013HasAny(value))
{
arr.SetValue(Str_Chars1013Remove(value), col, row, 0);
arr.SetValue("1", col, row, 1);
}
else
{
arr.SetValue(Str_Chars1013Remove(value), col, row, 0);
arr.SetValue("0", col, row, 1);
}
}
}
return arr;
}
catch (Exception ex)
{
// VB: Array_RemoveCRsMulti = vArrayName
// Call Error_Handle(...)
Error_Handle(
"Array_RemoveCRsMulti",
msMODULENAME,
1,
"remove all carriage returns from all entrie in the multi dimensional array \"" +
sArrayName + "\"");
return vArrayName;
}
}
Array_RemoveCRsSingle
Removes all the carriage returns from the text entries in a single dimensional array.public object Array_RemoveCRsSingle(
string sArrayName,
object vArrayName)
{
try
{
Array arr = (Array)vArrayName;
int rowLower = arr.GetLowerBound(0);
int rowUpper = arr.GetUpperBound(0);
for (int row = rowLower; row <= rowUpper; row++)
{
string value = Convert.ToString(arr.GetValue(row, 0));
if (Str_Chars1013HasAny(value))
{
arr.SetValue(Str_Chars1013Remove(value), row, 0);
arr.SetValue("1", row, 1);
}
else
{
arr.SetValue(Str_Chars1013Remove(value), row, 0);
arr.SetValue("0", row, 1);
}
}
return arr;
}
catch (Exception)
{
Error_Handle(
"Array_RemoveCRsSingle",
msMODULENAME,
1,
"remove all carriage returns from all entries in the single dimensional array \"" +
sArrayName + "\"");
return vArrayName;
}
}
Array_RemoveDuplicatesMulti
Removes any entries from a multi dimensional array that contain a duplicate entry in a given column. Maybe add all unique entries to a string, then populate again removing from the string concat and using Instr(). Alternatively use a collection ?? The slightly smaller multi dimensional array is returned TEST !!!.public object Array_RemoveDuplicatesMulti(
string sArrayName,
object vArrayName,
int iDuplicateCol)
{
Array arr = (Array)vArrayName;
try
{
// === VB: On Error Resume Next ===
// We simulate this by catching and ignoring exceptions inside the block.
var clCells = new System.Collections.Specialized.StringCollection();
int rowLower = arr.GetLowerBound(0);
int rowUpper = arr.GetUpperBound(0);
int colLower = arr.GetLowerBound(1);
int colUpper = arr.GetUpperBound(1);
// Reverse loop (UBound ? LBound)
for (int r = rowUpper; r >= rowLower; r--)
{
try
{
object keyValue = arr.GetValue(r, iDuplicateCol);
if (keyValue == null || keyValue == DBNull.Value || keyValue.ToString() == "")
{
clCells.Add("");
}
else
{
string key = Convert.ToString(keyValue);
clCells.Add(key); // VB Collection.Add item, key
}
}
catch
{
// swallow errors (VB: On Error Resume Next)
}
}
// === VB: On Error GoTo 0 ===
// No-op in C#
// === VB: On Error GoTo AnError ===
// We now switch to normal try/catch
// ReDim vArrayNameTemp(clCells.Count, UBound(vArrayName,2))
int newRowCount = clCells.Count;
int colCount = colUpper - colLower + 1;
object[,] vArrayNameTemp = new object[newRowCount + 1, colCount + 1];
long luniquecount = 1;
// Reverse loop again
for (int r = rowUpper; r >= rowLower; r--)
{
object vcompare = arr.GetValue(r, iDuplicateCol);
if (Array_IsItIn(vArrayNameTemp, vcompare, iDuplicateCol) == false)
{
for (int c = colLower; c <= colUpper; c++)
{
vArrayNameTemp[luniquecount, c] = arr.GetValue(r, c);
}
luniquecount++;
}
}
return vArrayNameTemp;
}
catch (Exception)
{
// VB: Array_RemoveDuplicatesMulti = vArrayName
Error_Handle(
"Array_RemoveDuplicatesMulti",
msMODULENAME,
1,
"remove any duplicate rows from the multi dimensional array \"" +
sArrayName + "\" in column \"" + iDuplicateCol + "\"");
return vArrayName;
}
}
Array_RemoveHiddenFolders
public static void Array_RemoveHiddenFolders(
ref System.Array asFoldersArray)
{
try
{
if (gbEND_GENERAL == true)
return;
int ifolderscount = 0;
// First pass: count non-hidden folders
for (int i = 0; i < asFoldersArray.Length; i++)
{
string path = Convert.ToString(asFoldersArray.GetValue(i));
var info = new System.IO.DirectoryInfo(path);
if ((info.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden)
{
ifolderscount++;
}
}
// VB: ReDim astemp(ifolderscount - 1)
System.Array astemp = Array.CreateInstance(typeof(string), ifolderscount);
int itempcount = 0;
// Second pass: copy non-hidden folders
for (int i = 0; i < asFoldersArray.Length; i++)
{
string path = Convert.ToString(asFoldersArray.GetValue(i));
var info = new System.IO.DirectoryInfo(path);
if ((info.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden)
{
astemp.SetValue(path, itempcount);
itempcount++;
}
}
// VB: ReDim asFoldersArray(astemp.Length - 1)
// asFoldersArray = astemp
asFoldersArray = 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(
"RemoveHiddenFolders",
msCLASSNAME,
"remove the hidden folders from the array.",
mobjCOMException,
mobjException);
}
}
}
Array_SortSingleString
public static string[] Array_SortSingleString(
string sArrayName,
System.Array vArrayName,
int iNumberOfColumns,
int iSortByColumn,
bool bAscending = true)
{
try
{
if (gbEND_GENERAL == true)
return null;
bool bsorted;
int iarraylistcount;
string scomparevalue1;
string scomparevalue2;
int icolumncount;
// VB: ReDim asTempArray(iNumberOfColumns - 1)
System.Array asTempArray = Array.CreateInstance(typeof(string), iNumberOfColumns);
// Repeat until sorted
do
{
bsorted = true;
// Loop from 2nd record
int recordCount = vArrayName.Length / iNumberOfColumns;
for (iarraylistcount = 1; iarraylistcount <= recordCount - 1; iarraylistcount++)
{
int prevIndex = (iarraylistcount - 1) * iNumberOfColumns + (iSortByColumn - 1);
int currIndex = (iarraylistcount * iNumberOfColumns) + (iSortByColumn - 1);
scomparevalue1 = Convert.ToString(vArrayName.GetValue(prevIndex));
scomparevalue2 = Convert.ToString(vArrayName.GetValue(currIndex));
// ASCENDING
if (bAscending)
{
if (string.Compare(scomparevalue1, scomparevalue2) == 1)
{
// Copy previous row into temp
for (icolumncount = 0; icolumncount < iNumberOfColumns; icolumncount++)
{
asTempArray.SetValue(
Convert.ToString(vArrayName.GetValue(((iarraylistcount - 1) * iNumberOfColumns) + icolumncount)),
icolumncount);
}
// Move current row into previous row
for (icolumncount = 0; icolumncount < iNumberOfColumns; icolumncount++)
{
vArrayName.SetValue(
vArrayName.GetValue((iarraylistcount * iNumberOfColumns) + icolumncount),
((iarraylistcount - 1) * iNumberOfColumns) + icolumncount);
}
// Move temp into current row
for (icolumncount = 0; icolumncount < iNumberOfColumns; icolumncount++)
{
vArrayName.SetValue(
asTempArray.GetValue(icolumncount),
(iarraylistcount * iNumberOfColumns) + icolumncount);
}
bsorted = false;
}
}
// DESCENDING
if (!bAscending)
{
if (string.Compare(scomparevalue2, scomparevalue1) == 1)
{
// Copy previous row into temp
for (icolumncount = 0; icolumncount < iNumberOfColumns; icolumncount++)
{
asTempArray.SetValue(
Convert.ToString(vArrayName.GetValue(((iarraylistcount - 1) * iNumberOfColumns) + icolumncount)),
icolumncount);
}
// Move current row into previous row
for (icolumncount = 0; icolumncount < iNumberOfColumns; icolumncount++)
{
vArrayName.SetValue(
vArrayName.GetValue((iarraylistcount * iNumberOfColumns) + icolumncount),
((iarraylistcount - 1) * iNumberOfColumns) + icolumncount);
}
// Move temp into current row
for (icolumncount = 0; icolumncount < iNumberOfColumns; icolumncount++)
{
vArrayName.SetValue(
asTempArray.GetValue(icolumncount),
(iarraylistcount * iNumberOfColumns) + icolumncount);
}
bsorted = false;
}
}
}
} while (bsorted == false);
// VB returns String()
string[] result = new string[vArrayName.Length];
for (int i = 0; i < vArrayName.Length; i++)
result[i] = Convert.ToString(vArrayName.GetValue(i));
return result;
}
catch (Exception ex)
{
clsError.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null, ex);
return null;
}
}
Array_SortSingleVariant
Sorts a single dimensional array into ascending order using the QuickSort method.public static void Array_SortSingleVariant(
System.Array saArrayName)
{
try
{
if (gbEND_GENERAL == true)
return;
System.Array.Sort(saArrayName);
}
catch (Exception ex)
{
clsError.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex,
"sort the array.");
}
}
Array_ToArraySingle
public static float[] Array_ToArraySingle(
System.Array objArray)
{
float[] result = null;
try
{
if (gbEND_GENERAL == true)
return null;
int iarraytotal = objArray.GetUpperBound(0);
// VB: ReDim sngArraySingle(iarraytotal - 1)
float[] sngArraySingle = new float[iarraytotal];
// VB loops from 1 ? UBound
for (int i = 1; i <= iarraytotal; i++)
{
sngArraySingle[i - 1] = Convert.ToSingle(objArray.GetValue(i));
}
return sngArraySingle;
}
catch (Exception ex)
{
clsError.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex);
return null;
}
}
Array_ToArrayString
public static string[] Array_ToArrayString(
System.Array objArray)
{
string[] result = null;
try
{
if (gbEND_GENERAL == true)
return null;
int iarraytotal = objArray.GetUpperBound(0);
// VB: ReDim asArrayString(iarraytotal - 1)
string[] asArrayString = new string[iarraytotal];
// VB loops from 1 ? UBound
for (int i = 1; i <= iarraytotal; i++)
{
asArrayString[i - 1] = Convert.ToString(objArray.GetValue(i));
}
return asArrayString;
}
catch (Exception ex)
{
clsError.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex);
return null;
}
}
public static System.Array ToArrayString_SystemArray(System.Array objArray)
{
System.Array result = null;
try
{
if (gbEND_GENERAL == true)
return null;
int iarraytotal = objArray.GetUpperBound(0);
// VB intent: System.Array.Resize(ByRef asArrayString, iarraytotal - 1)
// Correct C# equivalent:
System.Array asArrayString =
Array.CreateInstance(typeof(string), iarraytotal);
// VB loops from 1 ? UBound
for (int i = 1; i <= iarraytotal; i++)
{
asArrayString.SetValue(
Convert.ToString(objArray.GetValue(i)),
i - 1);
}
return asArrayString;
}
catch (Exception ex)
{
clsError.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex);
return null;
}
}
Array_ToArrayStringUnique
public static System.Array Array_ToArrayStringUnique(
string sArrayName,
System.Array saArrayName)
{
System.Array saArrayTemp;
System.Int32 ifindunder;
string sprevious = "";
string scurrent = "";
System.Collections.ArrayList alUniqueList;
System.Int32 iarraycount;
try
{
ifindunder = saArrayName(0).ToString.IndexOf("_");
if (ifindunder > 0)
{
sprevious = saArrayName(0).ToString.SubString(0, ifindunder - 1);
}
if (ifindunder = 0)
{
sprevious = saArrayName(0).ToString();
}
alUniqueList.Add(sprevious);
for (iarraycount = 0; iarraycount < saArrayName.GetUpperBound(0); iarraycount++)
{
ifindunder = saArrayName(iarraycount).ToString.IndexOf("_");
if (ifindunder > 0)
{
scurrent = saArrayName(iarraycount).ToString.SubString(0, ifindunder - 1);
}
if (ifindunder = 0)
{
scurrent = saArrayName(iarraycount).ToString();
}
//could we / should we use :
//if (alUniqueList.Contains(scurrent) == false) { }
if (scurrent != sprevious)
{
alUniqueList.Add(scurrent);
}
sprevious = scurrent;
}
saArrayTemp = alUniqueList.ToArray();
return saArrayTemp;
}
catch { }
finally { }
}
Array_ToCheckedListBox
public static void Array_ToCheckedListBox(
System.Windows.Forms.CheckedListBox chlbCheckedListBox,
string sArrayName,
System.Array saArrayName,
bool bClearList,
params string[] saExtrasBefore)
{
try
{
if (gbEND_GENERAL == true)
return;
// Clear list if requested
if (bClearList)
chlbCheckedListBox.Items.Clear();
// Add extras first
foreach (string sText in saExtrasBefore)
{
chlbCheckedListBox.Items.Add(sText);
}
// Add array contents
int lower = saArrayName.GetLowerBound(0);
int upper = saArrayName.GetUpperBound(0);
for (int i = lower; i <= upper; i++)
{
object value = saArrayName.GetValue(i);
if (value != null && value.ToString().Length > 0)
{
chlbCheckedListBox.Items.Add(Convert.ToString(value));
}
}
}
catch (Exception ex)
{
clsError.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex,
"transfer the contents of the array '" + sArrayName + "'" +
" to the checkedlistbox '" + chlbCheckedListBox.Name +
"' one element at a time");
}
}
Array_ToComboBox
public static void Array_ToComboBox(
System.Windows.Forms.ComboBox objComboBox,
string sArrayName,
System.Array saArrayName,
bool bClearList,
params string[] saExtrasBefore)
{
try
{
if (gbEND_GENERAL == true)
return;
string sText = "";
// Clear list if requested
if (bClearList)
objComboBox.Items.Clear();
// Add extras first
foreach (string extra in saExtrasBefore)
{
objComboBox.Items.Add(extra);
}
// Add array contents
for (int i = 0; i < saArrayName.Length; i++)
{
object value = saArrayName.GetValue(i);
if (value != null && value.ToString().Length > 0)
{
objComboBox.Items.Add(value.ToString());
}
}
}
catch (Exception ex)
{
clsError.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex,
"transfer the contents of the array '" + sArrayName + "'" +
" to the combobox '" + objComboBox.ToString() + "' one element at a time");
}
}
Array_ToDataGridView
public void Array_ToDataGridView(
System.Array arItemsArray,
string sArrayDelimiter,
System.Windows.Forms.DataGridView dgrDataGridView)
{
System.Windows.Forms.DataGridViewRow objDataGridViewRow;
System.Array arComponentsArray;
int iItemCount;
int icolumnno;
try
{
Tracer_Add2("SUBROUTINE",
System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
if (arItemsArray != null)
{
for (iItemCount = 0; iItemCount <= arItemsArray.GetUpperBound(0); iItemCount++)
{
// VB: Split(string, delimiter) returns a System.Array
arComponentsArray = Microsoft.VisualBasic.Strings.Split(
Convert.ToString(arItemsArray.GetValue(iItemCount)),
sArrayDelimiter);
dgrDataGridView.Rows.Add();
objDataGridViewRow = dgrDataGridView.Rows[dgrDataGridView.Rows.Count - 1];
for (icolumnno = 0; icolumnno <= dgrDataGridView.Columns.Count - 1; icolumnno++)
{
objDataGridViewRow.Cells[icolumnno].Value =
arComponentsArray.GetValue(icolumnno);
}
}
}
}
catch (Exception ex)
{
modMessages.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex);
}
}
Array_ToDataSet
public static Array_DataSet ToDataSet(
string sArrayName,
System.Array asArrayName)
{
try
{
if (gbEND_GENERAL == true)
return null;
DataSet objDataSet = new DataSet("TempDataSet");
DataTable objDataTable = new DataTable("ListBoxTable");
DataColumn objDataColumn;
DataRow objDataRow;
int icolumnnumber;
int irowcount;
// Create columns: Column0, Column1, ...
for (icolumnnumber = 0; icolumnnumber <= asArrayName.GetUpperBound(0); icolumnnumber++)
{
objDataColumn = new DataColumn("Column" + icolumnnumber);
objDataColumn.DataType = Type.GetType("System.String");
objDataTable.Columns.Add(objDataColumn);
}
// Create rows
for (irowcount = 0; irowcount <= asArrayName.GetUpperBound(1); irowcount++)
{
objDataRow = objDataTable.NewRow();
for (icolumnnumber = 0; icolumnnumber <= asArrayName.GetUpperBound(0); icolumnnumber++)
{
objDataRow["Column" + icolumnnumber] =
asArrayName.GetValue(icolumnnumber, irowcount);
}
objDataTable.Rows.Add(objDataRow);
}
objDataSet.Tables.Add(objDataTable);
return objDataSet;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
return null;
}
catch (Exception objException)
{
mobjException = objException;
return null;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ToDataSet",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
}
Array_ToListBox
public static void Array_ToListBox(
System.Windows.Forms.ListBox objListBox,
string sArrayName,
System.Array saArrayName,
bool bClearList,
params string[] saExtrasBefore)
{
try
{
if (gbEND_GENERAL == true)
return;
string sText = "";
// Clear list if requested
if (bClearList)
objListBox.Items.Clear();
// Add extras first
foreach (string extra in saExtrasBefore)
{
objListBox.Items.Add(extra);
}
// Add array contents
int lower = saArrayName.GetLowerBound(0);
int upper = saArrayName.GetUpperBound(0);
for (int i = lower; i <= upper; i++)
{
object value = saArrayName.GetValue(i);
if (value != null && value.ToString().Length > 0)
{
objListBox.Items.Add(value.ToString());
}
}
}
catch (Exception ex)
{
clsError.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex,
"transfer the contents of the array '" + sArrayName + "'" +
" to the listbox '" + objListBox.ToString() + "' one element at a time");
}
}
Array_ToListView
public static void Array_ToListView(
System.Windows.Forms.ListView objListView,
string Arrayname,
System.Array saArrayName,
bool bColumnHeadersIncluded = false,
bool bIncludeFolderBack = false,
bool bClearItems = false)
{
try
{
if (gbEND_GENERAL == true)
{
return;
}
System.Windows.Forms.ListViewItem objListViewItem;
int iarraycount;
int icolumncount;
objListView.BeginUpdate();
if (bClearItems == true)
{
objListView.Items.Clear();
}
if (bIncludeFolderBack == true)
{
objListView.Items.Add("...");
}
for (iarraycount = 0; iarraycount <= saArrayName.GetUpperBound(0); iarraycount++)
{
// First column becomes the ListViewItem.Text
objListViewItem = new System.Windows.Forms.ListViewItem(
saArrayName.GetValue(iarraycount, 0).ToString(),
0);
objListView.Items.Add(objListViewItem);
// Add subitems for remaining columns
for (icolumncount = 1; icolumncount <= saArrayName.GetUpperBound(1); icolumncount++)
{
objListViewItem.SubItems.Add(
saArrayName.GetValue(iarraycount, icolumncount).ToString());
}
}
objListView.EndUpdate();
}
catch (Exception ex)
{
clsError.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex);
}
}
Array_ToRegistryList
public static void Array_ToRegistryList(
string sArrayName,
System.Array saArrayName,
string sAppName,
string sSection,
System.Array asColumnsRegistryPrefix,
bool bDeleteExisting)
{
System.Int32 iarraycount;
System.Int32 icolcount;
try
{
if (clsRegistry.KeyExists(sAppName,sSection) == true) && (bDeleteExisting == true)
{
clsRegistry.Delete(sAppName,sSection);
}
for (iarraycount = 0; iarraycount < saArrayName.GetLength; iarraycount++)
{
for (icolcount = 0; icolcount < asColumnsRegistryPrefix.GetLength; icolcount++)
{
clsRegistry.Save(sAppName,
sSection,
asColumnsRegistryPrefix(icolcount) + " " + iarraycount + 1,
saArrayName(iarraycount,icolcount));
}
}
}
catch { }
finally { }
}
Array_ToStringConCat
public static string Array_ToStringConCat(
string sArrayName,
System.Array saArrayName,
string sSeperatorChar = ";")
{
string result = "";
try
{
if (gbEND_GENERAL == true)
return "";
string sconcat = "";
int iarraycount;
for (iarraycount = saArrayName.GetLowerBound(0);
iarraycount <= saArrayName.GetUpperBound(0);
iarraycount++)
{
object value = saArrayName.GetValue(iarraycount);
if (value != null && value.ToString().Length > 0)
{
// VB ignores sSeperatorChar — so C# must too
sconcat = sconcat + Convert.ToString(value);
}
}
// VB: Return sconcat.Substring(0, sconcat.Length - 1)
return sconcat.Substring(0, sconcat.Length - 1);
}
catch (Exception ex)
{
clsError.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex,
"transfer the contents of the array '" + sArrayName +
"' to a string concatenation one element at a time");
return "";
}
}
Array_ToTreeView
public static void Array_ToTreeView(
System.Windows.Forms.TreeNode objAddToNode,
string sArrayName,
System.Array saArrayName,
int iImageIndex = -1)
{
string sText;
int iarraycount;
try
{
if (gbEND_GENERAL == true)
return;
if (saArrayName == null)
{
clszMessagesGeneral.Message(
"The array argument 'saArrayName' is Nothing!",
"clsArray.ToTreeView");
return;
}
for (iarraycount = 0; iarraycount < saArrayName.Length; iarraycount++)
{
object value = saArrayName.GetValue(iarraycount);
if (value != null && value.ToString().Length > 0)
{
System.Windows.Forms.TreeNode newNode =
objAddToNode.Nodes.Add(Convert.ToString(value));
newNode.ImageIndex = iImageIndex;
}
}
}
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_ToTreeView",
msCLASSNAME,
"transfer the contents of the array '" + sArrayName + "'" +
" to the treeview node \"" + objAddToNode.Text +
"\" one element at a time",
mobjCOMException,
mobjException);
}
}
}
Array_ToTreeViewTags
public static void Array_ToTreeViewTags(
System.Windows.Forms.TreeNode objAddToNode,
string sArrayName,
System.Array saArrayName)
{
string sText;
int iarraycount;
try
{
if (gbEND_GENERAL == true)
return;
for (iarraycount = 0; iarraycount < saArrayName.Length; iarraycount++)
{
object value = saArrayName.GetValue(iarraycount);
if (value != null && value.ToString().Length > 0)
{
// VB assumes the node already exists — C# preserves this behaviour
objAddToNode.Nodes[iarraycount].Tag = Convert.ToString(value);
}
}
}
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_ToTreeViewTags",
msCLASSNAME,
"transfer the contents of the array '" + sArrayName + "'" +
" to the tags of the treeview node '" + objAddToNode.Text + "'",
mobjCOMException,
mobjException);
}
}
}
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top