Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I keep getting the output in messagebox as '0' for any numerical value I give in the textBox1. can you please fix my code ? Thanks

C#
public partial class MainWindow : Window
{
    public double[] average = new double[5];
}
private void button1_Click(object sender, RoutedEventArgs e)
{
    average[0] = Convert.ToDouble(textBox1.Text);
    MessageBox.Show(Convert.ToString(average[0]));
}


PS: I have also tried this.textBox1.Text, but it did not work. Could you please also explain me what 'this' keyword exactly does ? Thanks


Thanks everyone for the reply, but My objective here is not to display the value in the textbox, but to store it in an array. I am planning to store a new value in the double array everytime i click the button. I want the code to be something like this.
private button_click()
{
int i=0;
code to store the current value in the textbox into a double array[i] and probably display it on a messagebox
....
i++;
textbox1.clear();
}
Posted
Updated 9-Sep-11 1:33am
v5
Comments
J.Karthick 9-Sep-11 6:38am    
How come you build your code??

I got error like "expected enum or struct"

Your button Click event should come inside Class...You declare it outside
steersteer 9-Sep-11 7:35am    
It is just a portion of my code, and no I did not define my button click method outside my mainwindow class.

what 'this' means: http://msdn.microsoft.com/en-us/library/dk1507sz(v=vs.80).aspx[^]

you better do
C#
double mycheckDouble = 0;
if (double.TryParse(textBox1.Text, out mycheckDouble)
   MessageBox.Show(string.Format("value found {0}", mycheckDouble));
else
   MessageBox.Show("Failed to convert value to a double");
 
Share this answer
 
v2
Comments
Simon Bang Terkildsen 9-Sep-11 6:34am    
+5 for TryParse the best way to do it, as you'll not get any exception when the input is incorrect.
Herman<T>.Instance 9-Sep-11 6:36am    
exactly. Try Catching is expensive in use and avoiding try catch but having the same result is always a big win.
Pravin Patil, Mumbai 9-Sep-11 6:44am    
My 5 tooo..... for the TryParse.
Most correct way to catch Data Type Level error.
steersteer 9-Sep-11 7:27am    
My objective here is not to display the value in the textbox, but to store it in an array. I am planning to store a new value in the double array everytime i click the button. I want the code to be something like this.
private button_click()
{
int i=0;
code to store the current value in the textbox into a double array[i]
....
i++;
textbox1.clear();
}
steersteer 9-Sep-11 7:27am    
Thanks for the reply btw.
Convert.ToDouble()[^] returns 0 when the input is null, an empty string or "0". So either you have more than one TextBox and using the wrong one or you do not change the value in your TextBox

the this is the reference to the current instance of your class. refer to the documentation this (C# Reference)[^]
 
Share this answer
 
v2
Comments
Herman<T>.Instance 9-Sep-11 6:35am    
Good link ;)
C#
public partial class MainWindow : Window
{
    public double[] average = new double[5];

    public static int i = 0;

  protected void AddToArray()
  {
      if(i != 5 && i < 5)
      {
          average[i] = Convert.TODouble(txtbox.text);
          i++;
      }
  }

}
// then call that mwthod to your button click you can write your code to stop clicking the button if it is at 5 times
 
Share this answer
 
Comments
steersteer 9-Sep-11 8:55am    
Thanks
I would try something like this:-

C#
List<double> doubles = new List<double>();//use list rather than array - much easier

       private void button1_Click(object sender, EventArgs e)
       {
           double result;
           double.TryParse(textBox1.Text, out result);
           if (result != null)
           {
               doubles.Add(result);
               textBox1.Clear();
           }
           else
           {
               MessageBox.Show("Please enter valid number.");
           }
       }


Hope this helps

In reply to your comment, it is certainly possible to store any Class in a List<class>, something like this:-

C#
class Car
   {
       public string Make { get; set; }
       public string Model { get; set; }
   }


XML
List<Car> cars = new List<Car>();
            cars.Add(new Car(){ Make = "Nissan", Model="Sunny"});


Here[^] is the MSDN page for List<t></t>

As for storing one List for both windows, the best thing would be to have a public property in your Main window which returns a List<double></double> and to pass this as a prameter to your second form. then you just add the second forms doubles directly to your List.
 
Share this answer
 
v2
Comments
steersteer 9-Sep-11 8:43am    
Thank you very much. Yea. Lists look much easier. Have not used lists before. Just one doubt. if i have two windows with one list in each, can you please tell me how to store all the elements in doubles(list in window1) in window2.doubles(list in window2) ? Can you please give me a link for a tutorial(video would be great) about using lists in visual C# ?. Something for a clumsy beginner please ? :) Thanks. Much appreciated.
steersteer 9-Sep-11 8:45am    
also, can i create a list of objects using List< <<CAR>> carname = new List <<CAR>>(); where car being the class name
I am wondering how is your code running, as you have written

C#
public double[] average = new int[5];


which should be like this:

C#
public double[] average = new double[5];


hope it helps :)
 
Share this answer
 
Comments
Herman<T>.Instance 9-Sep-11 6:51am    
sharp!
Uday P.Singh 9-Sep-11 7:03am    
what does it mean?
Herman<T>.Instance 9-Sep-11 7:17am    
that you see very sharp he is declaring an int array to a double arra, which will lead to errors.
Uday P.Singh 9-Sep-11 7:23am    
okay got it!
steersteer 9-Sep-11 7:18am    
my bad. I pasted the code i had before fixing that error.

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