0

I currently have a function with output myResult a dictionary of array of byte. I want to convert it into a dictionary of list of byte since for each entry I may store more than 1 array of byte. What is the format to replace the array with a list and how do I add each array to the list. The current format is the following:

int img_sz = img0->width * img0->height * img0->nChannels;

array <Byte>^ hh = gcnew array<Byte> (img_sz);

Marshal::Copy( (IntPtr)img->imageData, hh, 0, img_sz );

Dictionary<String^,array< Byte >^>^ myResult = gcnew Dictionary<String^,array< Byte >^>(); 

myResult["OVERVIEW"]=hh;

Any help is appreciated.

1 Answer 1

1

I'm not entirely sure which one of these you're going for, so I'll answer them both.

Dictionary<String^, List<Byte>^>^

If you want to end up with Dictionary<String^, List<Byte>^>^, just call the List<T> constructor that takes an IEnumerable<T>, and add it to the dictionary as you are now.

Dictionary<String^,List<Byte>^>^ myResult = gcnew Dictionary<String^,List<Byte>^>(); 

myResult["OVERVIEW"] = gcnew List<Byte>(hh);

Dictionary<String^, List<array<Byte>^>^>^

If you want to end up with Dictionary<String^, List<array<Byte>^>^>^, you'll need to check the dictionary to see if it as a list for that key yet, add the list if not, and then add the new array to the list. Call this method with the various arrays and name of the list you want to store each of them in.

void AddToResults(Dictionary<String^, List<array<Byte>^>^>^ myResult, 
                  String^ key, 
                  array<Byte>^ hh)
{
    List<array<Byte>^>^ thisList;

    if(!myResult->TryGetValue(key, thisList))
    {
        thisList = gcnew List<array<Byte>^>();
        myResult->Add(key, thisList);
    }

    thisList->Add(hh);
}
Sign up to request clarification or add additional context in comments.

6 Comments

He meant Dictionary<String^, List<array<Byte>^>^>
So if I have hh1 and hh2 to store in the list, how do i store them?
What's the difference between List<Byte>^ and List<array<Byte>^>^? I thought that List<Byte^> should be able to solve it.
Based on your previous questions, I know you're working with images here. The former is a single image, but stored in a list, rather than in an array. The latter is a list of images. From your original question, it wasn't entirely clear which one you were trying to do, so I answered both.
Can I add multiple images if i use List<Byte>^ and then nameoflist->Add(hh)?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.