Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
2.14/5 (5 votes)
See more:
May be I had to much drinks last night. Why this method does not increment the value of "number" by 1 every time I receive the value "1" in "getTurns"?


class Turns
    {
        private int number;

        public void Counts(int getTurn, out int getBack)
        {
            if (getTurn.Equals(1))
            {
                number++;
            }
           
            getBack = number;

        }
    }
Posted
Comments
Richard MacCutchan 26-May-13 12:04pm    
It works fine for me; show how you are calling it.
Sergey Alexandrovich Kryukov 26-May-13 12:07pm    
Not
if (getTurn.Equals(1)) {...}
but
if (getTurn == 1) ...!

And there is need to "out" as you can do return number.

If you are going to write is such inaccurate way, feeling drunk from time to time will be guaranteed!

—SA
Millone 26-May-13 12:36pm    
@ Sergey. I asked for support and it was not my intention to confront, but because you are asking, I reply. I am using out because I need to add other parameters and "return" would not allow me to send back more than one variable. I could return an array but "out" is fine enough to send back 2 variables. Concerning "Equals" does exactly the same of "if" in this case, so I do not see the point. I have sorted out the problem that was of a completely different nature.
Sergey Alexandrovich Kryukov 26-May-13 13:36pm    
What confront?! You are getting an advice, because you really need it, and you are talking about confrontation. You are not trying to be constructive. No, you can use return as well, only it would be more neat. Equals are not exactly the same: it gives the same result with lower performance.

Look, you are lost, you are very much lost. You really need help, but we hardly can provide you this kind of help. You simply have to forget all you "know" about programming and start from the very beginning. Take a manual and read, doing simple exercises. Only in this way you get get out of the dead and you are in.

—SA
Manfred Rudolf Bihy 26-May-13 12:14pm    
Should work fine, except that I'm SA on this one. A completely other thing is making this method void. Why not just return the incremented value? There should be an example of your usage pattern to explain that.

@Millone, please, read comments!

Manfred R. Bihy is right that you should care about instance field initialization. Please, read these articles:
10.4.5 Variable initializers[^]
10.4.5.1 Static field initialization[^]
10.4.5.2 Instance field initialization[^]
Instance Constructors (C# Programming Guide)[^]
Static Constructors (C# Programming Guide)[^]
Static Classes and Static Class Members (C# Programming Guide)[^]
Access Modifiers (C# Programming Guide)[^]

Millone wrote:
The problem was that I was basically calling the Count method from two different instances of the Turns class. When number was an instance field, it would start off at 0 for each instance. However, when I changed it to static, it would start off at 0 for the class as a whole and would be incremented by 1 as each instance called the Count method.


Probably, you don't need to call another instance of your class, but you don't know how to access to existing one...
3.7 Scopes (C#)[^]

I would suggest you to read about delegates[^] and interfaces[^].
Using delegates or/and interfaces you'll be able to increase Counts value as many times as you wish, using only the one instance of your class.

Bad practice is always bad practice, no matter of result (working application)!
 
Share this answer
 
v3
Comments
Ian A Davidson 26-May-13 17:24pm    
While some of what you have here is generally good, this is not addressing the question at all. We don't know the rest of the code, or what he is trying to do.
Maciej Los 26-May-13 17:44pm    
Thank you for your comment, but i can't agree with you at all (except the first part of your comment: "generally good"). When OP will read linked articles, he'll understand his mistake.

1 star for "generally good" answer? Is it justifiable?

;)
Ian A Davidson 26-May-13 18:03pm    
Sorry you disagree. I was being polite. Some of the advice given would be good if the question was different. (Although, why you would bother with "number" at all in this case I'm not sure; I'd go with "public int Count { get; set; }" ) However, as the question stands, there is nothing here to help. Hence 1 star.
Ian.
Maciej Los 26-May-13 19:31pm    
;(
How does this even complile? The variable number of type int has never been initialized.
I stand humbled and corrected by the comments below, but I still think that uninitialized value and to some lesser degree also reference types in class members are an absolute no go. I always assign a value even if it may seem a bit verbose, not even the beginner is left guessing what is actually going on.

My two cents!

Regards,
Manfred
 
Share this answer
 
v2
Comments
Ian A Davidson 26-May-13 12:35pm    
What do you mean? I can't see anything wrong with this.
Ian.
Millone 26-May-13 12:46pm    
@Manfred, it did!
Sergey Alexandrovich Kryukov 26-May-13 13:39pm    
Manfred, sorry, but you did one mistake: this is not a variable, this is a class member, it does not require initializer, because it is initialized by default. Remember the syntax (or just try it out).

I would advise you to remove this incorrect post.
—SA
Manfred Rudolf Bihy 26-May-13 14:31pm    
Thanks for pointing that out SA, I'll amend my post.
Ian A Davidson 26-May-13 16:24pm    
Manfred, your comment is still incorrect. In C# (or any other .NET language, i.e. if you're using the CLR), initialising a field (either static or instance field) of a class to its default value is redundant (except in some rare cases), and so you're just adding a performance hit to your application (admittedly probably only a problem if you are creating thousands of those objects, but it is good practice not to do it). Read here: http://msdn.microsoft.com/en-US/library/ms182274(v=vs.80).aspx
Regards,
Ian.
try this
C#
class Turns
{
public object Counts(int count)
   { 
	if (count == 1) 
        {
	   count += 1;
	}
	return count;
   }
}
 
Share this answer
 
v2
Comments
Bojjaiah 27-May-13 1:01am    
Hi basmeh,
void type doesn't have return type?
I changed the "private int number" to "static int number" and its working fine.
The problem was that I was basically calling the Count method from two different instances of the Turns class. When number was an instance field, it would start off at 0 for each instance. However, when I changed it to static, it would start off at 0 for the class as a whole and would be incremented by 1 as each instance called the Count method.

class Turns
    {
        static int number;
 
        public void Counts(int getTurn, out int getBack)
        {
            if (getTurn.Equals(1))
            {
                number++;
            }
           
            getBack = number;
 
        }
    }
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 26-May-13 13:37pm    
Gibberish. It would work with instance-field "number" as well, only would depend on the instance.
You have no clue on how things work. See my comments to the question. Sorry, really sorry...
—SA
Ian A Davidson 26-May-13 17:27pm    
I agree with SA to an extent, except that I don't think we know enough about the rest of your code. My gut feel, however, is that your object model is wrong and needs some rework.
Regards,
Ian.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900