Art F
Art F

Reputation: 4200

.NET the following 2 lines give a compile error, why?

I have the following 2 lines in ASP.NET in VB.NET (For C# just replace the world 'Dim' with 'var') that I got from an example.

 Dim tmpFile = Path.GetTempFileName()
 Dim tmpFileStream = File.OpenWrite(tmpFile)

I get an error on File.OpenWrite(tmpFile) that says 'Overload resolution failed because no accessible 'File' accepts this number of arguments'. Can anyone explain why this error is happening? I tried looking at documentation and can't seem to figure it out. Thank you.

Upvotes: 4

Views: 141

Answers (2)

JaredPar
JaredPar

Reputation: 754695

Notice that the error message is specifying File and not OpenWrite. It looks like there is another File in context which has a higher precedence than System.IO.File. This is likely the source of the error. Try using a fully qualified name here

Dim tmpFileStream = System.IO.File.OpenWrite(tmpFile)

Upvotes: 7

Steven Doggart
Steven Doggart

Reputation: 43743

Add the following line to the top of your code file:

Imports System.IO

Also, as Daniel suggested, it can be helpful, by making the code more clear, to specify your types, for instance:

Dim tmpFile As String = Path.GetTempFileName()
Dim tmpFileStream As FileStream = File.OpenWrite(tmpFile)

In the latest versions of VB, it will automatically infer the type for you, so it will make tmpFile to be a String type variable, even though you didn't specify. However, in older versions of VB, it will just make it a base Object type, in which case it would not be able to determine which overload to use.

Upvotes: 1

Related Questions