2

I am trying to convert VB.NET code to C# code.

This is my VB.NET code:

Dim Part1 As String = "Some string"
Dim p_str As String
For I = 1 To Len(Part1)
    p_str = Chr(Asc(Mid$(Part1, I, 1)) + 17)
Next

I have translated it to C# like this:

string Part1 = "Some string";
string p_str = null;
for (I = 0; I <= Part1.Length - 1; I++)
{
    p_str = Convert.ToChar((Part1.IndexOf(Part1.Substring(I, 1)) + 65) + 17).ToString();
}

Can anyone tell me whether it’s correct?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Rakesh
  • 2,730
  • 10
  • 39
  • 65
  • 1
    This works well enough: http://www.developerfusion.com/tools/convert/vb-to-csharp/ – dcaswell Sep 07 '13 at 08:05
  • The loop needs to start at 1 in C# as well in order to be equivalent. – Asad Saeeduddin Sep 07 '13 at 08:05
  • 1
    @Asad the Mid and Len functions come from VB (VB6, VBA) and the starting indices are 1, not 0. Thus to deliver the same you have to start C# from 0 and VB from 1. – varocarbas Sep 07 '13 at 08:32
  • @user814064 I have to recognise that I misassesed your conversor: it is pretty good. It recognises that it is old VB code and relies on "strings" (although it does not say anything about the reference you have to add to your project). In any case, I don't think that relying on conversors is a good proceeding (better learning both languages). – varocarbas Sep 07 '13 at 08:58
  • @user814064: https://encrypted.google.com/search?hl=en&q=translate%20vb.net%20to%20c%23 – Ry- Sep 07 '13 at 23:20
  • @user814064: Yes, meaning Google doesn’t care, because they’re synonyms, and neither do I. – Ry- Sep 07 '13 at 23:24
  • The code as shown is overwriting p_str in every iteration of the loop, so p_str ends up as only the last conversion and you don't need the loop. If there is more code which uses p_str in the iteration, it would have helped to put in a comment like `'more code here`. – Andrew Morton Sep 07 '13 at 23:43
  • I use http://converter.telerik.com/ this seemes to be a little faster than http://www.developerfusion.com/ , both of them work right 98 percent of the times. – SamuraiJack Nov 14 '13 at 04:58

3 Answers3

6

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
Community
  • 1
  • 1
varocarbas
  • 12,354
  • 4
  • 26
  • 37
1

You don't need a reference to Microsoft.VisualBasic (edit: see the comments - in this case you do need it), and your 'for' loop condition should be "I <= Part1.Length":

string Part1 = "Some string";
string p_str = null;
for (int I = 1; I <= Part1.Length; I++)
{
    'edit: I thought this would work, but it doesn't:
    p_str = ((char)(Convert.ToInt32(Part1[I - 1]) + 17)).ToString();
    'edit: the following works, with "Strings.Chr" and "Strings.Asc" remaining:
    p_str = Microsoft.VisualBasic.Strings.Chr(Microsoft.VisualBasic.Strings.Asc(Part1[I - 1]) + 17).ToString(); 
}
Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28
  • Why are you saying "you certainly don't need a reference to Microsoft.VisualBasic" when I am saying right the contrary? Can you please, test your code and tell me the second result you get? Is not € isn't it? (because this is what you get with VB). And next time, test your code before answering anything (mainly when you are saying something against what someone has stated). If you don't think that what I have said was right, you should have told me so directly; and , eventually, help me to not write completely wrong statements. – varocarbas Sep 07 '13 at 17:13
  • The code is tested - your answer results in p_str having the value 'x', and my answer results in p_str having the value 'x' (the VB.NET code yields the same result) - both yield the same result. My answer doesn't require the reference to Microsoft.VisualBasic. – Dave Doknjas Sep 07 '13 at 17:22
  • Sorry to correct you but you test it wrong. This code iterate through all the characters but only stores the last one (this is the x you are getting, the last character). If you put a break point inside the loop, you would see that the first iteration is fine, but the second is different and so on... some characters are converted identically and some others not (that's why you HAVE to rely on Microsoft.VisualBasic) – varocarbas Sep 07 '13 at 17:24
  • In any case, if you read someone's statement (mine above but also the one in the link I am referring) saying something completely against what seems to result from just one occurrence (the character x); don't you think that you should either test it further or understand others' claims better or start a discussion with them? Instead of concluding right away that the other people is wrong and that you are right (after just doing a sample test)? – varocarbas Sep 07 '13 at 17:28
  • You're right - it appears that my solution is wrong. I wasn't saying that your answer was wrong, just that I didn't think the reference to Microsoft.VisualBasic was *necessary* - I still don't think it's necessary, but I can't find the solution without it yet. – Dave Doknjas Sep 07 '13 at 17:35
  • Logically, I am not saying that this is the only alternative. What I said was that there is a pretty extended idea (as you wrote in your answer) that it can be easily replaced with casts and this is not the case. Actually, in the link I provided, the chosen answer (and the answer getting more votes) says precisely this. In any case, please, in the future (at least with me), feel completely free to correct me if you consider that I am wrong. – varocarbas Sep 07 '13 at 17:38
  • I was about to delete my answer, but I think this discussion will be useful to others since the inclination to automatically convert "Chr" to a "(char)" cast and "Asc" to "Convert.ToInt32" is very common and people should always re-examine whether this will work. (By the way, I upvoted your answer). – Dave Doknjas Sep 07 '13 at 17:50
  • Bringing some attention to this issue will certainly be interesting as far as it is very easy to mix these things up; and it is really nice from you to think in this way. On the other hand, it might not be too positive for your own interests (eventual down-votes; although perhaps not with these comments). Do whatever you think that is better. (I thought so. Thanks. Sorry but I cannot pay you back) – varocarbas Sep 07 '13 at 17:57
  • Personally you all get an A++ for this conversation as far as I'm concerned and I'm going to vote you all up. – dcaswell Sep 07 '13 at 23:18
  • A little follow-up: after more thorough testing, it appears that "AscW" (but not "Asc") can reliably be converted to "Convert.ToInt32" and "ChrW" (but not "Chr") can reliably be converted to a "(char)" cast. – Dave Doknjas Sep 08 '13 at 01:08
0

pretty much but you need to declare the I variable in your for loop. Correction:

for (int I = 0; I <= Part1.Length-1; I++)
 {
...

 }
Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72