0% found this document useful (0 votes)
257 views

How To Get Text and A Variable in A Messagebox - Stack Overflow

The document discusses different ways to display both text and a variable in a message box in Visual Basic .NET. It recommends using string interpolation or the string format method. String concatenation in VB.NET uses the ampersand "&" operator to combine a string and a variable into a single string to display in a message box.

Uploaded by

Krishanu Modak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
257 views

How To Get Text and A Variable in A Messagebox - Stack Overflow

The document discusses different ways to display both text and a variable in a message box in Visual Basic .NET. It recommends using string interpolation or the string format method. String concatenation in VB.NET uses the ampersand "&" operator to combine a string and a variable into a single string to display in a message box.

Uploaded by

Krishanu Modak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

 

How to get text and a variable in a messagebox


Asked
9 years, 8 months ago Active
9 months ago Viewed
151k times

I just need to know how to have plain text and a variable in a messagebox.

13 For example:

I can do this: MsgBox(variable)

And I can do this: MsgBox("Variable = ")


4
But I can't do this: MsgBox("Variable = " + variable)

vb.net messagebox

Share Follow asked Dec 25 '11 at 15:06


Mark Kramer
3,008 7 30 50

string concatenation in vb.net uses the ampersand "&"


– Ric
Dec 25 '11 at 15:23

Give an example and put it in an answer and I will accept it


–  Mark Kramer
Dec 25 '11 at 15:32

5 Answers Active Oldest Votes

As has been suggested, using the string.format method is nice and simple and very readable.

21 In vb.net the " + " is used for addition and the " & " is used for string concatenation.

In your example:

MsgBox("Variable = " + variable)

becomes:

MsgBox("Variable = " & variable)

I may have been a bit quick answering this as it appears these operators can both be used for
concatenation, but recommended use is the "&", source http://msdn.microsoft.com/en-
us/library/te2585xw(v=VS.100).aspx

maybe call

variable.ToString()

update:

Use string interpolation (vs2015 onwards I believe):

MsgBox($"Variable = {variable}")

Share Follow edited Nov 1 '17 at 16:35 answered Dec 25 '11 at 15:35
Ric
11.8k 3 25 35

2 Yep, (String1 + String2) and (String1 & String2) are both fine, but if want to concatenate a
String and an Integer, you need & . Just ran into this issue today.
– TylerH
Apr 25 '17 at 18:28

That'd be much more verbose than simply using & , wouldn't it?
– TylerH
Apr 25 '17 at 18:33

Only slightly. Very readable and you could also use string interpolation depending on which .net version
you use.
– Ric
Apr 25 '17 at 19:10

Why not use:

5 Dim msg as String = String.Format("Variable = {0}", variable)

More info on String.Format

Share Follow answered Dec 25 '11 at 15:13


IAbstract
19k 15 85 139

I kind of run into the same issue. I wanted my message box to display the message and the
vendorcontractexpiration. This is what I did:
0
Dim ab As String

Dim cd As String

ab = "THE CONTRACT FOR THIS VENDOR WILL EXPIRE ON "

cd = VendorContractExpiration

If InvoiceDate >= VendorContractExpiration - 120 And InvoiceDate <


VendorContractExpiration Then

MsgBox [ab] & [cd], vbCritical, "WARNING"

End If

Share Follow edited Jul 5 '16 at 22:29 answered Jul 5 '16 at 22:09
Alex K Julieta
21.4k 18 102 218 1
MsgBox("Variable {0} " , variable)

0
Share Follow answered Dec 19 '17 at 13:12
Muhammad Saeed
79 2 9

I wanto to display the count of rows in the excel sheet after the filter option has been applied.

0 So I declared the count of last rows as a variable that can be added to the Msgbox

Sub lastrowcall()

Dim hm As Worksheet

Dim dm As Worksheet

Set dm = ActiveWorkbook.Sheets("datecopy")

Set hm = ActiveWorkbook.Sheets("Home")

Dim lngStart As String, lngEnd As String

lngStart = hm.Range("E23").Value

lngEnd = hm.Range("E25").Value

Dim last_row As String

last_row = dm.Cells(Rows.Count, 1).End(xlUp).Row

MsgBox ("Number of test results between the selected dates " + lngStart + "

and " + lngEnd + " are " + last_row + ". Please Select Yes to continue

Analysis")

End Sub

Share Follow edited Nov 11 '20 at 18:00 answered Nov 10 '20 at 17:54
Cristiano Casciotti Gowtham
978 8 21 1 2

You might also like