7 Replies - 418 Views - Last Post: 18 December 2012 - 04:12 PM Rate Topic: -----

#1 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Field is never assigned? causing problem?

Posted 18 December 2012 - 02:16 PM

hi all i spent ages looking at this code trying to work out why when i debug it line for line the values just seem to reset? or never hold their values.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace coffeemakingapp
{
    public class coffeemaking
    {
        double COC;//double COC; //Cups of coffee
       double CostOC; // cost of coffee
        double total;
        public coffeemaking(int COC, double CostOC )
        {
            
        }

        public coffeemaking()
        {
           
            double COC = 10;
            double CostOC = 0.50;
            
            
        }

        public void Menu()
    {
        Console.WriteLine(" Amount of Coffee: {0}", COC);
        Console.WriteLine(" Price of Coffee: {0:C}", CostOC);

    }

        public void Insert(int pound, int fifty, int twenty, int ten, int five)
        {
            double poundtotal = ((pound) / 100);
            double fiftytotal = ((fifty) / 100);
            double twentytotal = ((twenty) / 100);
            double tentotal = ((ten) / 100);
            double fivetotal = ((five) / 100);

          double Total = (poundtotal + fiftytotal + twentytotal + tentotal + fivetotal);  
            
        }

    public void Select()
{
    if (total == CostOC)
        {
        Console.WriteLine(" Here is your Coffee ");

        }
    else
        {
            if (total < CostOC)
            {
                Console.WriteLine(" You Entered {0:C} a coffee is {1:C} , Please Enter More Money", total, CostOC);
            }
            else if (total > CostOC)
            {
                Console.WriteLine(" This machine only accepts exact change");
            }
        }


    }

    public void Refund()
{
    Console.WriteLine(" Here Is your Change {0:C} ", total);
    }

    public static void Main()
    
    {
        coffeemaking newcoffee = new coffeemaking();

        Console.WriteLine(" Best Coffee Machine of 2012");

        newcoffee.Menu();

        //insert money

        Console.WriteLine(" Please Enter Money: ");

        int pound = IO.GetInt("Pound: ");
        int fifty = IO.GetInt("Fifty Pence: ");
        int twenty = IO.GetInt("Twenty Pence: ");
        int ten = IO.GetInt("Ten Pence: ");
        int five = IO.GetInt("Five Pence: ");

        newcoffee.Insert(pound, fifty, twenty, ten, five);

       

      newcoffee.Select();

        newcoffee.Refund();

        Console.ReadLine();

    }
    }
}



my problem is it creates a newcoffee and sets the values and then when it leaves the class they dont have the value and causing me erros please can i get some advice. thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Field is never assigned? causing problem?

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Field is never assigned? causing problem?

Posted 18 December 2012 - 02:34 PM

Hi. With this code:

public coffeemaking()
{
  double COC = 10;
  double CostOC = 0.50;        
}



you are declaring two brand new variables of which are totally unrelated to the instance fields you have defined earlier (apart from the fact that they share the same names). Those new variables are local to coffeemaking().

Therefore, you aren't setting your instance fields, you are setting those two new local variables.

To set the fields, you need:

public coffeemaking()
{
  COC = 10;
  CostOC = 0.50;        
}



The omission of double in that code means that I am no longer declaring new variables, and assigning them values, but I am assigning values to the instance variables that were declared earlier.

You may find it helpful to read this page on scope :)

EDIT: Here is the part of that linked page (modified to remove extraneous elements) that is especially relevant to this scenario:

Spoiler

This post has been edited by CodingSup3rnatur@l-360: 18 December 2012 - 02:52 PM

Was This Post Helpful? 2
  • +
  • -

#3 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: Field is never assigned? causing problem?

Posted 18 December 2012 - 02:52 PM

thanks and yes i realised i had to create a new "coffeemachine" with initial amounts. i just tried to change the topic and question of this lol here is my code now

namespace coffeemakingapp
{
    public class coffeemaking
    {
        public double COC;//double COC; //Cups of coffee
       public double CostOC; // cost of coffee
        public double total;
        double poundtotal;
        double fiftytotal;

        public coffeemaking()
        {
           
            double COC = 10;
            double CostOC = 0.50;
            
            
        }
        public coffeemaking(double COCamount, double CostOCamount)
        {
            COC = COCamount;
            CostOC = CostOCamount;
        }

        public void Menu()
    {
        Console.WriteLine(" Amount of Coffee: {0}", COC);
        Console.WriteLine(" Price of Coffee: {0:C}", CostOC);

    }

        public void Insert(int pound, int fifty, int twenty, int ten, int five)
        {
            int fraction = 100;
            double poundtotal = (pound / fraction);
            double fiftytotal = ((fifty) / 100);
            double twentytotal = ((twenty) / 100);
            double tentotal = ((ten) / 100);
            double fivetotal = ((five) / 100);

          double Total = (poundtotal + fiftytotal + twentytotal + tentotal + fivetotal);  
            
        }

    public void Select()
{
    if (total == CostOC)
        {
        Console.WriteLine(" Here is your Coffee ");

        }
    else
        {
            if (total < CostOC)
            {
                Console.WriteLine(" You Entered {0:C} a coffee is {1:C} , Please Enter More Money", total, CostOC);
            }
            else if (total > CostOC)
            {
                Console.WriteLine(" This machine only accepts exact change");
            }
        }


    }

    public void Refund()
{
    Console.WriteLine(" Here Is your Change {0:C} ", total);
    }

    public static void Main()
    
    {
        
        coffeemaking newcoffee = new coffeemaking(10,0.50);

        Console.WriteLine(" Best Coffee Machine of 2012");

        newcoffee.Menu();

        //insert money

        Console.WriteLine(" Please Enter Money: ");

        int pound = IO.GetInt("Pound: ");
        int fifty = IO.GetInt("Fifty Pence: ");
        int twenty = IO.GetInt("Twenty Pence: ");
        int ten = IO.GetInt("Ten Pence: ");
        int five = IO.GetInt("Five Pence: ");

        newcoffee.Insert(pound, fifty, twenty, ten, five);

       

      newcoffee.Select();

        newcoffee.Refund();

        Console.ReadLine();

    }
    }
}





i get to the public void insert section and i basically need to turn the user input of integer into a decimal value but for some reason when i use the

double poundtotal = (pound / fraction);
            double fiftytotal = ((fifty) / 100);



none of these methods turn them into decimals and the values stay the same?
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Field is never assigned? causing problem?

Posted 18 December 2012 - 03:06 PM

Well, one problem is that you're doing integer division. Example:

double poundtotal = (pound / fraction)


pound and fraction are both defined as integers. Therefore, the result will be an integer. Change fraction to a double and you'll get fractional division.

Also, you still have the same initial problem in your default constructor.
Was This Post Helpful? 2
  • +
  • -

#5 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: Field is never assigned? causing problem?

Posted 18 December 2012 - 03:53 PM

ahh thanks i get it now :) and thanks again ye i get it so aslong as its declared in the program you dont have to keep declaring them?

sorry to be a pain :)
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Field is never assigned? causing problem?

Posted 18 December 2012 - 03:58 PM

Quote

so aslong as its declared in the program you dont have to keep declaring them?


I have no idea what you're asking here. Please clarify.
Was This Post Helpful? 0
  • +
  • -

#7 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: Field is never assigned? causing problem?

Posted 18 December 2012 - 04:09 PM

im just confirming then , as long as you declare variables once you dont have to keep declaring them down the line?
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Field is never assigned? causing problem?

Posted 18 December 2012 - 04:12 PM

Please read the tutorial that was already linked earlier, it discusses a concept called "scope", and is an important thing to understand.

http://msdn.microsof...v=vs.71%29.aspx

Basically, if you've created a variable at the class-level (more properly called a field) then it's accessible anywhere inside the class. If you create a variable inside a function, it only exists while you're in that function, and is not accessible anywhere outside of the function or in any other functions.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1