12 Replies - 1778 Views - Last Post: 18 January 2010 - 08:37 PM Rate Topic: -----

#1 boosh   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 14-January 10

I can't get this to run in a visual C# form

Post icon  Posted 14 January 2010 - 09:51 PM

I am new to the C# world and I am having a hell of a time. I can't get this code to work in a visual C# windows form. can any body help?
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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: I can't get this to run in a visual C# form

#2 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: I can't get this to run in a visual C# form

Posted 14 January 2010 - 09:56 PM

this is the C++ form. I dont know C#, but where is the entry point of your program?
Was This Post Helpful? 0
  • +
  • -

#3 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: I can't get this to run in a visual C# form

Posted 14 January 2010 - 09:56 PM

You're in the wrong forum, off to the C# forum.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6258
  • View blog
  • Posts: 24,026
  • Joined: 23-August 08

Re: I can't get this to run in a visual C# form

Posted 14 January 2010 - 10:04 PM

What the heck does this

Quote

I can't get this code to work in a visual C# windows form

mean exactly??? Are there errors? Does your hard drive get formatted? Is the cupholder not retracting?

BE. SPECIFIC.
Was This Post Helpful? 0
  • +
  • -

#5 boosh   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 14-January 10

Re: I can't get this to run in a visual C# form

Posted 16 January 2010 - 05:38 PM

View PostImaSexy, on 14 Jan, 2010 - 08:56 PM, said:

this is the C++ form. I dont know C#, but where is the entry point of your program?


I don't know what an entry point is. I tryed to place the code into my button object but I keep getting an error for having } in the wrong spot I think.
Was This Post Helpful? 0
  • +
  • -

#6 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: I can't get this to run in a visual C# form

Posted 16 January 2010 - 05:42 PM

Please post the code that you are having problems with, and any errors that you are receiving. Please use code tags like this. :code:
Was This Post Helpful? 0
  • +
  • -

#7 boosh   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 14-January 10

Re: I can't get this to run in a visual C# form

Posted 16 January 2010 - 05:49 PM

View PostJackOfAllTrades, on 14 Jan, 2010 - 09:04 PM, said:

What the heck does this

Quote

I can't get this code to work in a visual C# windows form

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!

View PostSixOfEleven, on 16 Jan, 2010 - 04:42 PM, said:

Please post the code that you are having problems with, and any errors that you are receiving. Please use code tags like this. :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 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

Was This Post Helpful? 0
  • +
  • -

#8 remorseless   User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 129
  • Joined: 08-August 09

Re: I can't get this to run in a visual C# form

Posted 16 January 2010 - 06:02 PM

You have too many ending braces, plus the double Temp was not initialized. Some compilers don't like working with uninitialized variables. I have no idea what the Show() is, but I trust that you know so here's your code revised.
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! :D

EDIT: Sorry, I forgot the breaks in the switch :P try the code now.

This post has been edited by remorseless: 17 January 2010 - 09:00 PM

Was This Post Helpful? 0
  • +
  • -

#9 boosh   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 14-January 10

Re: I can't get this to run in a visual C# form

Posted 16 January 2010 - 06:24 PM

View Postremorseless, on 16 Jan, 2010 - 05:02 PM, said:

You have too many ending braces, plus the double Temp was not initialized. Some compilers don't like working with uninitialized variables. I have no idea what the Show() is, but I trust that you know so here's your code revised.
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! :D

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?
Was This Post Helpful? 0
  • +
  • -

#10 boosh   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 14-January 10

Re: I can't get this to run in a visual C# form

Posted 16 January 2010 - 06:56 PM

B)

This post has been edited by boosh: 17 January 2010 - 05:58 PM

Was This Post Helpful? 0
  • +
  • -

#11 boosh   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 14-January 10

Re: I can't get this to run in a visual C# form

Posted 17 January 2010 - 05:56 PM

:^:
Was This Post Helpful? 0
  • +
  • -

#12 remorseless   User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 129
  • Joined: 08-August 09

Re: I can't get this to run in a visual C# form

Posted 17 January 2010 - 08:59 PM

Sure. Sometimes a compiler doesn't like uninitialized variables simply because it doesn't want errors in your code. The compiler doesn't know if you wanted that variable to start off being 0 or 19 or whatever value. So, to make sure the code runs the way you want it, it gives you an error saying there is an uninitialized variable.
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! :D

Also, what problems are you getting with your code?
Was This Post Helpful? 0
  • +
  • -

#13 boosh   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 14-January 10

Re: I can't get this to run in a visual C# form

Posted 18 January 2010 - 08:37 PM

View Postremorseless, on 17 Jan, 2010 - 07:59 PM, said:

Sure. Sometimes a compiler doesn't like uninitialized variables simply because it doesn't want errors in your code. The compiler doesn't know if you wanted that variable to start off being 0 or 19 or whatever value. So, to make sure the code runs the way you want it, it gives you an error saying there is an uninitialized variable.
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! :D

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 :crazy:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1