views:

195

answers:

2

I get compile error because the compiler thinks Path.Combine refers to my field, but I want it to refer to class System.IO.Path. Is there a good way to handle this other than always having to write the FQN like System.IO.Path.Combine()?

using System.IO;

class Foo
{
   public string Path;

   void Bar(){ Path.Combine("",""); } // compile error here
}
+5  A: 

You can do this:

using IOPath = System.IO.Path;

Then in your code:

class Foo
{
   public string Path;

   void Bar(){ IOPath.Combine("",""); } // compile error here
}
Praveen Angyan
I was editing my answer to add this example but you've already got it so +1 to you!
Michael Haren
This is a good answer; however, I would urge most people to, whenever possible, avoid using names that clearly conflict with .NET framework types. A name like FilePath or DataPath, or somesuch may be a better way to disambiguate property while at the same time adding clarity. However, I do think that is one of those cases, where the Path class name was probably a poor choice on the part of the .NET designers, since it is a common and useful name for properties.
LBushkin
Or avoid aliases completely, especially if its just a one-time conflict.
Will Eddins
+3  A: 

Seperate your references:

this.Path = System.IO.Path.PathFunction();

I'd strongly suggest implying the System.IO namespace when using Path anywhere inside that class, because it's very difficult to tell the difference. Using the this. qualifier and the full namespace makes them distinct.

Will Eddins