4 Replies - 1040 Views - Last Post: 27 June 2014 - 05:05 AM Rate Topic: -----

#1 sheketo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-April 14

why converted values from string to double gives me syntax error

Posted 24 June 2014 - 06:22 AM

hi
I am using asp.net to design my final year project, i use four dropdownlists and convert the value to double at each dropdownlist, and at the end I use all these values to calculate as shown in the attached codes. But it does not work well. please assist why not working?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Solar_Designing
{
    public partial class Wire_Size : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            double  distance;
            double current;
            double voltage;
            double Loss;
            if (DropDownList1.SelectedItem.Text == "12 Volts")
            {
                voltage = 12;
            }
            else if (DropDownList1.SelectedItem.Text == "24 Volts")
            {
                voltage = 24;
            }
            else if (DropDownList1.SelectedItem.Text == "48 Volts")
            {
                voltage = 48;
            }



            if (DropDownList2.SelectedItem.Text == "5 Amps")
            {
                current = 5;
            }
            else if (DropDownList2.SelectedItem.Text == "10 Amps")
            {
                current = 10;
            }
            else if (DropDownList2.SelectedItem.Text == "15 Amps")
            {
                current = 15;
            }


            if (DropDownList3.SelectedItem.Text == "5 Feet")
            {
                distance = 5;
            }
            else if (DropDownList3.SelectedItem.Text == "10 Feet")
            {
                distance = 10;
            }
            else if (DropDownList3.SelectedItem.Text == "15 Feet")
            {
                distance = 15;
            }


            if (DropDownList4.SelectedItem.Text == "2% Loss")
            {
                Loss = 2/100;
            }
            else if (DropDownList4.SelectedItem.Text == "3% Loss")
            {
                Loss = 3/100;
            }
            else if (DropDownList4.SelectedItem.Text == "4% Loss")
            {
                Loss = 4/100;
            }
            else if (DropDownList4.SelectedItem.Text == "5% Loss")
            {
                Loss = 5/100;
            }
            double wire ;
            int k = Convert.ToInt32(12.9);
            int m = Convert.ToInt32(2);
            double volt = Convert.ToDouble(voltage);
            double curr = Convert.ToDouble(current);
            double loss = Convert.ToDouble(Loss);
            double dis = Convert.ToDouble(distance);
             double wire = Convert.ToDouble((dis * curr * k) / (volt * loss));
              TextBox1.Text = Convert.ToString(wire);
        }
    }
}

This post has been edited by modi123_1: 24 June 2014 - 06:45 AM
Reason for edit:: use the 'code' button in the editor next time!


Is This A Good Question/Topic? 0
  • +

Replies To: why converted values from string to double gives me syntax error

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9057
  • View blog
  • Posts: 34,001
  • Joined: 12-June 08

Re: why converted values from string to double gives me syntax error

Posted 24 June 2014 - 06:46 AM

Quote

But it does not work well.

Explain this.
Was This Post Helpful? 0
  • +
  • -

#3 sheketo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-April 14

Re: why converted values from string to double gives me syntax error

Posted 24 June 2014 - 11:16 AM

in this part of the code (double volt = Convert.ToDouble(voltage);
double curr = Convert.ToDouble(current);
double loss = Convert.ToDouble(Loss);
double dis = Convert.ToDouble(distance);), the voltage, current, Loss and distance were underline with red lines. When i placed the corsor on it, the error message said "use of unassigned variable 'voltage' and so on. when I tried to run it, the error message "THERE WERE ERRORS BUILDING THE PROJECT".

thanks
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9057
  • View blog
  • Posts: 34,001
  • Joined: 12-June 08

Re: why converted values from string to double gives me syntax error

Posted 24 June 2014 - 11:26 AM

My guess if if you put a breakpoint at, about, line 86 you'll see your conditions set up (the if/elses) has a big ol' gaping hole where no condition is satisfied and the variables are never given a value.

22	            double voltage;

24	            if (DropDownList1.SelectedItem.Text == "12 Volts") '-- ** condition 
25	            {
26	                voltage = 12;
27	            }
28	            else if (DropDownList1.SelectedItem.Text == "24 Volts")'-- ** condition 
29	            {
30	                voltage = 24;
31	            }
32	            else if (DropDownList1.SelectedItem.Text == "48 Volts")'-- ** condition 
33	            {
34	                voltage = 48;
35	            }
'-- ** condition !? Where's the regular "else" to assure a value is being set?  How about just a default value for the variable when it is declared?

Was This Post Helpful? 0
  • +
  • -

#5 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 391
  • Joined: 04-January 11

Re: why converted values from string to double gives me syntax error

Posted 27 June 2014 - 05:05 AM

Just as modi123_1 said, assign a default value during declaration.


double  distance = 0;
double current = 0;
double voltage = 0;
double Loss = 0;




It doesn't have to be always zero. Consider the most appropriate default value to assign.

This post has been edited by November-06: 27 June 2014 - 05:07 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1