0

I currently have some global variables like this (the global part isn't really relevant):

public Brush backgroundColor;
public Brush textColor;
public double timeOffset;
public double dateOffset;
public string title;
public bool showTitle;
public bool showText;

I declare a new List<string> to store said variables using this:

List<string> x = new List<string>();
x.Add(backgroundColor);
x.Add(textColor);
x.Add(timeOffset.ToString());
x.Add(dateOffset.ToString());
x.Add(title);
x.Add(showTitle.ToString());
x.Add(showText.ToString());

Noticeably, I'm only temporarily storing these strings into my list. I will be using them later on as objects. If I wanted to convert my strings from this list to types like bool or double, I can simply us Convert.ToDouble() or Convert.ToBoolean(), however, I am unable to find anything that could do so for a Brush object.

My Brush object is used like this: Brushes.Black (Reference MSDN). I've looked at this thread, but the ways they input are either in RGB or Hexadecimal, which isn't what I need.

EDIT: If there is no way, or you can suggest better ways to use this, please let me know. Why I need a Brushes.XXXX is because I am drawing images onto bitmap objects.

Community
  • 1
  • 1
theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169
  • 3
    You shouldn't be storing "colors" as brushes. Store them as colors.. then build a brush based on them. You simply cannot create a single brush with the properties of two other brushes... – Simon Whitehead Jan 22 '14 at 22:02
  • How do you even use that list? Do you "remember" what the type of the item in each index is? – Dave Zych Jan 22 '14 at 22:04
  • Why store them as strings anyway? Create a class as a wrapper or at least store them in a list of `object`, this will make the casting really a lot easier. – Abbas Jan 22 '14 at 22:04
  • @DaveZych Yes. It's a list of only seven objects. @SimonWhitehead Why I am doing it this way is basically because I have a method that takes an input for `Brush`, like this: `public void manualBackgroundColor(Brush backgroundColor)` which draws things onto a `graphics` object. – theGreenCabbage Jan 22 '14 at 22:05
  • @Abbas Like this? `List settings = new List()`? – theGreenCabbage Jan 22 '14 at 22:06
  • 1
    @theGreenCabbage `It's a list of only seven objects` - Yes, and you're _only_ doing it wrong. – Dave Zych Jan 22 '14 at 22:07
  • @DaveZych No need to be snarky.. I'm only here to learn. What do you suggest? – theGreenCabbage Jan 22 '14 at 22:08
  • @theGreenCabbage Review my answer. – Dave Zych Jan 22 '14 at 22:08
  • @theGreenCabbage yes like that. But a more correct way is to create a class holding the properties and then you can pass a single instance of the class around to do stuff. No ugly casting needed. :) – Abbas Jan 22 '14 at 22:10

2 Answers2

3

I would advise creating a class to hold all of this information:

public class MyDrawingInfo
{
    public Brush BackgroundColor;
    public Brush TextColor;
    public double TimeOffset;
    public double DateOffset;
    public string Title;
    public bool ShowTitle;
    public bool ShowText;
}

Now you can instantiate a class with this info, pass it around and not have to perform some crazy casting everywhere.

EDIT: To use this class, first instantiate it:

MyDrawingInfo mdi = new MyDrawingInfo();
mdi.BackgroundColor = Brushes.Black;
//etc

Then, when calling another method, pass the property you need:

ManualBackgroundColor(mdi.BackgroundColor)

Note: I updated the names of the properties and your method name. It is standard in C# to have your property and method names be Pascal Case

Dave Zych
  • 21,581
  • 7
  • 51
  • 66
  • Hey Dave. How would I use this, for example, pass `backgroundColor` to the input of a method that's outside of this class? – theGreenCabbage Jan 22 '14 at 22:14
  • Cool, cool. Hey, so one more question. I need to have my seven variables parsed in through a list no matter what, since that's the design of the settings component (which is what houses the variables above). It must be accessed through 0-6th index. How do I store those into the global vars of the `MyDrawingInfo` class? Someone above suggested using `List` – theGreenCabbage Jan 22 '14 at 22:23
  • 1
    I was the one who proposed using this, check my answer for a more detailed explanation. :) – Abbas Jan 22 '14 at 23:06
1

If you need the properties in a list, I suggest you use List<object>. Since all classes in .NET derive from the object class, casting will be a lot more logic than casting a string to an object. Since you're using a list of objects, you don't need a class anymore to hold the properties. Also, you can use the object initializer to create the list instead of calling the Add() method every time. The assignment of the list will look like:

List<object> properties = new List<object>
{
    backgroundColor,
    textColor,
    timeOffset,
    dateOffset,
    title,
    showTitle,
    showText
};

And now you can pass this list to your method, like this:

YourListExpectingMethod(properties);

In the method you'll have to provide some logic to determine to which type to cast the object!

More reading:

Community
  • 1
  • 1
Abbas
  • 14,186
  • 6
  • 41
  • 72
  • Hey Abbas - if I wanted to access `backgroundColor` of `properties`, do I simply do so with `properties.backgroundColor`? Do I need to specify getters or setters below? Thanks! – theGreenCabbage Jan 23 '14 at 19:31
  • No, they are in the list. So you access them through an indexer, like this: `Brush backgroundColor = (Brush)properties[0];`. Getters and setters are for properties and properties belong to classes. The 'properties' in the list are just items from that list. – Abbas Jan 23 '14 at 19:53