Hi in my opinion property of my object shouold be 2, but after this code, is still 1, why?
internal class Program
{
private static void Main(string[] args)
{
MyClass value = new MyClass() { Property = 1 };
value.Property = value.Property++;
Console.WriteLine(value.Property);
Console.ReadKey();
}
}
internal class MyClass
{
public int Property;
}
in my opinion this should value.Property = value.Property++; first put to value what is in value and the increment property of this object, why id doesn't work?
What this does is:
Evaluate value.Property on the right hand side (result is 1) and remember the result
Increment value.Property (now it's equal to 2)
Assign the remembered result to value.Property (it's now again equal to 1!)
If you change Property to a property with an explicit getter and setter and put some debugging code inside the setter, you will see that it does indeed change values 1 -> 2 -> 1.
If you changed value.Property++ to ++value.Property, the first two steps would be reversed, so we 'd have:
Increment value.Property (now it's equal to 2)
Evaluate value.Property on the right hand side (it's still 2)
Assign the result of step 2 to value.Property (it's still 2)
Of course, this is unnecessarily complicated and one could even say, wrong. If you want to increment Property, all you have to do is this:
++value.Property;
Because valueProperty++ is the same as the following:
int Increment(ref int value)
{
var temp = value;
value = value + 1;
return temp;
}
value.Property = value.Property++;
Here value.Property++ means it assing 1 before incrementing.
Because the = operator is lower down the order of operator precedence than increment.
http://msdn.microsoft.com/en-us/library/6a71f45d.aspx <- shows al the operators and their order of precedence.
The increment gets evaluated first completely. then the returned value from the increment is put through the = operator.
Related
I'm trying to better understand the use of properties, in particular within Unity game engine.
What I have to do is very simple: there is a class called "Enemy" that, every time it is instanciated (i.e. spawned), must access to the class GameSession and increase the value of a counter field, "numSpawnedEnemies", that however I want to be accessible through the property "NumSpawnedEnemies".
Note: GameSession is a singleton (i.e only 1 instance exists), but I omitted the related code for simplicity.
public class GameSession : MonoBehaviour
{
int numSpawnedEnemies = 0;
public int NumSpawnedEnemies
{
get
{
return numSpawnedEnemies;
}
set
{
Debug.Log("Value: " + value); // Just for debugging purpose
numSpawnedEnemies += value;
}
}
}
In the Enemy class I then do this, in the Start() method (i.e. the method automatically prompted by Unity for each instance of the Enemy class):
gameSession = FindObjectOfType<GameSession>();
// Unity function searching for the instance of GameSession
gameSession.NumSpawnedEnemies += 1;
The result, however, is not what I expected: in fact the counter "numSpawnedEnemies" gets increased every time x2 (i.e. 1, 2, 4, 8, 16, 32, 64...), rather than +1. That because, as the debug.console function shows, the value of "value" is, actually, 1, 2, 4, etc.
Setting the value to 0 (after assigning its value to numSpawnedEnemies) in the set {} doesn't change anything, but even if it would, that wouldn't explain to me this behaviour (in fact I expect that it get reset every time I access the property).
If I simply put numSpawnedEnemies as public and increment this field from the Enemy class, it works fine.
The questions then are:
How comes that "value" keeps track of a previous value? (shouldn't it be "reset" every time I access the property from some point?)
If answer to question 1 is that it is a normal behaviour, why does it get increased by x2 each time?
Thanks in advance!
You have += on both the property and the Enemy using it.
In the property replace
numSpawnedEnemies += value;
With
numSpawnedEnemies = value;
Short solution like
#Pino De Francesco said
change statement in property from
numSpawnedEnemies += value;
to
numSpawnedEnemies = value;
Explaination
You can treat the property like 'Get' and 'Set' method depend on you implenment.
So your NumSpawnedEnemies be like
void NumSpawnedEnemies(int value){
numSpawnedEnemies += value;
}
int NumSpawnedEnemies(){ return numSpawnedEnemies;}
then if you call NumSpawnedEnemies += 1
it equal you pass numberSpanwedEnemies + 1 as value
to NumSpawnedEnemies(int value)
then final equation of your statement is
numberSpawnedEnemies = numberSpawnedEnemies + value
numberSpawnedEnemies = numberSpawnedEnemies + (numberSpawnedEnemies + 1)
that why your result is double each time you called
I have an enumeration like so:
[Flags]
public enum UserProcessStage : uint
{
ShopSelection = 1,
FillBasket = 2,
SpecifyShipmentCredentials = 4,
SpecifyPaymentCredentials = 8,
OrderComplete = 16
}
Assuming I have a variable whose value is FillBakset (2), what I want to do is be able to increment it to the next value that is defined within the enumeration (SpecifyShipmentCredentials, 4).
The problem is that incrementing it causes its value to be 3 since it is based on an integer, I tried multipliying it by 2 but got a compilation error.
How could I increment an enumeration value to the next one ?
Thanks
You can use this code. It basically orders the enum by underlying value and then givs you the first enum which is bigger than the one specified. If none found, it will return 0 because of DefaultIfEmty():
public static UserProcessStage GetNext(UserProcessStage value)
{
return (from UserProcessStage val in Enum.GetValues(typeof (UserProcessStage))
where val > value
orderby val
select val).DefaultIfEmpty().First();
}
I got simple problem, but couldn't find solution:
Is it possible to check if given variable has been assigned?
int i;
// stuff happens
if (someTest(i));
i = 0;
Console.Write("now i is assigned for sure")
For value types, the variable is always assigned. There is a value there of some kind. Even so, if you try to read the variable before it is assigned the compiler will tell you and show an error: your code will not compile.
In case of you need to know given field set or not, you can have Property to control that field. in the setter you can control it, for example in below example isSet Boolean flag is updated when the value set. if you need to reset the flag based on another value you can add another condition in the setter.
private int i;
private bool isSet;
public int IProp
{
get { return i;}
set { isSet =true; i=value; }
}
// test
Console.WriteLine("Is Set:" + isSet);
IProp = 0;
Console.WriteLine("Is Set:" + isSet);
//results
//Is Set:False
//Is Set:True
I am new to C#.
What I wanted to do is to increase the lvl everytime my opponent's life turns to 0
I also want to make the maximum lvl up to 5.
public int displayMenu()
{
int pLvl = 1;
if (opponent.strength == 0)
{
(pLvl++);
a.Write("You are on level: {0}", pLvl);
}
}
Remove the parentheses around pLvl++;
But you're using a local variable. So everytime this method will be called the value will be set back to it's initial value 1. You could use a field instead.
The parantheses are usually used for casting or to wrap conditional expressions.
You can also use prefix increment shortand:
public int displayMenu()
{
int pLvl = 1;
if (opponent.strength == 0)
{
a.Write("You are on level: {0}", ++pLvl);
}
}
It will increase pLvl by one, and then output your message with incremented value.
Note that after method is executed, pLvl value is lost, since it is a local variable. Perhaps, it is not what you actually want.
In my project i'm using enums example:
public enum NcStepType { Start = 1, Stop = 3, Normal = 2 }
i'm reading values from a database, but sometimes there are 0-values in my record, so i want an enum that looks like
public enum NcStepType { Start = 1 OR 0, Stop = 3, Normal = 2 }
is this possible (in c#) ?
You could create a generic extension method that handles unknown values:
public static T ToEnum<T>(this int value, T defaultValue)
{
if (Enum.IsDefined(typeof (T),value))
return (T) (object) value;
else
return defaultValue;
}
Then you can simply call:
int value = ...; // value to be read
NcStepType stepType = value.ToEnum(NcStepType.Start);
// if value is defined in the enum, the corresponding enum value will be returned
// if value is not found, the default is returned (NcStepType.Start)
No, basically. You would have to give it one of the values (presumably the 1), and interpret the other (0) manually.
No it is not, and I'm not sure how it would work in practice.
Can't you just add logic that maps 0 to 1 when reading from the DB?
Normally i define in such cases the 0 as follows:
public enum NcStepType
{
NotDefined = 0,
Start = 1,
Normal = 2,
Stop = 3,
}
And somewhere in code i would make an:
if(Step == NcStepType.NotDefined)
{
Step = NcStepType.Start;
}
This makes the code readable and everyone knows what happens... (hopefully)
No, in C# an enum can have only one value.
There's nothing that says the value in the database must map directly to your enum value however. You could very easily assign a value of Start whenever you read 0 or 1 from the database.
public enum NcStepType { Start = 1 | 0, Stop = 3, Normal = 2 }
No solution in C#. But you can take 2 steps:
1. Set default value of your DB field to 1.
2. Update all existing 0 to 1.
As far as I know you can write this
enum NcStepType { Start = 0, Start = 1, Stop = 3, Normal = 2 }
The only problem is later there would be no way telling which Start was used for variable initialization (it would always look like it was the Start = 0 one).