'********************************************************************************************************** ' Nazwa: kluczeSlownika ' Autor: mielk | 2013-04-10 ' ' Opis: Zwraca zestaw kluczy podanego obiektu typu Dictionary jako jednowymiarowa tablica. ' ' Argumenty: ' dict Obiekt typu Dictionary, któego klucze mają zostać zwrócone. ' ' Zwraca: ' Variant() Tablica kluczy podanego obiektu typu Dictionary. ' ' ' Wyjątki: ' NiedozwolonyTypDanych Wywoływany, kiedy podany parametr nie jest obiektem typu Dictionary. ' ' ' --- Zmiany ---------------------------------------------------------------------------------------------- ' 2013-04-10 mielk Utworzenie funkcji '********************************************************************************************************** Public Function kluczeSlownika(dict As Variant) As Variant() Const NAZWA_METODY As String = "kluczeSlownika" '------------------------------------------------------------------------------------------------------ Const DICTIONARY As String = "Dictionary" '------------------------------------------------------------------------------------------------------ Dim varKlucze As Variant Dim tablica() As Variant Dim lngElement As Long '------------------------------------------------------------------------------------------------------ 'Sprawdź czy podaby argument wejściowy jest obiektem typu Dictionary. If VBA.StrComp(VBA.TypeName(dict), DICTIONARY, vbTextCompare) Then GoTo NiedozwolonyTypDanych 'Jeżeli podany słownik jest pusty, zwrócona zostanie pusta tablica. W takiej sytuacji nie ma więc sensu 'przeprowadzać żadnych dodatkowych operacji. If dict.Count Then 'Zmień rozmiar tablicy wynikowej [tablica], żeby pomieściła wszystkie klucze z podanego 'obiektu typu Dictionary. ReDim tablica(1 To dict.Count) For Each varKlucze In dict.keys lngElement = lngElement + 1 If VBA.IsObject(varKlucze) Then Set tablica(lngElement) = varKlucze Else tablica(lngElement) = varKlucze End If Next varKlucze End If 'Przypisz tablicę z kluczami do zmiennej będącej wynikiem funkcji. kluczeSlownika = tablica '========================================================================================================== PunktWyjscia: Exit Function '---------------------------------------------------------------------------------------------------------- NiedozwolonyTypDanych: 'Obsługa błędów dla sytuacji, kiedy podany parametr nie jest obiektem typu Dictionary. GoTo PunktWyjscia End Function ' ' ''[4Export] ''[@uid:] ''[@name:Get dictionary values as array] ''[@keywords:vba dictionary values functions arrays] ''[@tooltip:Function to get the array containing all the values from the given dictionary]] ''********************************************************************************************************** '' Nazwa: getValuesArray '' Autor: mielk | 2013-04-10 '' '' Opis: Returns set of values of the specified dictionary as a one-dimensional array. '' '' Argumenty: '' dict Dictionary which value are to be returned. '' '' Zwraca: '' Variant() Array of the values from the given dictionary. '' '' '' Wyjątki: '' NiedozwolonyTypDanych '' Thrown if the given parameter is not a dictionary. '' '' '' --- Zmiany ---------------------------------------------------------------------------------------------- '' 2013-04-10 mielk Utworzenie funkcji ''********************************************************************************************************** 'Public Function getValuesArray(dict As Variant) As Variant() ' Const NAZWA_METODY As String = "getValuesArray" ' '------------------------------------------------------------------------------------------------------ ' Const DICTIONARY_TYPENAME As String = "Dictionary" ' '------------------------------------------------------------------------------------------------------ ' Dim varKey As Variant ' Dim arr() As Variant ' Dim lngItem As Long ' '------------------------------------------------------------------------------------------------------ ' ' ' 'Check if the given parameter dict is a dictionary. ' If VBA.StrComp(VBA.TypeName(dict), DICTIONARY_TYPENAME, vbTextCompare) Then GoTo NiedozwolonyTypDanych ' ' ' 'If the given dictionary is empty, empty array will be returned. ' If dict.Count Then ' ' 'Resize final table [arr] to be big enough for ' 'all the items from the given dictionary. ' ReDim arr(1 To dict.Count) ' For Each varKey In dict.keys ' lngItem = lngItem + 1 ' ' If VBA.IsObject(dict.item(varKey)) Then ' Set arr(lngItem) = dict.item(varKey) ' Else ' arr(lngItem) = dict.item(varKey) ' End If ' ' Next varKey ' ' End If ' ' ' 'Assign the final table to the result variable. ' getValuesArray = arr ' ' ''========================================================================================================== 'PunktWyjscia: ' Exit Function ' ''---------------------------------------------------------------------------------------------------------- 'NiedozwolonyTypDanych: ' '(...) ' 'Put your own error handling here for a case if the given parameter is not a dictionary. ' ' ' Call Err.Raise(number:=ERR_ILLEGAL_TYPE, Source:=NAZWA_METODY, _ ' description:="Parameter [dict] given to this function is not a dictionary") ' ' ' ' GoTo PunktWyjscia ' 'End Function '