1

Why it is producing the error "expression required"?

Public Class WinApp

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub buttonSubmit_Click(sender As Object, e As EventArgs) Handles buttonSubmit.Click
        MessageBox.Show("Hello" + ' ' + "how are you");
    End Sub
End Class
Artem Kulikov
  • 2,250
  • 19
  • 32
  • ok fine where you are getting that error? –  Aug 05 '15 at 05:32
  • That is because you are trying to mix vb.net and c# code. – Pradeep Kumar Aug 05 '15 at 05:33
  • #suji, I'm getting the error 'expression required' by the + operator – user2460539 Aug 05 '15 at 05:35
  • The correct vb.net code would be: `MessageBox.Show("Hello" + " " + "how are you")` – Pradeep Kumar Aug 05 '15 at 05:35
  • Thanx #Pradeep Kumar – user2460539 Aug 05 '15 at 05:38
  • @Pradeep Kumar: No, the correct VB.NET code would be: `MessageBox.Show("Hello" & " " & "how are you")`. `+` is recommended to _not_ be used as it's only made for integers. See: http://stackoverflow.com/a/734631/3740093 – Visual Vincent Aug 05 '15 at 09:33
  • @Visual Vincent, The view expressed in that post may be personal opinion of the author. It doesn't matter whether you use + or & for string concatenation. However best practices include using `OPTION STRICT ON` that would take care of the concerns expressed in that post. – Pradeep Kumar Aug 05 '15 at 09:40
  • @Pradeep Kumar: The `+` will convert the strings into integers before concatenating them anyway. So I guess you're right. It really doesn't matter as long as you don't want to concatenate integers as strings and described in the post. _But I'd recommend to use the `&` concatenation operator._ – Visual Vincent Aug 05 '15 at 09:45
  • @VisualVincent - The `+` operator will most certainly _not_ convert the strings to integers in that code. It may well attempt to convert the string to an integer if an integer is included in the expressions, e. g. `"Hello" + 4`. But `Option Strict On` should catch this, but I agree with you that for string concatenation the `&` operator should be used. – Chris Dunaway Aug 05 '15 at 14:01

2 Answers2

0

In order to run app in c# open the c# application . You are opening vb.net project and asking c# code :) Actual code for concatenate is given below in c#

 private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello" + ' ' + "how are you");
        }
Tharif
  • 13,794
  • 9
  • 55
  • 77
0

MessageBox.Show("Hello" + " " + "how are you") is the VB.net code .. you're mixing it with some other code C# or C++ code etc.

Pretty_Girl
  • 157
  • 1
  • 8