# .NET and More > WPF, WCF, WF >  WPF Brushes in an array?

## Megalith

Hi all,

I am working on a graph program, all works well but i have decided to add a feature to change the colour scheme. how this will work (if it can be done) is the relevant brush is obtained from an array. So herein lies the problem i am having. some of these schemes contain image brushes, some are drawing brushes, some solid brushes and some gradient brushes. There doesn't seem to be any type for an array that will allow any kind of brush (thought brushes would work).

Anyone have a possible solution to this issue? maybe i can use strings and somehow use this?

----------


## bflosabre91

couldnt you use a dictionary instead? Something like this should work..



```
       Dim di As New Dictionary(Of Int32, Brush)
        Dim br As New SolidColorBrush
        Dim lb As New LinearGradientBrush
        di.Add(0, br)
        di.Add(1, lb)
```

----------


## Megalith

ok i worked a lil on this and got this code which wont work


```
        brushColour(1, 0) = New ImageBrush
        brushColour(1, 0).imagesource = New BitmapImage(New Uri("Images/red.jpg", UriKind.RelativeOrAbsolute))
        brushColour(1, 1) = New SolidColorBrush
        brushColour(1, 1) = Brushes.Red
        mainPage.Rectangle1.Fill = brushColour(1, 0)
```

the program errors as soon as i try to use the array (filling it worked fine using type object). Looks like i'm going to have to create different arrays for each type (SolidColorBrush, ImageBrush, DrawingBrush, Linear GradientBrush etc)

@bflosabre91 using a dictionary produces the same problem.

----------


## bflosabre91

im still not convinced that a dictionary wouldnt be the way to go for this. what is the error you get when you try to use a brush in the array or dictionary? 

this worked for me with no problems. i was able to set rect.Fill = dict(0) and rect.Fill = dict(1)



```
    Private dict As New Dictionary(Of Int32, Brush)


        Dim im As New ImageBrush
        im.ImageSource = New BitmapImage(New Uri("C:/bubble.png", UriKind.RelativeOrAbsolute))
        dict.Add(0, im)
        Dim br As New SolidColorBrush
        br.Color = Colors.LightGreen
        dict.Add(1, br)
```

----------


## techgnome

Does it error here:
brushColour(1, 0).imagesource = New BitmapImage(N....

or here:
mainPage.Rectangle1.Fill = brushColour(1, 0)


-tg

----------


## techgnome

Megalith - the difference between what you are trying to do and what bflosabre91 has done is in the creation of the object... you're trying to do that right into the array, and then set properties that are specific to the inherited type you created... which is why I asked where the error happens... if you create an array of Brush type... to add an ImageBrush (which should be inheriting form Brush) ... you need to create an ImageBrush object, set it's properties, then ADD THAT to the array.  Same goes for a List(Of T) or Dictionary ... as long as your lists/arrays/what ever, are of Brush type, you should be able to add to them, but I don't think you can simply just do this:
brushColour(1, 0).imagesource = New BitmapImage(New Uri("Images/red.jpg", UriKind.RelativeOrAbsolute))
Because in order for that to work, your array would have to be an ImageBrush... which it isn't.

-tg

----------


## DeanMc

Seconded what techgnome says is true and I personally would also recommend Generic Containers over arrays. Either IList or IDictionary.

----------


## Megalith

ok guys thanks for the help  :Smilie:  i scrapped the array thing. @tg i got the error when i tried to change the rectangle fill. i declared the array as no type (presume this is type object?) anyway i updated to the dictionary idea (i'll look into IDictionary and IList too Dean) and had the sub in my globals module which caused an error (possibly circular???), when i moved it to within the same form as the listbox then it worked perfect and i can now change the colours as intended. 

I have one question, can i change the colour of a dictionary item after it is added? or will i need to remove the indexed item and replace it with a new brush of the type needed should i update the image somewhere. dont think i will need to but who knows :s

will work with this over the coming days and let you know if i have any issues.

----------


## chris128

> I have one question, can i change the colour of a dictionary item after it is added? or will i need to remove the indexed item and replace it with a new brush of the type needed


I think that depends on whether or not the Brush type is a Class or a Structure (reference type or value type). If its a Structure (i.e. a value type) I think you will find that you cannot update an existing item in the collection - well you can do it, you wont get an exception or anything, but it will be as if you hadn't done it. If its a Class (i.e. a reference type) then there should be no problem updating existing items in the collection  :Smilie: 
EDIT: Just to clarify, when I say "update an item in the collection" I mean modify a property of the item.

----------

