Click here to Skip to main content
15,912,400 members
Home / Discussions / C#
   

C#

 
GeneralRe: Inaccessible due to its protection level Pin
ASPnoob12-Feb-10 17:25
ASPnoob12-Feb-10 17:25 
GeneralRe: Inaccessible due to its protection level Pin
Wes Aday12-Feb-10 17:37
professionalWes Aday12-Feb-10 17:37 
AnswerRe: Inaccessible due to its protection level Pin
DaveyM6912-Feb-10 22:56
professionalDaveyM6912-Feb-10 22:56 
AnswerRe: Inaccessible due to its protection level Pin
Luc Pattyn13-Feb-10 1:26
sitebuilderLuc Pattyn13-Feb-10 1:26 
QuestionCall a .aspx web-form from Microsoft CRM 4 Pin
quercus12-Feb-10 12:56
quercus12-Feb-10 12:56 
QuestionTTF file format Pin
sduhd12-Feb-10 12:13
sduhd12-Feb-10 12:13 
AnswerRe: TTF file format Pin
harold aptroot12-Feb-10 12:52
harold aptroot12-Feb-10 12:52 
QuestionWindows service & Timers Pin
koleraba12-Feb-10 10:06
koleraba12-Feb-10 10:06 
AnswerRe: Windows service & Timers Pin
PIEBALDconsult12-Feb-10 10:38
mvePIEBALDconsult12-Feb-10 10:38 
GeneralRe: Windows service & Timers Pin
koleraba12-Feb-10 11:01
koleraba12-Feb-10 11:01 
GeneralRe: Windows service & Timers Pin
PIEBALDconsult12-Feb-10 14:09
mvePIEBALDconsult12-Feb-10 14:09 
GeneralRe: Windows service & Timers Pin
koleraba12-Feb-10 22:36
koleraba12-Feb-10 22:36 
GeneralRe: Windows service & Timers Pin
PIEBALDconsult13-Feb-10 3:20
mvePIEBALDconsult13-Feb-10 3:20 
AnswerRe: Windows service & Timers Pin
DaveyM6912-Feb-10 10:47
professionalDaveyM6912-Feb-10 10:47 
GeneralRe: Windows service & Timers Pin
koleraba12-Feb-10 11:04
koleraba12-Feb-10 11:04 
QuestionRe: Windows service & Timers Pin
koleraba12-Feb-10 12:39
koleraba12-Feb-10 12:39 
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 

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.