JudgeDreed
JudgeDreed

Reputation: 153

Having a static variable called as argument

I would like to implement a structure which contains a defined color issued from System.Drawing.Color. Here is my final code.

namespace ColorSystem
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    internal void testfunc(Label lbl, System.Drawing.Color newcolor)
    {
        lbl.BackColor = newcolor;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var getvalue = ColorValues.NormalFont;
        testfunc(label1, ColorValues.NormalFont);
        testfunc(label2, ColorValues.NormalFont2);
    }

}
public static class ColorValues
{
    // Private variable to hold font once instantiated
    public static System.Drawing.Color NormalFont{ get{ return Color.AliceBlue;}}//Dynamic
    public static System.Drawing.Color NormalFont2 =         Color.AliceBlue;//static ??
}

What I still don't get is what the difference between those two lines:

 public static System.Drawing.Color NormalFont{ get{ return        Color.AliceBlue;}}//Dynamic
 public static System.Drawing.Color NormalFont2 =         Color.AliceBlue;//static ??

When I look with the mouse I see that.

What interpreter show me

What is the difference between the cube and the hand? Hand is the (get) so is it dynamic? and the cube is a data in memory... ??

Thanks a lot folks!

Upvotes: 2

Views: 61

Answers (3)

CrudaLilium
CrudaLilium

Reputation: 672

Your first example works, but then you say you want make your method take your struct ColorValue as parameter, sure you can do it, but you can't fill BackColor of Label with your struct, because it is of different type and as you guess you would have to pass instance of your struct as you did in your last example.

So either go with your first example, or with your last, but you have to create instances of your struct.

Regarding memory, if you created 1000 instances of Form1 you would make 1000 instances of ColorValue, but if your struct ColorValue will stay as it is, it is only 1 byte. Generally memory is not an issue.

Edit: What is the difference between the following?

public static System.Drawing.Color NormalFont{ get{ return        Color.AliceBlue;}}//Dynamic
public static System.Drawing.Color NormalFont2 =         Color.AliceBlue;//static ??

First one is a property, while the second is a field. Properties have set and get methods, so difference is that trying to get value of property will call get method which will return what you want, while field is direct reference to what you want. It is generally advised to expose only properties and hide fields, reason being, that you cannot (or cannot easily) debug/monitor access to fields, while it is easy to debug access to properties.

Upvotes: 1

Jon
Jon

Reputation: 3255

I would use a static class, and have some read-only properties. Only instantiate the colors when they are used, so your class will start out with all nulls (no memory allocated), then as you read/get each property, it will instantiate based on the System.Drawing.Color

public static class ColorValues
{
    // Private variable to hold font once instantiated
    private static System.Drawing.Color _normalFont;
    public static System.Drawing.Color NormalFont 
    {
        get
        {
            // Only instantiate when needed
            if (_normalFont == null)
            {
                _normalFont = Color.AliceBlue;         
            }
            return _normalFont;
        }
    }
}

Then you can use in your form like:

   private void button1_Click(object sender, EventArgs e)
   {
       testfunc(label1, ColorValues.NormalFont);
   }

I tried this

  public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }

    internal void testfunc(Label lbl, ColorValues newcolor)
    {
        lbl.BackColor = newcolor;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        testfunc(label1, ColorValues.NormalFont);
    }

}
public static class ColorValues
{
    // Private variable to hold font once instantiated
    public static System.Drawing.Color NormalFont{ get{ return Color.AliceBlue;}}
}
/*  public struct ColorValue
{
    public static System.Drawing.Color NormalFont = Color.AliceBlue;
}*/

Upvotes: 0

arias_JC
arias_JC

Reputation: 559

Your testfunc should look like this:

internal void testfunc(Label lbl, ColorValue newcolor)
{
    lbl.BackColor = newcolor.NormalFont;
}

How are you passing in your struct? It should be like so:

ColorValue colorstruct;
testfunc(somelabel, colorstruct);

Also, consider changing the name of NormalFont since its not really a font but a color.

Upvotes: 1

Related Questions