You've either had too much or not enough coffee when...
-
public class MyObject
{
public Value1 { get; set; }
public Value2 { get; set; }
public Value3 { get; set; }/// /// Initializes a new instance of the class. /// /// The first value. /// The second value. /// The third value. public MyObject(int value1, int value2, int value3) { Value1 = value1; Value2 = value2; Value3 = Value3; }
}
var value = new MyObject(1,2,3);
Why is MyObject.Value3 always equal to 0? /slaps self repeatedly, and insert appropriate comic[^]
cheers Chris Maunder
-
It has a few humorous properties? :) That said, I'm surprised there were no compiler errors. UPDATE: I was focused on the missing types on your properties (cut/paste error in posting?). I think it would be a "capital" idea if you fixed those...then, the other issue might be more apparent :)
Eric Lynch wrote:
I'm surprised there were no compiler errors.
It is always interesting to me that C# doesn't produce a compiler error for that. But, I guess it figures you know best. :rolleyes: Maybe there's a warning, but we all ignore warnings. :laugh:
-
The code presented is perfectly acceptable to the compiler. Resharper will notify you that input parameter value3 is not being used, which I would then have noticed that. I don't know if the latest version of VS2017 shows that too, I think it does. Also, I think the unused input parameter may also show up as a compiler warning, but could be wrong there, depending on a person's settings, etc.
I was talking about the missing types on the property declarations. Just updated before I read your reply.
-
public class MyObject
{
public Value1 { get; set; }
public Value2 { get; set; }
public Value3 { get; set; }/// /// Initializes a new instance of the class. /// /// The first value. /// The second value. /// The third value. public MyObject(int value1, int value2, int value3) { Value1 = value1; Value2 = value2; Value3 = Value3; }
}
var value = new MyObject(1,2,3);
Why is MyObject.Value3 always equal to 0? /slaps self repeatedly, and insert appropriate comic[^]
cheers Chris Maunder
you misspelled
value3
.".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
Eric Lynch wrote:
I'm surprised there were no compiler errors.
It is always interesting to me that C# doesn't produce a compiler error for that. But, I guess it figures you know best. :rolleyes: Maybe there's a warning, but we all ignore warnings. :laugh:
I think it does issue a warning when you assign something to itself...at least I recall seeing one. I was more focused on the missing types in the property declarations.
-
you misspelled
value3
.".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013You forgot to capitalize the first letter of your sentence.
-
public class MyObject
{
public Value1 { get; set; }
public Value2 { get; set; }
public Value3 { get; set; }/// /// Initializes a new instance of the class. /// /// The first value. /// The second value. /// The third value. public MyObject(int value1, int value2, int value3) { Value1 = value1; Value2 = value2; Value3 = Value3; }
}
var value = new MyObject(1,2,3);
Why is MyObject.Value3 always equal to 0? /slaps self repeatedly, and insert appropriate comic[^]
cheers Chris Maunder
-
public class MyObject
{
public Value1 { get; set; }
public Value2 { get; set; }
public Value3 { get; set; }/// /// Initializes a new instance of the class. /// /// The first value. /// The second value. /// The third value. public MyObject(int value1, int value2, int value3) { Value1 = value1; Value2 = value2; Value3 = Value3; }
}
var value = new MyObject(1,2,3);
Why is MyObject.Value3 always equal to 0? /slaps self repeatedly, and insert appropriate comic[^]
cheers Chris Maunder
-
Eric Lynch wrote:
I'm surprised there were no compiler errors.
It is always interesting to me that C# doesn't produce a compiler error for that. But, I guess it figures you know best. :rolleyes: Maybe there's a warning, but we all ignore warnings. :laugh:
There is no warning, which surprised me - and I have "treat warnings as errors" set by default ...
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
There is no warning, which surprised me - and I have "treat warnings as errors" set by default ...
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
There (sort of) is a warning for self-assignment. If you assign
value3 = value3
you do get a warning. If you assign
Value3 = Value3
you do not get a warning. Strange, the warning must only be for self-assignment of variables, but not properties?
-
There (sort of) is a warning for self-assignment. If you assign
value3 = value3
you do get a warning. If you assign
Value3 = Value3
you do not get a warning. Strange, the warning must only be for self-assignment of variables, but not properties?
Not that strange: properties are syntactic sugar for getter and setter methods, so what you are actually doing is:
Value3 = Value3;
Value3_setter(Value3_getter());
But the compiler should have spotted it:: lazy programmers strike again ... :laugh:
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Not that strange: properties are syntactic sugar for getter and setter methods, so what you are actually doing is:
Value3 = Value3;
Value3_setter(Value3_getter());
But the compiler should have spotted it:: lazy programmers strike again ... :laugh:
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
Yeah, in their defense, I guess there are cases where that "self-assignment" might actually have "desired" side effects (such as modifying some other local variable). Though, I'd still like it if the compiler kicked out a low level warning...mostly, because I'm bound to make that mistake myself sometime :(
-
You forgot to capitalize the first letter of your sentence.
that's okay, because my sentence is not part of any widely recognized API...
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
public class MyObject
{
public Value1 { get; set; }
public Value2 { get; set; }
public Value3 { get; set; }/// /// Initializes a new instance of the class. /// /// The first value. /// The second value. /// The third value. public MyObject(int value1, int value2, int value3) { Value1 = value1; Value2 = value2; Value3 = Value3; }
}
var value = new MyObject(1,2,3);
Why is MyObject.Value3 always equal to 0? /slaps self repeatedly, and insert appropriate comic[^]
cheers Chris Maunder
Chris Maunder wrote:
Why is MyObject.Value3 always equal to 0?
Because your code is doing what you told it to do - not what you want it to do? (That is usually my problem with my code!)
Socialism is the Axe Body Spray of political ideologies: It never does what it claims to do, but people too young to know better keep buying it anyway. (Glenn Reynolds)
-
public class MyObject
{
public Value1 { get; set; }
public Value2 { get; set; }
public Value3 { get; set; }/// /// Initializes a new instance of the class. /// /// The first value. /// The second value. /// The third value. public MyObject(int value1, int value2, int value3) { Value1 = value1; Value2 = value2; Value3 = Value3; }
}
var value = new MyObject(1,2,3);
Why is MyObject.Value3 always equal to 0? /slaps self repeatedly, and insert appropriate comic[^]
cheers Chris Maunder
-
public class MyObject
{
public Value1 { get; set; }
public Value2 { get; set; }
public Value3 { get; set; }/// /// Initializes a new instance of the class. /// /// The first value. /// The second value. /// The third value. public MyObject(int value1, int value2, int value3) { Value1 = value1; Value2 = value2; Value3 = Value3; }
}
var value = new MyObject(1,2,3);
Why is MyObject.Value3 always equal to 0? /slaps self repeatedly, and insert appropriate comic[^]
cheers Chris Maunder
All languages have their stupid parts, this is in my opinion one of the major ones of the languages deriving from C.
Wrong is evil and must be defeated. - Jeff Ello
-
public class MyObject
{
public Value1 { get; set; }
public Value2 { get; set; }
public Value3 { get; set; }/// /// Initializes a new instance of the class. /// /// The first value. /// The second value. /// The third value. public MyObject(int value1, int value2, int value3) { Value1 = value1; Value2 = value2; Value3 = Value3; }
}
var value = new MyObject(1,2,3);
Why is MyObject.Value3 always equal to 0? /slaps self repeatedly, and insert appropriate comic[^]
cheers Chris Maunder
Unfortunately that happens, and aging doesn't help. Interestingly enough, in spite of 0x01AA remark,
g++
doesn't complain aboutclass Foo
{
int F;
public:
Foo(int f){F = F;}
//...But it does complain about (which is, by the way, the construct every sensible
C++
developer would have chosen)class Foo
{
int F;
public:
Foo(int f):F(F){}
//...spitting out a sane
warning: ‘Foo::F’ is initialized with itself
-
public class MyObject
{
public Value1 { get; set; }
public Value2 { get; set; }
public Value3 { get; set; }/// /// Initializes a new instance of the class. /// /// The first value. /// The second value. /// The third value. public MyObject(int value1, int value2, int value3) { Value1 = value1; Value2 = value2; Value3 = Value3; }
}
var value = new MyObject(1,2,3);
Why is MyObject.Value3 always equal to 0? /slaps self repeatedly, and insert appropriate comic[^]
cheers Chris Maunder
-
public class MyObject
{
public Value1 { get; set; }
public Value2 { get; set; }
public Value3 { get; set; }/// /// Initializes a new instance of the class. /// /// The first value. /// The second value. /// The third value. public MyObject(int value1, int value2, int value3) { Value1 = value1; Value2 = value2; Value3 = Value3; }
}
var value = new MyObject(1,2,3);
Why is MyObject.Value3 always equal to 0? /slaps self repeatedly, and insert appropriate comic[^]
cheers Chris Maunder
I don't even know what language this is, but I will presume Java. :^) You have Value3 being assigned to the value of ... Value3, so nothing happens there. Evidently, it is initialized to the value of 0. Is it a default int when there is no declaration type? :confused: