locked
a variable assigned to itself ?

    Question

  • In a c# book for beginners ( like i'm) i found , in a code example , this method :

    public void CalculateCostOfDecorations(bool fancy)
    {
        fancyDecorations = fancy; // this statement sets a field via a checkBox
    ............................
    }

    Now , the set accessor of a private variabe , calls the method in this way :

    CalculateCostOfDecorations(fancyDecorations);

    so it seems that we will have :

    fancyDecorations = fancyDecorations; i.e. a variable assigned to itself.

    This is a bit strange , or i'm wrong ?
    Thanks a lot for any explanation

    Friday, August 28, 2009 12:29 PM

Answers

  • In a c# book for beginners ( like i'm) i found , in a code example , this method :

    public void CalculateCostOfDecorations(bool fancy)
    {
        fancyDecorations = fancy; // this statement sets a field via a checkBox
    ............................
    }

    Now , the set accessor of a private variabe , calls the method in this way :

    CalculateCostOfDecorations(fancyDecorations);

    so it seems that we will have :

    fancyDecorations = fancyDecorations; i.e. a variable assigned to itself.

    This is a bit strange , or i'm wrong ?
    Thanks a lot for any explanation



    I added a property to your 'complete' code snippet.

    /*========================================*/

     public class DinnerParty
        {

            private bool fancyDecorations;

            private int numberOfPeople;

            private Decimal _CostOfDecorations;

            public Decimal CostOfDecorations
            {
                get
                {
                    return _CostOfDecorations;
                }
                set
                {
                    _CostOfDecorations = value;
                }
            }


            public int NumberOfPeople
            {
                get
                {
                    return numberOfPeople;
                }
                set
                {
                    numberOfPeople = value;

                    CalculateCostOfDecorations(fancyDecorations);
                }
            }

            public void CalculateCostOfDecorations(bool fancy)
            {

                fancyDecorations = fancy;

                if (fancy)
                {
                    CostOfDecorations = (NumberOfPeople * 15.00M) + 50M;
                }

                else
                {
                    CostOfDecorations = (NumberOfPeople * 7.50M) + 30M;
                }
            }

        }

    /*========================================*/

    The variable names are similar, "fancy" and "fancyDecorations", but it is not assigning a variable to itself, per se.

    The method that it is calling is public.  Consumers of the class can call the method, as well as the class itself.  The method makes more sense when you consider that consumers can call the method with the parameter set as "true" or "false".  The method sets the internal variable depending upon what boolean value is passed into the method.

    Apparently, the class needs to call the method, too. 
    So instead of writing duplicate code, the developer used the same method. 
    Doing so avoids the chore of making sure that any changes are made wherever the duplicate code resides.
    Does that make sense?


    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by Jeff.Ferguson Saturday, August 29, 2009 7:34 PM
    • Marked as answer by Harry Zhu Wednesday, September 02, 2009 2:51 AM
    Friday, August 28, 2009 2:08 PM

All replies

  • Your code snippet seems incomplete, it is missing the variable declarations.

    I suspect that issue is one of variable scope, however. 
    There just be more variables involved than you have noticed thus far, variables with similar names.

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, August 28, 2009 1:04 PM
  • here is the remaining part : ( thanks for your attention ! )

    it'a at pag 204 of the ( good) book : HF C#

    public class DinnerParty { 

    private bool fancyDecorations;

    private int numberOfPeople;

    public int NumberOfPeople
    {
    get { return numberOfPeople; }
    set
    {numberOfPeople = value;

    CalculateCostOfDecorations(fancyDecorations);}
    }

    public void CalculateCostOfDecorations(bool fancy)
    {

    fancyDecorations = fancy;

    if (fancy) {CostOfDecorations = (NumberOfPeople * 15.00M) + 50M;}

     else {CostOfDecorations = (NumberOfPeople * 7.50M) + 30M;}}

    }

     

     


     

     

     

     

     

     

    }

     

     

     

    Friday, August 28, 2009 1:41 PM
  • In a c# book for beginners ( like i'm) i found , in a code example , this method :

    public void CalculateCostOfDecorations(bool fancy)
    {
        fancyDecorations = fancy; // this statement sets a field via a checkBox
    ............................
    }

    Now , the set accessor of a private variabe , calls the method in this way :

    CalculateCostOfDecorations(fancyDecorations);

    so it seems that we will have :

    fancyDecorations = fancyDecorations; i.e. a variable assigned to itself.

    This is a bit strange , or i'm wrong ?
    Thanks a lot for any explanation



    I added a property to your 'complete' code snippet.

    /*========================================*/

     public class DinnerParty
        {

            private bool fancyDecorations;

            private int numberOfPeople;

            private Decimal _CostOfDecorations;

            public Decimal CostOfDecorations
            {
                get
                {
                    return _CostOfDecorations;
                }
                set
                {
                    _CostOfDecorations = value;
                }
            }


            public int NumberOfPeople
            {
                get
                {
                    return numberOfPeople;
                }
                set
                {
                    numberOfPeople = value;

                    CalculateCostOfDecorations(fancyDecorations);
                }
            }

            public void CalculateCostOfDecorations(bool fancy)
            {

                fancyDecorations = fancy;

                if (fancy)
                {
                    CostOfDecorations = (NumberOfPeople * 15.00M) + 50M;
                }

                else
                {
                    CostOfDecorations = (NumberOfPeople * 7.50M) + 30M;
                }
            }

        }

    /*========================================*/

    The variable names are similar, "fancy" and "fancyDecorations", but it is not assigning a variable to itself, per se.

    The method that it is calling is public.  Consumers of the class can call the method, as well as the class itself.  The method makes more sense when you consider that consumers can call the method with the parameter set as "true" or "false".  The method sets the internal variable depending upon what boolean value is passed into the method.

    Apparently, the class needs to call the method, too. 
    So instead of writing duplicate code, the developer used the same method. 
    Doing so avoids the chore of making sure that any changes are made wherever the duplicate code resides.
    Does that make sense?


    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by Jeff.Ferguson Saturday, August 29, 2009 7:34 PM
    • Marked as answer by Harry Zhu Wednesday, September 02, 2009 2:51 AM
    Friday, August 28, 2009 2:08 PM
  • ah ,ok : thinking in terms of "true" or "false" make sense !
    thanks again
    Friday, August 28, 2009 2:40 PM