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

I have the following 2 lines in ASP.NET in VB.NET (for C # just replace the "Dim" world with "var"), which I got from the example.

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

I get a File.OpenWrite(tmpFile) error message that says: "The overload error failed because no available" File "accepts this number of arguments." Can someone explain why this error occurs? I tried to look at the documentation and cannot understand. Thanks.

+4
source share
2 answers

Note that the error message indicates File , not OpenWrite . There seems to be another File in the context that takes precedence over System.IO.File . This is probably the source of the error. Try using the full name here

 Dim tmpFileStream = System.IO.File.OpenWrite(tmpFile) 
+7
source

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

 Imports System.IO 

Also, as Daniel suggested, it might be helpful to make the code more understandable to indicate your types, for example:

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

In recent versions of VB, it automatically infers the type for you, so tmpFile will be a variable of type String , even if you did not specify. However, in older versions of VB, it will simply become the base type of Object , in which case it will not be able to determine which overload to use.

+1
source

Source: https://habr.com/ru/post/1446298/


All Articles