Try-ing to make my first form

old_man

Member
Joined
Oct 24, 2016
Messages
17
Location
Michigan USA
Programming Experience
Beginner
Here is my code for the button

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace windows_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            double temp;
            temp = Convert.ToDouble(tbTempF.text) - 32;
            temp = temp / 9.0;
            temp = temp * 5.0;
            tbTempC.text = Convert.ToString(temp);

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}


the error is for this code tbTempC and the error (THIS DOES NOT EXIST IN CURRENT CONTEXT)

What does this error means?
How do i fix it?
thank you:neglected:

form_1.jpg
 
Last edited:
You haven't marked your "public Form1()" with a return values or void if it's meant to be a subroutine.
And you should probably make it private instead of public, and maybe give it a meaningful name.


public Form1()
private void calculateTempurature()
{
double temp;
temp = Convert.ToDouble(tbTempF.text) - 32;
temp = temp / 9.0;
temp = temp * 5.0;
tbTempC.text = Convert.ToString(temp);
}
 
You haven't marked your "public Form1()" with a return values or void if it's meant to be a subroutine.

It's a constructor, so it doesn't have a return type.

As for the issue, it looks like 'tbTempF' and 'tbTempC' are supposed to be TextBoxes on your form. Have you actually added TextBoxes with those names? If not then they won't magically appear on their own. My guess is that you're blindly copying and pasting code from the web without properly understanding what it's actually doing.
 
yes, I am just like the last code I put up. But I know every thing now about that code. I will know every thing about this block of code when i get dine with it.
 
It's a constructor, so it doesn't have a return type.

As for the issue, it looks like 'tbTempF' and 'tbTempC' are supposed to be TextBoxes on your form. Have you actually added TextBoxes with those names? If not then they won't magically appear on their own. My guess is that you're blindly copying and pasting code from the web without properly understanding what it's actually doing.
Yes, I know it's a constructor, however I took into account that this is something he will want to run repeatedly seeing as it has interactions with two textboxes.
Perhaps I shouldn't make that assumption, but in this case I did.

As for his build errors, I agree in that he probably doesn't have two textboxes with those names on his form.
 
Yes, I know it's a constructor, however I took into account that this is something he will want to run repeatedly seeing as it has interactions with two textboxes.
Perhaps I shouldn't make that assumption, but in this case I did.

Looking at the code more closely, I think your assumption is quite valid. That method declaration is perfectly valid for a constructor but that's not a valid constructor because it has no call to InitializeComponent. Also, why would you be taking input from a control when the form hasn't even been constructed, never mind displayed. I would say that the OP should be adding a Button to their form and putting code like that in the Click event handler. Of course, as we both agree, they'd need to actually add the TextBoxes to that form too, if they want to move data from one TextBox to another.

@old_man, let's think this through. The idea here is obviously for the user to enter a temperature in Fahrenheit and for your code to convert it to Celsius. If you were to pick up an existing piece of software that supposedly did this, what would you expect it to include? I don't know about you but I'd expect it to have a TextBox into which I could enter the Fahrenheit temperature, somewhere for it to display the Celsius temperature and a Button to initiate the conversion. I think that I'd probably also expect it to be able to go the other way, so the place the Celsius temperature was displayed would also need to be a TextBox and it would need a second Button. Have you added all those controls to your form and have you either given them the names that your code expects or else modified the code to use the same names as you gave your controls? Clearly not. Also, you would perform the appropriate calculation when the user clicks a Button so it should be obvious that you need to handle the Click event of a Button and put the calculation code there. Remember that you're a programmer AND a user. ALWAYS consider what the user in you would expect when the programmer is developing software.
 
Back
Top Bottom