Click here to Skip to main content
14,772,815 members
Home » Discussions » C#
   

C#

 
GeneralRe: How to selected value of a dropbox column of a DataGridView programmatically? Pin
Alex Dunlop13-Nov-20 4:09
MemberAlex Dunlop13-Nov-20 4:09 
AnswerRe: How to selected value of a dropbox column of a DataGridView programmatically? Pin
Gerry Schmitz13-Nov-20 5:10
mveGerry Schmitz13-Nov-20 5:10 
QuestionCommented code in c# forms does it affect the performance Pin
Sachin Kulkarni B12-Nov-20 5:50
MemberSachin Kulkarni B12-Nov-20 5:50 
AnswerRe: Commented code in c# forms does it affect the performance Pin
OriginalGriff12-Nov-20 6:08
mveOriginalGriff12-Nov-20 6:08 
GeneralRe: Commented code in c# forms does it affect the performance Pin
trønderen12-Nov-20 8:22
Membertrønderen12-Nov-20 8:22 
AnswerRe: Commented code in c# forms does it affect the performance Pin
Gerry Schmitz13-Nov-20 4:58
mveGerry Schmitz13-Nov-20 4:58 
QuestionThe name does not exist in the current context Pin
darkdxd11-Nov-20 23:04
Memberdarkdxd11-Nov-20 23:04 
AnswerRe: The name does not exist in the current context Pin
Richard Deeming12-Nov-20 0:01
mveRichard Deeming12-Nov-20 0:01 
darkdxd wrote:
String C = TextBox1.Text;
int TXTC = Int32.Parse(C);
string TXTC1 = TXTC.ToString();
TXTC = (TXTF - 32) * (5 / 9); // <-- Error on this line
Look at the variables you have declared here:
  • C - a local variable of type string;
  • TXTC - a local variable of type int;
  • TXTC1 - an unused local variable of type string;
  • TextBox1 - a field of type TextBox;

You then try to perform a calculation using the variable TXTF, which you haven't declared anywhere within this method.

Some other problems:
  • You're using Int32.Parse, which will throw an exception if the user types in something that's not a number. You should use Int32.TryParse[^] instead.
  • You convert the integer back to a string for no reason;
  • You're performing integer arithmetic, which isn't going to work - for example, 5 / 9 will return 0;
  • Based on the variables names, it looks like you've got the calculations the wrong way round;
  • You don't do anything with the result of the calculation;

You're also still using the default control names assigned by the Visual Studio designer. You should give your controls more meaningful names instead.

An improved example:
protected void CelsiusTextBox_TextChanged(object sender, EventArgs e)
{
    string celsiusText = CelsiusTextBox.Text;
    
    double celsius;
    if (!double.TryParse(celsiusText, out celsius))
    {
        FahrenheitTextBox.Text = "Please enter a valid temperature";
        return;
    }
    
    double fahrenheit = celsius * (9D / 5D) + 32D;
    FahrenheitTextBox.Text = fahrenheit.ToString();
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

AnswerRe: The name does not exist in the current context Pin
trønderen12-Nov-20 0:01
Membertrønderen12-Nov-20 0:01 
AnswerRe: The name does not exist in the current context Pin
Richard MacCutchan12-Nov-20 0:03
mveRichard MacCutchan12-Nov-20 0:03 
AnswerRe: The name does not exist in the current context Pin
OriginalGriff12-Nov-20 0:06
mveOriginalGriff12-Nov-20 0:06 
QuestionHow to find days between a defined date and now? Pin
Alex Dunlop10-Nov-20 8:51
MemberAlex Dunlop10-Nov-20 8:51 
AnswerRe: How to find days between a defined date and now? Pin
Richard MacCutchan10-Nov-20 9:56
mveRichard MacCutchan10-Nov-20 9:56 
GeneralRe: How to find days between a defined date and now? Pin
Alex Dunlop10-Nov-20 17:48
MemberAlex Dunlop10-Nov-20 17:48 
GeneralRe: How to find days between a defined date and now? Pin
Richard MacCutchan10-Nov-20 22:57
mveRichard MacCutchan10-Nov-20 22:57 
AnswerRe: How to find days between a defined date and now? Pin
Gerry Schmitz10-Nov-20 15:51
mveGerry Schmitz10-Nov-20 15:51 
GeneralRe: How to find days between a defined date and now? Pin
Alex Dunlop10-Nov-20 17:57
MemberAlex Dunlop10-Nov-20 17:57 
GeneralRe: How to find days between a defined date and now? Pin
Gerry Schmitz10-Nov-20 19:11
mveGerry Schmitz10-Nov-20 19:11 
AnswerRe: How to find days between a defined date and now? Pin
Mycroft Holmes11-Nov-20 12:12
professionalMycroft Holmes11-Nov-20 12:12 
QuestionJSON "classes" repetitive Pin
Member 56973910-Nov-20 7:25
MemberMember 56973910-Nov-20 7:25 
AnswerRe: JSON "classes" repetitive Pin
Gerry Schmitz10-Nov-20 8:24
mveGerry Schmitz10-Nov-20 8:24 
GeneralRe: JSON "classes" repetitive Pin
Member 56973910-Nov-20 9:06
MemberMember 56973910-Nov-20 9:06 
GeneralRe: JSON "classes" repetitive Pin
Gerry Schmitz10-Nov-20 15:33
mveGerry Schmitz10-Nov-20 15:33 
QuestionColor the intersected rectangles in c# windows applicaion Pin
Member 147377589-Nov-20 20:47
MemberMember 147377589-Nov-20 20:47 
AnswerRe: Color the intersected rectangles in c# windows applicaion Pin
OriginalGriff9-Nov-20 21:12
mveOriginalGriff9-Nov-20 21:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.