C# Snippets


ArrayList_SortRecordsSingle

Public Shared Function SortRecordsSingle(ByVal arArrayList As ArrayList, _
ByVal iNumberOfColumns As Integer, _
ByVal iSortByColumn As Integer, _
Optional ByVal bAscending As Boolean = True) As ArrayList

Try
If gbEND_GENERAL = True Then Exit Function

Dim bsorted As Boolean
Dim iarraylistcount As Integer
Dim sngcomparevalue1 As Single
Dim sngcomparevalue2 As Single
Dim icolumncount As Integer
Dim asTempArray As String()

ReDim asTempArray(iNumberOfColumns - 1)

'Repeat until the array list is sorted correctly
Do
bsorted = True

'start looping through the array from the 2nd record
For iarraylistcount = 1 To (arArrayList.Count \ iNumberOfColumns) - 1

sngcomparevalue1 = CType(arArrayList(((iarraylistcount - 1) * iNumberOfColumns) + (iSortByColumn - 1)), Single)
sngcomparevalue2 = CType(arArrayList((iarraylistcount * iNumberOfColumns) + (iSortByColumn - 1)), Single)

If sngcomparevalue1 > sngcomparevalue2 Then

For icolumncount = 0 To (iNumberOfColumns - 1)
asTempArray(icolumncount) = _
CType(arArrayList(((iarraylistcount - 1) * iNumberOfColumns) + icolumncount), String)
Next icolumncount

For icolumncount = 0 To (iNumberOfColumns - 1)
arArrayList(((iarraylistcount - 1) * iNumberOfColumns) + icolumncount) = _
arArrayList((iarraylistcount * iNumberOfColumns) + icolumncount)
Next icolumncount

For icolumncount = 0 To (iNumberOfColumns - 1)
arArrayList((iarraylistcount * iNumberOfColumns) + icolumncount) = _
asTempArray(icolumncount)
Next icolumncount

bsorted = False
End If

Next iarraylistcount

Loop Until bsorted = True

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("SortRecordsSingle", msCLASSNAME, _
"", _
mobjCOMException, mobjException)
End If
End Try

End Function

ArrayList_SortRecordsString

Public Shared Function SortRecordsString(ByVal arArrayList As ArrayList, _
ByVal iNumberOfColumns As Integer, _
ByVal iSortByColumn As Integer, _
Optional ByVal bAscending As Boolean = True) _
As ArrayList

Try
If gbEND_GENERAL = True Then Exit Function

Dim bsorted As Boolean
Dim iarraylistcount As Integer
Dim scomparevalue1 As String
Dim scomparevalue2 As String
Dim icolumncount As Integer
Dim asTempArray As String()

ReDim asTempArray(iNumberOfColumns - 1)

'Repeat until the array list is sorted correctly
Do
bsorted = True

'start looping through the array from the 2nd record
For iarraylistcount = 1 To (arArrayList.Count \ iNumberOfColumns) - 1

scomparevalue1 = CType(arArrayList(((iarraylistcount - 1) * iNumberOfColumns) + (iSortByColumn - 1)), String)
scomparevalue2 = CType(arArrayList((iarraylistcount * iNumberOfColumns) + (iSortByColumn - 1)), String)

'if scomparevalue1 > scomparevalue2
If String.Compare(scomparevalue1, scomparevalue2) = 1 Then

For icolumncount = 0 To (iNumberOfColumns - 1)
asTempArray(icolumncount) = _
CType(arArrayList(((iarraylistcount - 1) * iNumberOfColumns) + icolumncount), String)
Next icolumncount

For icolumncount = 0 To (iNumberOfColumns - 1)
arArrayList(((iarraylistcount - 1) * iNumberOfColumns) + icolumncount) = _
arArrayList((iarraylistcount * iNumberOfColumns) + icolumncount)
Next icolumncount

For icolumncount = 0 To (iNumberOfColumns - 1)
arArrayList((iarraylistcount * iNumberOfColumns) + icolumncount) = _
asTempArray(icolumncount)
Next icolumncount

bsorted = False
End If

Next iarraylistcount

Loop Until bsorted = True

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("SortRecordsString", msCLASSNAME, _
"", _
mobjCOMException, mobjException)
End If
End Try
End Function

ArrayList_ToArrayString

Public Shared Function ToArrayString(ByVal alArrayList As ArrayList) _
As String()

Dim iarraycount As Integer
Dim astemp As String()

Try
If gbEND_GENERAL = True Then Exit Function

ReDim astemp(alArrayList.Count - 1)

For iarraycount = 0 To alArrayList.Count - 1
astemp(iarraycount) = CStr(alArrayList(iarraycount))
Next

Return astemp

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("ToArrayString", msCLASSNAME, _
"", _
mobjCOMException, mobjException)
End If
End Try
End Function

ArrayList_ToArrayStringUnique

Public Shared Function ToArrayStringUnique(ByVal alArrayList As ArrayList) _
As String()

Dim iarraycount As Integer

Try
If gbEND_GENERAL = True Then Exit Function

alArrayList.Sort()

For iarraycount = alArrayList.Count - 1 To 1 Step -1
If CStr(alArrayList(iarraycount)) = CStr(alArrayList(iarraycount - 1)) Then
alArrayList.Remove(alArrayList(iarraycount))
End If
Next

Return clsArrayList.ToArrayString(alArrayList)

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("ToArrayStringUnique", msCLASSNAME, _
"", _
mobjCOMException, mobjException)
End If
End Try
End Function

Collection_ToArray

source code
source code

Dictionary_Counting_Add

public static Dictionary_Counting_Add( 
System.Collections.Generic.Dictionary MyDictionary,
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.Dictionary MyDictionary)
{
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_ToStringConCat

Public Function Dictionary_ToStringConCat(ByVal dicItems As System.Collections.Generic.Dictionary(Of String, String), _
ByVal sItemDelimiter As String) _
As String

Dim keyvaluepair As System.Collections.Generic.KeyValuePair(Of String, String)
Dim svalue As String = ""
Dim sconcat As String = ""

Try
If My.Settings.ERROR_OCCURRED = True Then Return "" : Exit Function

If (dicItems.Count = 0) Then
Return ""
Exit Function
End If

For Each keyvaluepair In dicItems
svalue = keyvaluepair.Value
sconcat &= svalue & sItemDelimiter
Next

Return sconcat.Substring(0, sconcat.Length - 1)

Catch ex As Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message, _
"", False)
Return ""
End Try
End Function

DictionaryArray_ItemExistsInArray

Public Function DictionaryArray_ItemExistsInArray(ByVal dicItemsArray As System.Collections.Generic.Dictionary(Of String, String()), _
ByVal sFindKey As String, _
ByVal sFindItem As String) _
As Boolean

Dim keyvaluepair As System.Collections.Generic.KeyValuePair(Of String, String())

Try
If My.Settings.ERROR_OCCURRED = True Then Return False : Exit Function

If (dicItemsArray Is Nothing) Then
Return False
Exit Function
End If

If (dicItemsArray.Count = 0) Then
Return False
Exit Function
End If

For Each keyvaluepair In dicItemsArray
If (sFindKey = keyvaluepair.Key) Then
Return modGeneral.Array_ItemExists(keyvaluepair.Value, sFindItem)
Exit Function
End If
Next

Return False

Catch ex As Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message, _
"", False)
Return False
End Try
End Function

DictionaryArray_ToStringConCat

Public Function DictionaryArray_ToStringConCat(ByVal dicItems As System.Collections.Generic.Dictionary(Of String, String()), _
ByVal iArrayItem As Integer, _
ByVal sItemDelimiter As String) _
As String

Dim keyvaluepair As System.Collections.Generic.KeyValuePair(Of String, String())
Dim svalue As String = ""
Dim sconcat As String = ""

Try
If My.Settings.ERROR_OCCURRED = True Then Return "" : Exit Function

If (dicItems Is Nothing) Then
Return ""
Exit Function
End If

If (dicItems.Count = 0) Then
Return ""
Exit Function
End If

For Each keyvaluepair In dicItems
svalue = keyvaluepair.Value(iArrayItem)
sconcat &= svalue & sItemDelimiter
Next

Return sconcat.Substring(0, sconcat.Length - 1)

Catch ex As Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message, _
"", False)
Return ""
End Try
End Function

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top