Thread: The name ___ does not exist...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    The name ___ does not exist...

    Hi. I'm nearly as new to C# as I am to this forum. Anyway, errors regarding names not existing in the current context arise when the following code is used:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace InvoiceTotal
    {
           public partial class frmInvoiceTotal : Form
           {
    	  public frmInvoiceTotal()
    	  {
    		InitializeComponent();
    	  }
    
              int numberOfInvoices = 0;
              decimal totalOfInvoices = 0m;
              decimal invoiceAverage = 0m;
    
              private void btnCalculate_Click(object sender, EventArgs e)
    	  {
    	      decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
                  decimal discountPercent = .25m;
                  decimal discountAmount = Math.Round(subtotal * discountPercent, 2);
                  decimal invoiceTotal = subtotal - discountAmount;
    
                  txtSubtotal.Text = subtotal.ToString("c");
                  txtDiscountPercent.Text = discountPercent.ToString("p1");
                  txtDiscountAmount.Text = discountAmount.ToString("c");
                  txtTotal.Text = invoiceTotal.ToString("c");
    
                  numberOfInvoices++;
                  totalOfInvoices += invoiceTotal;
                  invoiceAverage = totalOfInvoices / numberOfInvoices;
      
                  txtNumberOfInvoices.Text = numberOfInvoices.ToString();  //red line
                  txtTotalOfInvoices.Text = totalOfInvoices.ToString("c"); //red line
                  txtDiscountAmount.Text = invoiceAverage.ToString("c");  //no red underline for this line
    
                  txtEnterSubtotal.Text = invoiceAverage.ToString("c");  //red line
    	      txtSubtotal.Focus();
    	  }
    
              private void btnClearTotals_Click(object sender, System.EventArgs e)
              {
                  numberOfInvoices = 0;
                  totalOfInvoices = 0m;
                  invoiceAverage = 0m;
    
                  txtNumberOfInvoices.Text = "";  //red line
                  txtTotalOfInvoices.Text = "";   //red line
                  txtInvoiceAverage.Text = "";    //red line
    
                  txtEnterSubtotal.Focus();       //red line
              }
    
              private void btnExit_Click(object sender, EventArgs e)
              {
                  this.Close();
              }
    
           }
    }
    For all the lines marked with a red line comment, the error arises. I'd like to know why txtDiscountAmount.Text is recognized, but let's say txtNumberOfInvoices.Text isn't.

    Thanks for reading.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    this is partial class

    form member definition is located in another cs file generated by form designer. check it to see which txt member variables are available and what are their names
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    2
    I actually figured that out, completely forgetting about the form designer. Thanks anyways.

    But a new problem emerged. After adding the missing txt variables, warnings came about. They stated

    Code:
    "member variable Is never assigned to and will always have its default value of null"
    Anyone know what's going on here?

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Are you setting the new variables to null? You should always set your variables to null or some value prior to using them in C#. You are using one of the new variables without first assigning it a value which in essence could cause an error later if the compiler wasn't so nice as to assign a default value to it for you.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Warnings like that should be taken very seriously.

    Im going to go out on a limb and guess that you want
    txtNumberOfInvoices
    and all the other red lines to be some sort of control, textbox or label or something? You probably want to add those to the form using the designer, you can name them by changing the Name parameter in the Properties dialog for the control.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. When does an action exist?
    By Yarin in forum General Discussions
    Replies: 18
    Last Post: 10-18-2009, 01:50 PM
  2. Check if a directory exist?
    By kevinawad in forum C++ Programming
    Replies: 12
    Last Post: 10-26-2008, 02:42 AM
  3. Compiler error in whitespace that doesn't even exist
    By beanroaster in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2005, 10:27 PM
  4. hwo to check if a file exist in certain dir?
    By Jasonymk in forum C++ Programming
    Replies: 4
    Last Post: 03-02-2003, 08:20 PM
  5. How to check a directory is exist or not?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 03-23-2002, 10:13 AM