13 Replies - 3175 Views - Last Post: 06 February 2012 - 01:43 AM

#1 seker85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-January 12

Conversion problem

Posted 01 February 2012 - 03:33 AM

I just want to make a calculation and get the output as a double or float. I'm going to need it to make a case or if-statement.
When I type this in visual studio this error appears: "error C2106: '=' : left operand must be l-value" LINE 268



266  #pragma endregion
267  private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
268  System::Convert::ToDouble(this->textBox3->Text) = 
System::Convert::ToInt16(this->textBox1->Text) / System::Convert::ToInt16(this->textBox2->Text);
269}



Is This A Good Question/Topic? 0
  • +

Replies To: Conversion problem

#2 Macjohn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 407
  • Joined: 10-April 09

Re: Conversion problem

Posted 01 February 2012 - 03:36 AM

Remember to keep programming question the the Programming lounge.
Was This Post Helpful? 0
  • +
  • -

#3 seker85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-January 12

Re: Conversion problem

Posted 01 February 2012 - 04:29 AM

Question: Why do I get the error?
Question: Can anyone help converting this?
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5065
  • View blog
  • Posts: 26,446
  • Joined: 10-May 07

Re: Conversion problem

Posted 01 February 2012 - 04:32 AM

What language is this for, so that I can move it to the correct sub-forum?

The lounge is not for programming questions.
Was This Post Helpful? 0
  • +
  • -

#5 seker85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-January 12

Re: Conversion problem

Posted 01 February 2012 - 05:01 AM

It's made in Visual Studio and it's C++
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5065
  • View blog
  • Posts: 26,446
  • Joined: 10-May 07

Re: Conversion problem

Posted 01 February 2012 - 05:02 AM

** Topic moved to C/C++ **
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Conversion problem

Posted 01 February 2012 - 05:26 AM

** Topic moved to CLI C++ **

Because C++ programmers don't know what the hell it is, either. ;)
Was This Post Helpful? 3
  • +
  • -

#8 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,356
  • Joined: 02-June 10

Re: Conversion problem

Posted 01 February 2012 - 06:12 AM

 System::Convert::ToDouble(this->textBox3->Text) =
System::Convert::ToInt16(this->textBox1->Text) / System::Convert::ToInt16(this->textBox2->Text);


The short answer is... The left side of the = sign needs to be a variable or something that you can assign a value to

x = 4

not

convert.todouble.x = blah blah blah
Was This Post Helpful? 1
  • +
  • -

#9 seker85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-January 12

Re: Conversion problem

Posted 01 February 2012 - 06:32 AM

Well this is what I made so far:

 
this->textBox3->Text = System::Convert::ToString(System::Convert::ToDouble(this->textBox1->Text)
/ ((System::Convert::ToDouble(this->textBox2->Text))/10  /((System::Convert::ToDouble(this->textBox2->Text))/100));



and this works but I get the output as a string. When I try to make it a double it says error:
error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'double' to 'System::String ^' in line 2-3

But it's working fine when I replace "Double" with "String" in line 1.
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,356
  • Joined: 02-June 10

Re: Conversion problem

Posted 01 February 2012 - 07:02 AM

While you are still learning, may I suggest you break down the long complex lines into more easily followed statements?

double Alpha = System::Convert::ToDouble(this->textBox1->Text);
double Bravo = ((System::Convert::ToDouble(this->textBox2->Text))/10  
double Charlie = ((System::Convert::ToDouble(this->textBox2->Text))/100));
double Result1 = Alpha/Bravo
double Result2 = Result1/Charlie


Now you have values that you can check at each step along the way.

Next... For the love of Pete rename your textboxes before you start working with them! It is so much easier to follow

tbNumerator/tbDenominator

Update: Here's the full suggestions I give everyone.


Let me also throw in a few tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

This post has been edited by tlhIn`toq: 01 February 2012 - 07:06 AM

Was This Post Helpful? 2
  • +
  • -

#11 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Conversion problem

Posted 01 February 2012 - 07:15 AM

What you offered doesn't really match up, paren wise:
this->textBox3->Text = System::Convert::ToString(
	System::Convert::ToDouble(this->textBox1->Text)
	/ (
		(System::Convert::ToDouble(this->textBox2->Text))/10  / 
		((System::Convert::ToDouble(this->textBox2->Text))/100)
		);
		// missing one ) ...



Take things a step at a time, at first.

Just guessing at what you're aiming for form above, I'd do it like so:
Double d1 = Double::Parse(textBox1->Text);
Double d2 = Double::Parse(textBox2->Text);
Double result = d1 / (d2/10) / (d2/100);
this->textBox3->Text = result.ToString();



Note, the Double::Parse is the same as Convert::ToDouble; I just prefer it. All .NET types have a ToString method, so you might as well get some use out of it. Note that "double" can be quite a different thing than "System::Double" if you're not careful. Stick with caps and .NET number structs to maintain sanity.

Hope this helps.

This post has been edited by baavgai: 01 February 2012 - 07:15 AM

Was This Post Helpful? 3
  • +
  • -

#12 seker85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-January 12

Re: Conversion problem

Posted 02 February 2012 - 01:55 AM

Thanks for the help, it's fine examples you gave. But my issue here is the conversion.

	Double d1 = Double::Parse(textBox1->Text);
	Double d2 = Double::Parse(textBox2->Text);
	Double result = d1 / (d2/10) / (d2/100);
	this->textBox3->Text = result.ToString(); 


I don't want the result to be a string but a double or float so I can use it in a loop.
Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Conversion problem

Posted 02 February 2012 - 05:17 AM

The TextBox Text property is a string. So, as you see, the control takes strings and gives strings. To display something in a TextBox, it has to find it's way to string. What you do with a value outside a control is up to you.

In the above example, the variable "result" is type Double, so I'm unsure what the issue is. We only get a string for it so the TextBox can use it.
Was This Post Helpful? 1
  • +
  • -

#14 seker85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-January 12

Re: Conversion problem

Posted 06 February 2012 - 01:43 AM

Thanks, I realized that after taking a good look.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1