A few pointers toward design
-
Hello, currently I am learning to create custom controls. And now here are a few question. A book sugested to use Cache and to use WeakReference class. When I think of holding properties, I do not know if I should chose a class or a struct. For example: Button. I want to cache to remember all appearance properties. Is it better to use Class or a struct. Does a class and struct compare by reference? Is it faster if I call gdi directly instead GDI+? Is there a better drawing calls instead of GDI+ for a windows form controls?
-
Hello, currently I am learning to create custom controls. And now here are a few question. A book sugested to use Cache and to use WeakReference class. When I think of holding properties, I do not know if I should chose a class or a struct. For example: Button. I want to cache to remember all appearance properties. Is it better to use Class or a struct. Does a class and struct compare by reference? Is it faster if I call gdi directly instead GDI+? Is there a better drawing calls instead of GDI+ for a windows form controls?
Saksida Bojan wrote:
When I think of holding properties, I do not know if I should chose a class or a struct. For example: Button. I want to cache to remember all appearance properties. Is it better to use Class or a struct.
Well, it depends. First on how many appearance properties a button has. Second how many buttons and controls you have. With struct(value) you get a faster access to the properties but you could run in a
StackOverflowException
given that the stack memory is relatively small. So it's your decision, cause you know how many properties to hold and how many controls you'll create.Saksida Bojan wrote:
Is it faster if I call gdi directly instead GDI+?
may be faster is some cases but it is more cumbersome.
Saksida Bojan wrote:
Is there a better drawing calls instead of GDI+ for a windows form controls?
Not that I know of. Unless you're willing to dive into DX and treat your client area a a DX surface.
-
Saksida Bojan wrote:
When I think of holding properties, I do not know if I should chose a class or a struct. For example: Button. I want to cache to remember all appearance properties. Is it better to use Class or a struct.
Well, it depends. First on how many appearance properties a button has. Second how many buttons and controls you have. With struct(value) you get a faster access to the properties but you could run in a
StackOverflowException
given that the stack memory is relatively small. So it's your decision, cause you know how many properties to hold and how many controls you'll create.Saksida Bojan wrote:
Is it faster if I call gdi directly instead GDI+?
may be faster is some cases but it is more cumbersome.
Saksida Bojan wrote:
Is there a better drawing calls instead of GDI+ for a windows form controls?
Not that I know of. Unless you're willing to dive into DX and treat your client area a a DX surface.
I would choose for a class, because a struct only holds the data of the button and you will have actions too. Besides a class allocates memory for you. Realize that a window quikly has dozens of controls on it, and if for example you want to create a listview you may design the cells as buttons. A struct would be faster i guess but on the other hand classes are faster as you would expect. Your language has a huge support for classes while with structures you have to do everything yourself. GDI is faster then GDI+ and is to my opinion not bad at all. GDI+ has some features you can't mimic in GDI (anti-aliassing for example).
-
Hello, currently I am learning to create custom controls. And now here are a few question. A book sugested to use Cache and to use WeakReference class. When I think of holding properties, I do not know if I should chose a class or a struct. For example: Button. I want to cache to remember all appearance properties. Is it better to use Class or a struct. Does a class and struct compare by reference? Is it faster if I call gdi directly instead GDI+? Is there a better drawing calls instead of GDI+ for a windows form controls?
Saksida Bojan wrote:
should chose a class or a struct
This is a long running debate and the general consensus seems to be... Structs should be used for things that hold a specific value that cannot be changed. If it needs to be changed then a new instance is required (immutable). For example,
Size
. If either the width or height changes then a new instance is assigned to the variable as it's now a different size! An object such as aForm
would always be a class as changing a property would not create a new form. A class that holds other class/struct instances is normally the best way to go and far easier to deal with. There are other issues such as recommended size of structs etc. If you do seriously consider using a struct I would do some serious research first and learn about the stack/heap etc...Saksida Bojan wrote:
Does a class and struct compare by reference
Do you mean pass by reference? Structs(being values) are passed by value and classes are passed by reference, although you can use references with structs by using
ref / out
.Dave
Tip: Passing values between objects using events (C#) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Saksida Bojan wrote:
should chose a class or a struct
This is a long running debate and the general consensus seems to be... Structs should be used for things that hold a specific value that cannot be changed. If it needs to be changed then a new instance is required (immutable). For example,
Size
. If either the width or height changes then a new instance is assigned to the variable as it's now a different size! An object such as aForm
would always be a class as changing a property would not create a new form. A class that holds other class/struct instances is normally the best way to go and far easier to deal with. There are other issues such as recommended size of structs etc. If you do seriously consider using a struct I would do some serious research first and learn about the stack/heap etc...Saksida Bojan wrote:
Does a class and struct compare by reference
Do you mean pass by reference? Structs(being values) are passed by value and classes are passed by reference, although you can use references with structs by using
ref / out
.Dave
Tip: Passing values between objects using events (C#) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)Hmm, it seems with this i woud prefer to use class
DaveyM69 wrote:
Do you mean pass by reference? Structs(being values) are passed by value and classes are passed by reference, although you can use references with structs by using ref / out.
I meant like this:
struct test
{
public Int32 x;
public Int32 y;
}test t1 = new test();
test t2 = new test();
t1.x = 30;
t2.x = 30;
t1.y = 45;
t2.y = 45;if (t1 == t2) // Does it return true? or if t2.y is 20 returns false?
DaveyM69 wrote:
There are other issues such as recommended size of structs etc. If you do seriously consider using a struct I would do some serious research first and learn about the stack/heap etc...
Do you know where can i find good reading material for this?
modified on Saturday, February 13, 2010 3:26 AM
-
Hmm, it seems with this i woud prefer to use class
DaveyM69 wrote:
Do you mean pass by reference? Structs(being values) are passed by value and classes are passed by reference, although you can use references with structs by using ref / out.
I meant like this:
struct test
{
public Int32 x;
public Int32 y;
}test t1 = new test();
test t2 = new test();
t1.x = 30;
t2.x = 30;
t1.y = 45;
t2.y = 45;if (t1 == t2) // Does it return true? or if t2.y is 20 returns false?
DaveyM69 wrote:
There are other issues such as recommended size of structs etc. If you do seriously consider using a struct I would do some serious research first and learn about the stack/heap etc...
Do you know where can i find good reading material for this?
modified on Saturday, February 13, 2010 3:26 AM
Welcome to the fustrating world of structs! You have fallen into the first two traps without realising it as we all have done at some point.
test t1 = new test();
Great, you now have a new
test
instance which has two values,t1.x == 0
andt2.y == 0
. You now have two issues. Firstly, you changet1
witht1.x = 30;
This should no longer be the same
test
instance... remember theSize
example I gave you earlier? Also, as x (and y) are public, anyone can change them, which again should creade new instances. The solution to this is to only set the fields in a constructor, make them private and expose them as readonly properties. To test for equality using the == operator, it needs overloading and the individual values checked. This also requires overloading != and overridingEquals
andGetHashCode
. So, even for a simple struct that holds two ints, you end up with something like this (untested!).public struct TestStruct : IEquatable<TestStruct>
{
public static readonly TestStruct Empty = new TestStruct();private int x; private int y; public TestStruct(int x, int y) { this.x = x; this.y = y; } public static bool operator ==(TestStruct a, TestStruct b) { return (a.x == b.x) && (a.y == b.y); } public static bool operator !=(TestStruct a, TestStruct b) { return !(a == b); } public int X { get { return x; } } public int Y { get { return y; } } public override bool Equals(object obj) { if (obj is TestStruct) return Equals((TestStruct)obj); return false; } public bool Equals(TestStruct other) { return other == this; } public override int GetHashCode() { return x ^ y; } public override string ToString() { return string.Format("X={0}, Y={1}", x, y); }
}
Dave
Tip: Passing values between objects using events (C#) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? ( -
Hmm, it seems with this i woud prefer to use class
DaveyM69 wrote:
Do you mean pass by reference? Structs(being values) are passed by value and classes are passed by reference, although you can use references with structs by using ref / out.
I meant like this:
struct test
{
public Int32 x;
public Int32 y;
}test t1 = new test();
test t2 = new test();
t1.x = 30;
t2.x = 30;
t1.y = 45;
t2.y = 45;if (t1 == t2) // Does it return true? or if t2.y is 20 returns false?
DaveyM69 wrote:
There are other issues such as recommended size of structs etc. If you do seriously consider using a struct I would do some serious research first and learn about the stack/heap etc...
Do you know where can i find good reading material for this?
modified on Saturday, February 13, 2010 3:26 AM
Saksida Bojan wrote:
Do you know where can i find good reading material for this
I'm no authority on the under the covers stuff, do some googling and you will find plenty of results. Hopefully someone more knowledgeable about this stuff will see this and post.
Dave
Tip: Passing values between objects using events (C#) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Welcome to the fustrating world of structs! You have fallen into the first two traps without realising it as we all have done at some point.
test t1 = new test();
Great, you now have a new
test
instance which has two values,t1.x == 0
andt2.y == 0
. You now have two issues. Firstly, you changet1
witht1.x = 30;
This should no longer be the same
test
instance... remember theSize
example I gave you earlier? Also, as x (and y) are public, anyone can change them, which again should creade new instances. The solution to this is to only set the fields in a constructor, make them private and expose them as readonly properties. To test for equality using the == operator, it needs overloading and the individual values checked. This also requires overloading != and overridingEquals
andGetHashCode
. So, even for a simple struct that holds two ints, you end up with something like this (untested!).public struct TestStruct : IEquatable<TestStruct>
{
public static readonly TestStruct Empty = new TestStruct();private int x; private int y; public TestStruct(int x, int y) { this.x = x; this.y = y; } public static bool operator ==(TestStruct a, TestStruct b) { return (a.x == b.x) && (a.y == b.y); } public static bool operator !=(TestStruct a, TestStruct b) { return !(a == b); } public int X { get { return x; } } public int Y { get { return y; } } public override bool Equals(object obj) { if (obj is TestStruct) return Equals((TestStruct)obj); return false; } public bool Equals(TestStruct other) { return other == this; } public override int GetHashCode() { return x ^ y; } public override string ToString() { return string.Format("X={0}, Y={1}", x, y); }
}
Dave
Tip: Passing values between objects using events (C#) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Now i understand it. And that is to use class not struct. Thank you for your time