8 Replies - 638 Views - Last Post: 15 October 2013 - 08:49 AM Rate Topic: -----

#1 Jmontes1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-October 13

How can i get variables to print out in a MessageBox

Posted 10 October 2013 - 03:58 PM

I am having trouble getting a program to print out the conversion of two numbers in a messageBox not (msgbox)
this is what i have so far

  MessageBox.Show("sngKilometers Is sngConversion""Would you like To start another Distance conversion?")

i have als0 tried
 MessageBox.Show("sngKilometers Is sngConversion" & "Would you like To start another Distance conversion?")


sngKilometers is a variable where user input is stored and sngconversion is where a the conversion from kilometers to miles is converted.

if your wondering why i am doing the variables as Floating points rather that integers. That is how the teacher wants us to write the program.

ANY Suggestions???

Is This A Good Question/Topic? 0
  • +

Replies To: How can i get variables to print out in a MessageBox

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,097
  • Joined: 02-July 08

Re: How can i get variables to print out in a MessageBox

Posted 10 October 2013 - 04:45 PM

It does not recognize a variable in a string. You have to tell it what to use. String.Format inserts text where the {} are by there index.

MessageBox.Show(String.Format("{0} is {1}. Would you like To start another Distance conversion?", sngKilometers, sngConversion)

This post has been edited by _HAWK_: 10 October 2013 - 04:45 PM

Was This Post Helpful? 2
  • +
  • -

#3 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 246
  • View blog
  • Posts: 1,377
  • Joined: 11-May 12

Re: How can i get variables to print out in a MessageBox

Posted 11 October 2013 - 06:01 AM

HAWK's way is how I would do it, another way you could do it is using the & concatenation operator like you do in your second attempt, but you would want this instead

MessageBox.Show(sngKilometers.ToString & " Is " & sngConversion.ToString & "Would you like To start another Distance conversion?")


Was This Post Helpful? 1
  • +
  • -

#4 rodit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-May 13

Re: How can i get variables to print out in a MessageBox

Posted 11 October 2013 - 12:39 PM

Very simple code for this:
MsgBox.Show(variable + "string here")

Was This Post Helpful? 0
  • +
  • -

#5 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 246
  • View blog
  • Posts: 1,377
  • Joined: 11-May 12

Re: How can i get variables to print out in a MessageBox

Posted 11 October 2013 - 12:42 PM

@rodit, you don't want to use the "+" operator when trying to concatenate strings.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3614
  • View blog
  • Posts: 12,437
  • Joined: 12-December 12

Re: How can i get variables to print out in a MessageBox

Posted 11 October 2013 - 12:47 PM

View Postrodit, on 11 October 2013 - 07:39 PM, said:

Very simple code for this:
MsgBox.Show(variable + "string here")

This is incorrect. It's MessageBox.Show() and, as ybadragon says, the concatenation operator is '&' and it is advisable not to use '+' for this purpose.

This post has been edited by andrewsw: 11 October 2013 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

#7 rodit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-May 13

Re: How can i get variables to print out in a MessageBox

Posted 12 October 2013 - 06:07 AM

View Postandrewsw, on 11 October 2013 - 12:47 PM, said:

View Postrodit, on 11 October 2013 - 07:39 PM, said:

Very simple code for this:
MsgBox.Show(variable + "string here")

This is incorrect. It's MessageBox.Show() and, as ybadragon says, the concatenation operator is '&' and it is advisable not to use '+' for this purpose.


K thanks for the tip... It still works though
Was This Post Helpful? 0
  • +
  • -

#8 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 605
  • Joined: 01-October 08

Re: How can i get variables to print out in a MessageBox

Posted 12 October 2013 - 06:35 AM

View Postrodit, on 12 October 2013 - 08:07 AM, said:

K thanks for the tip... It still works though


They know that in some cases it works, but under certain circumstances it might actually add the values on either side of the operator. The & is the correct operator to use for concatenation. When helping others the goal should be to give an answer that always works.
Was This Post Helpful? 2
  • +
  • -

#9 Jmontes1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-October 13

Re: How can i get variables to print out in a MessageBox

Posted 15 October 2013 - 08:49 AM

View Postybadragon, on 11 October 2013 - 06:01 AM, said:

HAWK's way is how I would do it, another way you could do it is using the & concatenation operator like you do in your second attempt, but you would want this instead

MessageBox.Show(sngKilometers.ToString & " Is " & sngConversion.ToString & "Would you like To start another Distance conversion?")



Thank you very much this made my code work. I was able to understand my problem.
To everyone else thank you you where all very helpfull
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1