0

I have some confusion as to the use of + and & in ASP.NET and VB.NET. See the following code:

Dim dtUser As DataTable = GetDetails()
        Dim serverPath As String = Nothing
        Dim virtualServerPath As String = Nothing
        Dim parentDir As DirectoryInfo = Nothing
        Dim childDir As DirectoryInfo = Nothing
        serverPath = Page.Server.MapPath(".") + "\"
        virtualServerPath = serverPath.Substring(0, serverPath.LastIndexOf("\"))
        virtualServerPath = virtualServerPath + "\SiteImages\" + dtUser.Rows(0)("Name")
        parentDir = Directory.CreateDirectory(virtualServerPath)
        childDir = parentDir.CreateSubdirectory(Session("RegID"))
        Dim strUserName as String=dtUser.Rows(0)("Name")
        If flUpload.HasFile Then
            flUpload.SaveAs(Server.MapPath("~/SiteImages/" & dtUser.Rows(0)("Name") & "/" & childDir + "/" + flUpload.FileName))

I am getting error concerned with usage of + and & in

 flUpload.SaveAs(Server.MapPath("~/SiteImages/" & strUserName & "/" & childDir + "/" + flUpload.FileName))

Can anybody help to remove the error

1
  • See my answer below. This question apparently has nothing to do w/ & and +, apparently they are interchangable, although I would never use + with string concat in VB. Commented Jul 15, 2009 at 5:14

4 Answers 4

3

Use "&" for concatenation, "+" will work until you have a value that a mathematical operation can be performed on in the concatenation. It will attempt to perform the addition rather than concatenation.

eg.

"blah" & "blah" works
"blah" + "blah" works
"blah" & 5 works
"blah" + 5 fails

The last one does not work as it will try to "add" 5 and a string

Sign up to request clarification or add additional context in comments.

1 Comment

Also I don't think you can mix them in the same statement. if you try to perform "blah" & "blah" + "blah" it will fail as it will attempt to add the last string.
0

change

flUpload.SaveAs(Server.MapPath("~/SiteImages/" & 
                        dtUser.Rows(0)("Name") & 
                                           "/" & 
                                      childDir + 
                                           "/" + 
            flUpload.FileName))

to

flUpload.SaveAs(Server.MapPath("~/SiteImages/" & 
                        dtUser.Rows(0)("Name") & 
                                           "/" & 
                                      childDir & 
                                           "/" & 
            flUpload.FileName))

and pay heed to @CodeWiki's comment on this answer that is not to mix + and & in one statement.

Comments

0

In VB, the & is for string concatenation. You should only use + for addition operations.

The problem with + is that it tries to do implicit conversion so you can do

 2.5 + 5

C# would give you an error because 2.5 would be a float and 5 would be an int. You would need to cast them. VB does the casting implicitly which can hide some bugs.

1 Comment

you just need to adjust the Compiler options for the project in Visual Studio to Warn or cause exception for those situations...it may not be a default though.
0

As far as I know VB string concatenation uses &, don't use +

"A" & "B" & "C" = "ABC"

"A" + "B" + "C" = hmmm error? (Edit) apparently this works...

(More Edit)...

Possible answer to your error:

I don't think the error is anything to do with & or + now. It might be your childDir which is of type DirectoryInfo. You might want to get the name of the directory in it instead of just plopping childDir in the string concat.

try change it to & childDir.Name in that concat.

1 Comment

apparently + is working also... Hmm... I didn't know that :) Never used it when I was doing VB 6 back then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.