You have to add a refererence to Microsoft.Visualbasic
(Top menu, under "Add Reference" and the ".NET" Tab) and use Strings.Chr
and Strings.Asc
if you want to emulate perfectly the behaviour of Chr
and Asc
(as warned in this link by Garry Shutler's answer). Your code would become:
string Part1 = "Some string";
string p_str = null;
for (int I = 0; I < Part1.Length; I++)
{
p_str = Strings.Chr(Strings.Asc(Part1.Substring(I, 1)) + 17).ToString();
}
CLARIFICATION 1: the original code you are posting is not (pure) VB.NET, it is VB (VB.NET allows most of the VB commands to be written in). That's why so many changes are required (for example: changing the starting index); VB.NET is much more similar to C#.NET than this (as shown below).
CLARIFICATION 2: it is generally accepted that the C# translations for VB's Asc
and Chr
are mere casts (to int
and char
respectively), as you can see in the most voted answer in the aforementioned link. THIS IS WRONG. Test this code with both alternatives to confirm that only the Strings
options deliver always the right result.
"PROPER" VB.NET CODE:
Dim Part1 As String = "Some string"
Dim p_str As String = Nothing
For I As Integer = 0 To Part1.Length - 1
p_str = Chr(Asc(Part1.Substring(I, 1)) + 17)
Next