Question Str Replace Error when System.IO.File used

Robo101Com

New member
Joined
Mar 23, 2009
Messages
2
Programming Experience
3-5
Hi,

So I have some code that uses the Replace function to modify the content of a string like:
x = Replace("Hello World", "H", "P")
This works fine until I decide I need to use so of the abilities found in "System.IO.File"
When I place at the top of my code.
Imports System.IO.File

The " x = Replace("Hello World", "H", "P") " code pops up with the
Error: "Expression does not produce a value."

Looks as if the System.IO.File thanks I now want to do some file manipulation... Uggg what to do?

Thanks for any help.
Shane
 
First up, you shouldn't be importing System.IO.File. While you can, importing types is something you should not do. System.IO.File is a class.

By importing System.IO.File you're telling the compiler that you want to be able to refer to any of its members without qualifying them. The System.IO.File class has a Replace so now your code is referring to the System.IO.File.Replace method.

What you should be doing is only importing the System.IO namespace and then using the type name in code, i.e. use File.Open, File.Exists, etc. in code. Now you can use Replace the same way as you were before because there is no name clash.

If you really did want to import the System.IO.File class then you could have used Strings.Replace in your code, thereby qualifying the name with the type it's a member of, so the compiler knew that you didn't mean File.Replace.

Having said all that, I strongly recommend not using that Replace function anyway. I would suggest using String.Replace instead, i.e.
VB.NET:
x = ("Hello World").Replace("H", "P")
That's the more .NET where what you were doing is more VB6-style. Not a crime and many people do it but I suggest not using any VB6-style functions.
 
jmcilhinney... Thanks so much for the help, I gave it a shot and it work just fine and im taking your advice and using String.Replace

Thanks!
Shane
 
Back
Top