enum TempUnits
{
Kelvins = 0,
Celsius = 1,
Fahrenheit = 2
}
public double TempConverter(double InitialTemp, TempUnits ConvFrom, TempUnits ConvTo)
{
double Temp;
//First make sure they didnt enter same values for convert from & convert to
if (ConvFrom == ConvTo)
{
//Return initial value
Temp = InitialTemp;
}
else
{
try {
//Now decide what they want done
switch (ConvFrom) {
//Convert from Kelvins
case TempUnits.Kelvins:
//To Celsius
if (ConvTo == TempUnits.Celsius)
{
//Celsius = Kelvin - 273.15
Temp = InitialTemp - 273.15;
}
//To Fahrenheit
else if (ConvTo == TempUnits.Fahrenheit) {
Temp = (InitialTemp - 273.15) * 1.8 + 32.0;
}
break;
//Convert from Celsius
case TempUnits.Celsius:
//To Kelvins
if (ConvTo == TempUnits.Kelvins)
{
//Kelvin = Celsius + 273.15
Temp = InitialTemp + 273.15;
}
//To Fahrenheit
else if (ConvTo == TempUnits.Fahrenheit) {
//degree F = degree C x 1.8 + 32.
Temp = InitialTemp * 1.8 + 32.0;
}
break;
//Convert from Fahrenheit
case TempUnits.Fahrenheit:
//To Celsius
if (ConvTo == TempUnits.Celsius)
{
//degree C = (degree F - 32.) / 1.8
Temp = (InitialTemp - 32.0) / 1.8;
}
//To Kelvins
else if (ConvTo == TempUnits.Kelvins) {
Temp = ((InitialTemp - 32.0) / 1.8) + 273.15;
}
break;
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message, "Conversion Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Temp = 0;
}
}
return Temp;
}
I can't get this to run in a visual C# form
Page 1 of 112 Replies - 972 Views - Last Post: 18 January 2010 - 08:37 PM
#1
I can't get this to run in a visual C# form
Posted 14 January 2010 - 09:51 PM
Replies To: I can't get this to run in a visual C# form
#2
Re: I can't get this to run in a visual C# form
Posted 14 January 2010 - 09:56 PM
#3
Re: I can't get this to run in a visual C# form
Posted 14 January 2010 - 09:56 PM
#4
Re: I can't get this to run in a visual C# form
Posted 14 January 2010 - 10:04 PM
Quote
mean exactly??? Are there errors? Does your hard drive get formatted? Is the cupholder not retracting?
BE. SPECIFIC.
#5
Re: I can't get this to run in a visual C# form
Posted 16 January 2010 - 05:38 PM
#6
Re: I can't get this to run in a visual C# form
Posted 16 January 2010 - 05:42 PM
#7
Re: I can't get this to run in a visual C# form
Posted 16 January 2010 - 05:49 PM
JackOfAllTrades, on 14 Jan, 2010 - 09:04 PM, said:
Quote
mean exactly??? Are there errors? Does your hard drive get formatted? Is the cupholder not retracting?
BE. SPECIFIC.
I started with a blank form, added the code and then built a windows form with a textbox, a button and two lables to display the temp convertion but when I try to place the button_click object to display the convertion after inputing the temp, I build the solution and I get an error regarding a } that comes up wrong. When I remove it or move it I get even more errors!
SixOfEleven, on 16 Jan, 2010 - 04:42 PM, said:
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 project_6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{ Show (temp);
Show (iflable);
Show (switchlable);}
enum TempUnits
{
Kelvins = 0,
Celsius = 1,
Fahrenheit = 2
}
public double TempConverter(double InitialTemp, TempUnits ConvFrom, TempUnits ConvTo)
{
double Temp;
//First make sure they didnt enter same values for convert from & convert to
if (ConvFrom == ConvTo)
{
//Return initial value
Temp = InitialTemp;
}
else
{
try {
//Now decide what they want done
switch (ConvFrom) {
//Convert from Kelvins
case TempUnits.Kelvins:
//To Celsius
if (ConvTo == TempUnits.Celsius)
{
//Celsius = Kelvin - 273.15
Temp = InitialTemp - 273.15;
}
//To Fahrenheit
else if (ConvTo == TempUnits.Fahrenheit) {
Temp = (InitialTemp - 273.15) * 1.8 + 32.0;
}
break;
//Convert from Celsius
case TempUnits.Celsius:
//To Kelvins
if (ConvTo == TempUnits.Kelvins)
{
//Kelvin = Celsius + 273.15
Temp = InitialTemp + 273.15;
}
//To Fahrenheit
else if (ConvTo == TempUnits.Fahrenheit) {
//degree F = degree C x 1.8 + 32.
Temp = InitialTemp * 1.8 + 32.0;
}
break;
//Convert from Fahrenheit
case TempUnits.Fahrenheit:
//To Celsius
if (ConvTo == TempUnits.Celsius)
{
//degree C = (degree F - 32.) / 1.8
Temp = (InitialTemp - 32.0) / 1.8;
}
//To Kelvins
else if (ConvTo == TempUnits.Kelvins) {
Temp = ((InitialTemp - 32.0) / 1.8) + 273.15;
}
break;
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message, "Conversion Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Temp = 0;
}
}
return Temp;
}
}
}
}
this is the error I get
Error 1 Type or namespace definition, or end-of-file expected
This post has been edited by boosh: 16 January 2010 - 05:56 PM
#8
Re: I can't get this to run in a visual C# form
Posted 16 January 2010 - 06:02 PM
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 _0509_it254_section5_JoshuaBauer_Unit6_Project6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Show (temp);
Show (iflable);
Show (switchlable);
}
}
public enum TempUnits
{
Kelvins = 0,
Celsius = 1,
Fahrenheit = 2
}
public double TempConverter(double InitialTemp, TempUnits ConvFrom, TempUnits ConvTo)
{
double Temp = 0; //Initialize the variables, sometimes compilers don't like uninitialized variables.
//First make sure they didnt enter same values for convert from & convert to
if (ConvFrom == ConvTo)
{
//Return initial value
Temp = InitialTemp;
}
else
{
try {
//Now decide what they want done
switch (ConvFrom) {
//Convert from Kelvins
case TempUnits.Kelvins:
//To Celsius
if (ConvTo == TempUnits.Celsius)
{
//Celsius = Kelvin - 273.15
Temp = InitialTemp - 273.15;
}
//To Fahrenheit
else if (ConvTo == TempUnits.Fahrenheit)
{
Temp = (InitialTemp - 273.15) * 1.8 + 32.0;
}
//--------------------------------------------------------
break;
//Convert from Celsius
case TempUnits.Celsius:
//To Kelvins
if (ConvTo == TempUnits.Kelvins)
{
//Kelvin = Celsius + 273.15
Temp = InitialTemp + 273.15;
}
//To Fahrenheit
else if (ConvTo == TempUnits.Fahrenheit)
{
//degree F = degree C x 1.8 + 32.
Temp = InitialTemp * 1.8 + 32.0;
}
//--------------------------------------------------------
break;
//Convert from Fahrenheit
case TempUnits.Fahrenheit:
//To Celsius
if (ConvTo == TempUnits.Celsius)
{
//degree C = (degree F - 32.) / 1.8
Temp = (InitialTemp - 32.0) / 1.8;
}
//To Kelvins
else if (ConvTo == TempUnits.Kelvins)
{
Temp = ((InitialTemp - 32.0) / 1.8) + 273.15;
}
//--------------------------------------------------------
break;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Conversion Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Temp = 0;
}
}
return Temp;
}
}
May I suggest you use Visual C# or Notepad++ as these have braces highlighting so you know where a closing brace's opening brace is or if a brace doesn't belong there.
Hope I helped!
EDIT: Sorry, I forgot the breaks in the switch
This post has been edited by remorseless: 17 January 2010 - 09:00 PM
#9
Re: I can't get this to run in a visual C# form
Posted 16 January 2010 - 06:24 PM
remorseless, on 16 Jan, 2010 - 05:02 PM, said:
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 project_6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Show (temp);
Show (iflable);
Show (switchlable);
}
}
public enum TempUnits
{
Kelvins = 0,
Celsius = 1,
Fahrenheit = 2
}
public double TempConverter(double InitialTemp, TempUnits ConvFrom, TempUnits ConvTo)
{
double Temp = 0; //Initialize the variables, sometimes compilers don't like uninitialized variables.
//First make sure they didnt enter same values for convert from & convert to
if (ConvFrom == ConvTo)
{
//Return initial value
Temp = InitialTemp;
}
else
{
try {
//Now decide what they want done
switch (ConvFrom) {
//Convert from Kelvins
case TempUnits.Kelvins:
//To Celsius
if (ConvTo == TempUnits.Celsius)
{
//Celsius = Kelvin - 273.15
Temp = InitialTemp - 273.15;
}
//To Fahrenheit
else if (ConvTo == TempUnits.Fahrenheit)
{
Temp = (InitialTemp - 273.15) * 1.8 + 32.0;
}
//--------------------------------------------------------
//Convert from Celsius
case TempUnits.Celsius:
//To Kelvins
if (ConvTo == TempUnits.Kelvins)
{
//Kelvin = Celsius + 273.15
Temp = InitialTemp + 273.15;
}
//To Fahrenheit
else if (ConvTo == TempUnits.Fahrenheit)
{
//degree F = degree C x 1.8 + 32.
Temp = InitialTemp * 1.8 + 32.0;
}
//--------------------------------------------------------
//Convert from Fahrenheit
case TempUnits.Fahrenheit:
//To Celsius
if (ConvTo == TempUnits.Celsius)
{
//degree C = (degree F - 32.) / 1.8
Temp = (InitialTemp - 32.0) / 1.8;
}
//To Kelvins
else if (ConvTo == TempUnits.Kelvins)
{
Temp = ((InitialTemp - 32.0) / 1.8) + 273.15;
}
//--------------------------------------------------------
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Conversion Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Temp = 0;
}
}
return Temp;
}
}
May I suggest you use Visual C# or Notepad++ as these have braces highlighting so you know where a closing brace's opening brace is or if a brace doesn't belong there.
Hope I helped!
That did help thank you. I am still having issues. I think my prob is initializing my veriables like double, int and string. I am using Visual C#, is there any advice you can give me on understanding initializing my veriables?
#10
Re: I can't get this to run in a visual C# form
Posted 16 January 2010 - 06:56 PM
This post has been edited by boosh: 17 January 2010 - 05:58 PM
#12
Re: I can't get this to run in a visual C# form
Posted 17 January 2010 - 08:59 PM
If you do something like this:
double x; double y = x + 1;
You might think that x in this case should be 0 right? Wrong. It will give you the error because maybe you wanted x to equal 42 or 345.234, which is why it gives you that error. Now, if you set the value when you declare the variable, you won't run into these problems, sort of like this:
double x = 0; double y = x + 1;
The above code will compile perfectly.
And that is end the of the course "Uninitialized Variables 101".
Hope that clears things up!
Also, what problems are you getting with your code?
#13
Re: I can't get this to run in a visual C# form
Posted 18 January 2010 - 08:37 PM
remorseless, on 17 Jan, 2010 - 07:59 PM, said:
If you do something like this:
double x; double y = x + 1;
You might think that x in this case should be 0 right? Wrong. It will give you the error because maybe you wanted x to equal 42 or 345.234, which is why it gives you that error. Now, if you set the value when you declare the variable, you won't run into these problems, sort of like this:
double x = 0; double y = x + 1;
The above code will compile perfectly.
And that is end the of the course "Uninitialized Variables 101".
Hope that clears things up!
Also, what problems are you getting with your code?
I think I get it. The last issue i am having with this code is in this line of code// public double TempConverter(double InitialTemp, TempUnits ConvFrom, TempUnits ConvTo)// . I do think it is because I haven't properly called this double variable
|
|

New Topic/Question
Reply



MultiQuote






|