user42348
user42348

Reputation: 4319

Using & and + in asp.net with vb

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

Upvotes: 0

Views: 868

Answers (4)

Jimmy Chandra
Jimmy Chandra

Reputation: 6580

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.

Upvotes: 0

Chris Thompson
Chris Thompson

Reputation: 16851

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.

Upvotes: 0

Fantastic.4
Fantastic.4

Reputation:

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.

Upvotes: 0

CodeKiwi
CodeKiwi

Reputation: 749

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

Upvotes: 3

Related Questions