tags:

views:

60

answers:

4

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

A: 

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.

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

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

CodeKiwi
CodeKiwi
A: 

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.

A: 

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.

Chris Thompson
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.
davidsleeps