Type /Namespace error in code
-
I have code for a calculator, and continue to get an error that I cannot seem to find. The error: The type or namespace name 'ArrayList' could not be found(are you missing a using directive or an assembly reference) I have partial classes, delaring the following in the Calculator.Designer.cs private string m_value; private ArrayList m_store; the rest of the code below is in the Calculator class: Can someone see what might be missing? Here is the code: public partial class Calculator : Form { public Calculator() { InitializeComponent(); m_value = ""; m_store = new ArrayList(); } private void btnSeven_Click(object sender, EventArgs e) { AddToArray(btnSeven); } private void btnEight_Click(object sender, EventArgs e) { AddToArray(btnEight); } private void btnNine_Click(object sender, EventArgs e) { AddToArray(btnNine); } private void btnFour_Click(object sender, EventArgs e) { AddToArray(btnFour); } private void btnFive_Click(object sender, EventArgs e) { AddToArray(btnFive); } private void btnSix_Click(object sender, EventArgs e) { AddToArray(btnSix); } private void btnOne_Click(object sender, EventArgs e) { AddToArray(btnOne); } private void btnTwo_Click(object sender, EventArgs e) { AddToArray(btnTwo); } private void btnThree_Click(object sender, EventArgs e) { AddToArray(btnThree); } private void btnZero_Click(object sender, EventArgs e) { AddToArray(btnZero); } //create method for array to calculate private void AddToArray(Button btn) { //m_value this to set the value m_value += btn.Text; //set label lblResult = " to display it to user lblResult.Text += btn.Text; //make all enabled to use it SetEnableOperatorBtns(true); } private void btnAdd_Click(object sender, EventArgs e) { AddOperatorToArray(btnAdd); } private void btnSubtract_Click(object sender, EventArgs e) { AddOperatorToArray(btnSubtract);
-
I have code for a calculator, and continue to get an error that I cannot seem to find. The error: The type or namespace name 'ArrayList' could not be found(are you missing a using directive or an assembly reference) I have partial classes, delaring the following in the Calculator.Designer.cs private string m_value; private ArrayList m_store; the rest of the code below is in the Calculator class: Can someone see what might be missing? Here is the code: public partial class Calculator : Form { public Calculator() { InitializeComponent(); m_value = ""; m_store = new ArrayList(); } private void btnSeven_Click(object sender, EventArgs e) { AddToArray(btnSeven); } private void btnEight_Click(object sender, EventArgs e) { AddToArray(btnEight); } private void btnNine_Click(object sender, EventArgs e) { AddToArray(btnNine); } private void btnFour_Click(object sender, EventArgs e) { AddToArray(btnFour); } private void btnFive_Click(object sender, EventArgs e) { AddToArray(btnFive); } private void btnSix_Click(object sender, EventArgs e) { AddToArray(btnSix); } private void btnOne_Click(object sender, EventArgs e) { AddToArray(btnOne); } private void btnTwo_Click(object sender, EventArgs e) { AddToArray(btnTwo); } private void btnThree_Click(object sender, EventArgs e) { AddToArray(btnThree); } private void btnZero_Click(object sender, EventArgs e) { AddToArray(btnZero); } //create method for array to calculate private void AddToArray(Button btn) { //m_value this to set the value m_value += btn.Text; //set label lblResult = " to display it to user lblResult.Text += btn.Text; //make all enabled to use it SetEnableOperatorBtns(true); } private void btnAdd_Click(object sender, EventArgs e) { AddOperatorToArray(btnAdd); } private void btnSubtract_Click(object sender, EventArgs e) { AddOperatorToArray(btnSubtract);
You forgot to include
using System.Collections
?Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
I have code for a calculator, and continue to get an error that I cannot seem to find. The error: The type or namespace name 'ArrayList' could not be found(are you missing a using directive or an assembly reference) I have partial classes, delaring the following in the Calculator.Designer.cs private string m_value; private ArrayList m_store; the rest of the code below is in the Calculator class: Can someone see what might be missing? Here is the code: public partial class Calculator : Form { public Calculator() { InitializeComponent(); m_value = ""; m_store = new ArrayList(); } private void btnSeven_Click(object sender, EventArgs e) { AddToArray(btnSeven); } private void btnEight_Click(object sender, EventArgs e) { AddToArray(btnEight); } private void btnNine_Click(object sender, EventArgs e) { AddToArray(btnNine); } private void btnFour_Click(object sender, EventArgs e) { AddToArray(btnFour); } private void btnFive_Click(object sender, EventArgs e) { AddToArray(btnFive); } private void btnSix_Click(object sender, EventArgs e) { AddToArray(btnSix); } private void btnOne_Click(object sender, EventArgs e) { AddToArray(btnOne); } private void btnTwo_Click(object sender, EventArgs e) { AddToArray(btnTwo); } private void btnThree_Click(object sender, EventArgs e) { AddToArray(btnThree); } private void btnZero_Click(object sender, EventArgs e) { AddToArray(btnZero); } //create method for array to calculate private void AddToArray(Button btn) { //m_value this to set the value m_value += btn.Text; //set label lblResult = " to display it to user lblResult.Text += btn.Text; //make all enabled to use it SetEnableOperatorBtns(true); } private void btnAdd_Click(object sender, EventArgs e) { AddOperatorToArray(btnAdd); } private void btnSubtract_Click(object sender, EventArgs e) { AddOperatorToArray(btnSubtract);
Hi, some remarks: 1. the ArrayList class you probably want is located in namespace System.Collections, hence it needs a
using System.Collections;
statement in every file that wants to use it. This is different fromusing System.Collections.Generic;
2. you should no longer use ArrayList, instead use List<T> which is a generic list holding objects of type T only (your choice of T!); it is supported since .NET 2.0 The generic collections require ausing System.Collections.Generic;
which you get for free when using Visual Studio. 3. your ArrayList is appearing in a Calculator.Designer.cs file? I have never seen the Designer use ArrayList like that, and you should not edit designer generated code at all. Put your code in Calculator.cs file, in the constructor, the Load handler, whatever. :)Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in
-
Hi, some remarks: 1. the ArrayList class you probably want is located in namespace System.Collections, hence it needs a
using System.Collections;
statement in every file that wants to use it. This is different fromusing System.Collections.Generic;
2. you should no longer use ArrayList, instead use List<T> which is a generic list holding objects of type T only (your choice of T!); it is supported since .NET 2.0 The generic collections require ausing System.Collections.Generic;
which you get for free when using Visual Studio. 3. your ArrayList is appearing in a Calculator.Designer.cs file? I have never seen the Designer use ArrayList like that, and you should not edit designer generated code at all. Put your code in Calculator.cs file, in the constructor, the Load handler, whatever. :)Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in
The only code I had in the Designer.cs file was: private string m_value; private ArrayList mstore; The rest of the code is in the is in the Calculator.cs file. So now I have made the change: private string m_value; private List; m_store; // moving the declarations to the cs. file public Calculator() { InitializeComponent(); m_value = ""; m_store = new List(); } When I remove the values above, the private calls, I get even more errors. Is using partial classes possibly causing my errors? The code was original in a publ class.
-
The only code I had in the Designer.cs file was: private string m_value; private ArrayList mstore; The rest of the code is in the is in the Calculator.cs file. So now I have made the change: private string m_value; private List; m_store; // moving the declarations to the cs. file public Calculator() { InitializeComponent(); m_value = ""; m_store = new List(); } When I remove the values above, the private calls, I get even more errors. Is using partial classes possibly causing my errors? The code was original in a publ class.
Hi, the variable declarations that used to be inside Calculator class in Calculator.Designer.cs file need to be inside Calculator class in Calculator.cs file. And that file needs all the import statements that lead the compiler to recognize the classes you are using (such as System.Collections.Generic for generic lists). If you plan on accessing m_value and m_store from outside the Calculator class, you would need to change their attribute from private to public, or better yet provide public methods or properties. I recommend you buy and study a tutorial book on C#, that would guide you step-by-step and teach you good practices from the start. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in
-
You forgot to include
using System.Collections
?Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro