I'm converting some VB code in C#, i have this if sentence
if param and 1 then
...
end if
i don't get how to convert this in C# (btw param is a short), is this something related to power of 2? How can i convert this?
You need to write this as a boolean expression:
if ((param & 1) != 0)
{
..
}
I would use a code convert to migrate you code across try the following site:
http://converter.telerik.com/
Related
I have this line of code:
var defaultResult = $"Enum_{#this.GetType().Name}_{#this}";
but I get this error:
expected ;
How do I fix this? String.Format?
The $ is a feature available in C# 6.0. Make sure you're set to the correct version.
String interpolation is supported onward c# 6.0.
read here for more
Is there an equivalent to Perl's $_ function? I'm rewriting some old perl scripts in C# and I never learned any perl. Heres an example of what i'm trying to figure out
sub copyText {
while($_[0]){
$_[1]->Empty();
$_[0] = $_[1]->IsText();
sleep(1);
}
First of all, $_ is not a function. It's just an ordinary variable (that happens to be read and changed by a lot of builtins).
Second of all, the code you posted does not use $_. It's accessing elements of #_, the parameter list.
A more more readable version of the code you posted would be:
sub copyText {
my ($arg1, $arg2) = #_;
while ($arg1) {
$arg2->Empty();
$arg1 = $arg2->IsText();
sleep(1);
}
$_[0] = $arg1; # arg1 is passed by reference
}
arg1 is a boolean passed by reference.
arg2 is some kind of object with a method named Empty and one named IsText.
Sorry, I don't know C#, but hopefully you can move on with this.
Perl's $_ function
It's not a function. It's a pronoun meaning 'it'.
There's another special variable #_, which is a pronoun meaning 'them'.
There's no analogue in C#.
I wrote VB.NET code like this:
d = Data.IndexOf("</a>", ("target='_top' class='ab1'>").Length() + s).
I want to write this in C#. When I wrote the above code in C#, it said there was an error with the Length keyword. How do I write the above code in C#?
Length is not a keyword in C# - it is either a property or an extension method on the object (like a string) that you are trying to manipulate.
So if it is a string you are using this will work:
myString.Length
(notice how the brackets are missing because it is a property).
You have an extra set of parentheses:
d = Data.IndexOf("</a>", "target='_top' class='ab1'>".Length + s)
Try that
Check this link out:
In it, you can easliy switch between C# to a VB good, to help you migrate:
http://msdn.microsoft.com/en-us/library/system.string.length.aspx#Y242
What will be the equivalent code for Format(iCryptedByte, "000") (VB.NET) in C# ?
String.Format(format, iCryptedByte); // where format like {0:D2}
See MSDN 1, 2, 3
Another very useful site for C# string formatting: http://blog.stevex.net/string-formatting-in-csharp/
Instead of {0:D3} you can also use the zero placeholder, e.g. {0:000} will pad with zeros to minimum length of three.
Microsoft.VisualBasic.Strings.Format(iCryptedByte, "000");
You'll need to add a reference to the Microsoft.VisualBasic assembly.
Given this VB code:
Strings.Format(iCryptedByte, format)
Replace with this C# code:
var csformat = "{0:" + format + "}";
String.Format(csformat, iCryptedByte);
Try:
iCryptedByte.ToString("D3");
see String.Format
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?
find: \(string\){:a*}
replace: \1.ToString()
Back up your solution first!
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.
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.
Suggest you to use this one
Find: \(string\){(.*)}{:Po}
Replace: \1.ToString()\2
Good luck!
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.
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...