|
Hi I faced a problem when converting for VS2002 to VS2005. 'Cannot declare variable of static type 'System.IO.File', because i declare an instance variable of this Type in my Old Code.
When i look in detail, the defination of System.IO.File has been changed
Previous Defination : public sealed class File : System.Object Member of System.IO
New Defination : public static class File : System.Object Member of System.IO
Does any body have solution, i cannot change my code,because its lot of code. I wonder Why Microsoft Did this.
Regards | | |
C# 1.1 To C# 2.0 Problem
wagnerjp
.NET 1.1 had no concept of a sealed class, so the best they could do was make it a sealed class with a private constructor. Now that they have the static keyword, they changed the File class to use it because it was always the intent of the File class to be static. They just didn't have a formal mechanism to make the class static before. This, appearantly, adds the restriction that you not only cannot create instances of the class, you also cannot declare variables or parameters of that type as well. This makes sense, though, since you cannot create an instance of it and such variables parameters would always be null. But, this shouldn't have been a problem because such variables would have been useless in .NET 1.1 without a mechanism for creating instances of type File. The only way you could have been creating a File object is with a Reflection hack. If thats what you did, then you were accessing the File object's private interface, which Microsoft may change at any time without reason or notice.
You're only real choice is to fix your code so that myFile.Method() is changed to File.Method(), and so forth. That is what was intended by the File class, and I guarantee it wont break in the next version :-).
MartinMalek
I have no idea how you managed to construct a System.IO.File object, since its constructor is private.
For example, in .Net 1.x if I do this:
System.IO.File file = new System.IO.File();
I get the following error:
"C:\test\VS2003\CSharp\ConsoleApplication1\Class1.cs(11): 'System.IO.File.File()' is inaccessible due to its protection level"
How was your code creating one It seems very odd.
publicENEMY
How can you create a System.IO.File object to pass to your SetFile() method The System.IO.File constructor is, as I said, private.
Microsoft can change this API because it's impossible to create System.IO.File objects, and therefore no callable code could be broken.
You should just be able to remove your SetFile() method, because the tFile parameter can only ever be null, and therefore you can't really do anything with it.
Mark_Davies
I worte my class like this
public class Test
{
private System.IO.File myFile = null;
public SetFile( System.IO.File tFile)
{
myFile = tFile
}
}
But this code No Creats problem on both lines
private System.IO.File myFile = null;
and this line too
public SetFile( System.IO.File tFile)
But question was this class was sealed, how can microsoft convert it to static, To Me its Odd, because there may be so many people using this API. And general Concept is u never change the API Interface.
Regards