Click here to Skip to main content
15,899,314 members
Home / Discussions / C#
   

C#

 
AnswerRe: Windows service & Timers Pin
22491714-Feb-10 17:33
22491714-Feb-10 17:33 
GeneralRe: Windows service & Timers Pin
koleraba14-Feb-10 23:50
koleraba14-Feb-10 23:50 
QuestionA few pointers toward design Pin
Saksida Bojan12-Feb-10 9:00
Saksida Bojan12-Feb-10 9:00 
AnswerRe: A few pointers toward design Pin
Dan Mos12-Feb-10 9:13
Dan Mos12-Feb-10 9:13 
GeneralRe: A few pointers toward design Pin
Rozis12-Feb-10 11:45
Rozis12-Feb-10 11:45 
AnswerRe: A few pointers toward design Pin
DaveyM6912-Feb-10 13:18
professionalDaveyM6912-Feb-10 13:18 
GeneralRe: A few pointers toward design [modified] Pin
Saksida Bojan12-Feb-10 18:54
Saksida Bojan12-Feb-10 18:54 
GeneralRe: A few pointers toward design Pin
DaveyM6912-Feb-10 23:42
professionalDaveyM6912-Feb-10 23:42 
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.
C#
test t1 = new test();
Great, you now have a new test instance which has two values, t1.x == 0 and t2.y == 0.
You now have two issues. Firstly, you change t1 with
C#
t1.x = 30;
This should no longer be the same test instance... remember the Size 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 overriding Equals and GetHashCode.

So, even for a simple struct that holds two ints, you end up with something like this (untested!).
C#
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? (Christian Graus)

GeneralRe: A few pointers toward design Pin
Saksida Bojan13-Feb-10 0:10
Saksida Bojan13-Feb-10 0:10 
GeneralRe: A few pointers toward design Pin
DaveyM6912-Feb-10 23:55
professionalDaveyM6912-Feb-10 23:55 
Questionbaseclass and a interface. Pin
Jarno Burger12-Feb-10 8:42
Jarno Burger12-Feb-10 8:42 
AnswerRe: baseclass and a interface. Pin
Saksida Bojan12-Feb-10 10:16
Saksida Bojan12-Feb-10 10:16 
AnswerRe: baseclass and a interface. Pin
rhuiden12-Feb-10 10:24
rhuiden12-Feb-10 10:24 
GeneralRe: baseclass and a interface. Pin
Jarno Burger14-Feb-10 13:33
Jarno Burger14-Feb-10 13:33 
Questioncodes Pin
ajay 201012-Feb-10 7:27
ajay 201012-Feb-10 7:27 
AnswerRe: codes Pin
Wes Aday12-Feb-10 7:46
professionalWes Aday12-Feb-10 7:46 
GeneralRe: codes Pin
ajay 201012-Feb-10 16:41
ajay 201012-Feb-10 16:41 
GeneralRe: codes Pin
Wes Aday12-Feb-10 17:11
professionalWes Aday12-Feb-10 17:11 
GeneralRe: codes Pin
ajay 201012-Feb-10 18:04
ajay 201012-Feb-10 18:04 
GeneralRe: codes Pin
ajay 201012-Feb-10 18:51
ajay 201012-Feb-10 18:51 
AnswerRe: codes Pin
hammerstein0512-Feb-10 8:00
hammerstein0512-Feb-10 8:00 
GeneralRe: codes Pin
ajay 201012-Feb-10 16:43
ajay 201012-Feb-10 16:43 
GeneralRe: codes Pin
hammerstein0512-Feb-10 18:35
hammerstein0512-Feb-10 18:35 
GeneralRe: codes Pin
ajay 201012-Feb-10 18:46
ajay 201012-Feb-10 18:46 
AnswerRe: codes Pin
OriginalGriff12-Feb-10 8:20
mveOriginalGriff12-Feb-10 8:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.