views:

293

answers:

6

I just converted a VB.net solution into C# solution. But lots of variables have been coverted like:

string var1 = (string)1;
string var2 = (string)intVar; //intVar is a int value

I just want to convert all (string)XXXX to XXXX.ToString() by using some Regular expressions or some other replace methods.

Can someome help me out?

A: 

Hi

I am not too familiar with regex but I offer a warning instead. You might not want to replace all (string)xxx to xxx.toString() because (as you know I am sure) (string) is casting and a ToString() is a method call. You can only cast something as string if the object is a descendant of string. You can call ToString() if the implementor of the class overrode the ToString() method. If not then you are just going to get the default implementation.

uriDium
In my solution most of cases are convert int value into string value. So my question is how to convert (string)intValue into format intValue.ToString(). It may give me some errors but totally the compilling errors will decrease tremendous
CodeYun
I agree that fixing the build errors is good, but, the other issue, as this poster and myself have pointed out, will go un-noticed if you simply use a find/replace method.
Mitchel Sellers
Not too sure why this got downvoted but anyway.
uriDium
Don't know why it was downvoted but so have upvoted it in support :)
Joe
Lol, thanks. :)
uriDium
+3  A: 

The text editor Notepad++ has regular expression support. You may try something like: Replace [(]string[)][ ]*([^ .\t;/]*) with \1.ToString().

This turns this:

(string) xyz;
(string) abc.123;
(string)alf;
(string)argu ment

into this:

xyz.ToString();
abc.ToString().123;
alf.ToString();
argu.ToString() ment

This however, does not handle the case of (string) aFunction( obj1, obj2 ).

You may want to handle these by yourself first, or build another regexp.

maxwellb
Comments for what's wrong with this would be helpful. I believe this addresses the OP's question, and provides caveats, and has been edited several times as new caveats are found.
maxwellb
I'd hazard a guess that he was asking about doing this in VS, which is not notepad++. Plus VS' regex is messed up, so standard regex won't work.
Will
Well, the question asks "How to I convert all (string)aaa to aaa.ToString()".. so, was addressing that. It's like the old "My car won't start. I need gas." Then getting heck when I give you gas but your car won't start. Another question may be in order. But I still gave you the gas. :-) Thanks for the comment.
maxwellb
No, the question asks "In C# VS2008 how to replace (string)aaa to aaa.ToString()" As in, In C# VS2008. If you've never had to deal with VS regexes you wouldn't appreciate the difference that makes. And, believe me, it does!
Will
Thank you. I will surely read up on those. I always look forward to learning new technologies and isms. Yet another window has opened up for me to learn some more about Visual Studio. :-)
maxwellb
A: 

Well, the reason that you get the extra code in the conversion, is that you don't have Option Strict On in the VB code. If you had, you wouldn't be able to do those implicit conversions in the first place.

So, the VB code should look like this, and the C# code would then look right in the conversion:

var1 As String = "1"

var2 As String = intVar.ToString

To fix this after the conversion is done is quite beyond what a regular expression is capable of. Sure, you could make an expression that would convert all (string)x to x.ToString(), but that would probably cause more problems that it fixed...

Guffa
+4  A: 

find: \(string\){:a*}
replace: \1.ToString()

Back up your solution first!

Will
You have to escape the brackets, which didn't show up in the answer. fixed.
Will
Thanks Will! Your solution is great for original question. But I found there are some other cases:Something like :attbXML.InnerText = (string)oField.Type;i have to convert to :attbXML.InnerText = oField.Type.ToString();Do you think it is possible?
CodeYun
You can switch :a* for [:a\.]*
Will
+1  A: 

I am not sure if you really want to do this as a mass conversion. As in all reality in your example, you should end up with the following.

string var1 = "1";

and

string var2 = intVar.ToString();

There is no need for your first example to be doing a cast, when it can be a string from the beginning.

Mitchel Sellers
A: 

Hi! Suggest you to use this one

Find: \(string\){(.*)}{:Po}

Replace: \1.ToString()\2

Good luck!

nbulba
Wow! it works great! Save my life.
CodeYun