-4

Hi I have to convert my project from java to C# and i need help with below lines to convert them into c#. Please look at below image and code which i have typed here.

 for (String profile : userProfiles)

    {
        int maxuser = 100;
        if (profile.equals("astd"))
            maxuser = 300;

        for (String suffix : suffixes)
        {
            for (int i = 0; i <= maxuser; i++)
            {

                String prefix = profile;
                System.out.println("prefix" + prefix);
                String num = Integer.toString(i);
                if (num.length() < 2)
                    num = "0" + num;
                String postfix = num;

                String username = prefix + postfix + suffix;
                System.out.println(username);

                //TODO add a

                Gson gson = new Gson();
                User u = new User();
                u.setFirstName(username

enter image description here

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
vic
  • 217
  • 1
  • 7
  • 18

4 Answers4

2

Not only foreach; there are lot more difference in the given code, i will specify some of them here:

  1. String and string are different in c#
  2. use foreach instead for for(:)
  3. use Console.WriteLine() to print
  4. for converstion use .ToString() instead for Integer.toString

Here is the corrected code

foreach (string profile in userProfiles)
        {
            int maxuser = 100;
            if (profile.Equals("astd"))
                maxuser = 300;
            foreach (string suffix in suffixes)
            {
                for (int i = 0; i <= maxuser; i++)
                {
                    string prefix = profile;
                    Console.WriteLine("prefix" + prefix);
                    string num = i.ToString();
                    if (num.Length < 2)
                        num = "0" + num;
                    string postfix = num;
                    string username = prefix + postfix + suffix;
                    Console.WriteLine(username);
                    //TODO add a
                    Gson gson = new Gson();
                    User u = new User();
                    u.setFirstName(username);
                }
            }
        }
Community
  • 1
  • 1
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • 2
    spoon fed....IMHO, we should not encourage such questions....We should try to guide OP first to try out on his own.. – Viru Mar 31 '16 at 06:40
  • that's why i have included reference links in the answer. And i believe this is not a tuition class to give step by step guidelines – sujith karivelil Mar 31 '16 at 06:43
  • 1
    But you have done opposite of what you are saying...OP has complete code which he can copy paste and done. – Viru Mar 31 '16 at 06:45
  • I understand it is not tuition but again it is not code writing service...There should be some effort from OP first – Viru Mar 31 '16 at 06:46
  • So what you are trying to say? not need to provide answer only provide guidelines, isn't it? – sujith karivelil Mar 31 '16 at 06:47
  • you have complete right to provide direct answers or answers with guidelines or only guidelines...but for sake of community, Ideal would be asking OP to try first and then working on what OP has tried... – Viru Mar 31 '16 at 06:49
1

Equivalents:

Java

for (String profile : userProfiles)

C#

foreach (string profile in userprofiles)

Java

System.out.println("text");

C#

Console.WriteLine("text");

Java

Integer.toString(i);

C#

i.ToString();

Java

num.length()

C#

num.Length
diiN__________
  • 7,393
  • 6
  • 42
  • 69
0

From this: for (String profile : userProfiles)

to this: foreach(String profile in userProfiles)

From this:

if (profile.equals("astd"))
     maxuser = 300;

To this:

if(profile == "astd")
   maxuser=300;

From this: System.out.println("prefix" + prefix);

To this: Console.WriteLine("prefix" + prefix); //assuming you're doing a console app

Lastly your num.length() should just be num.length

JC Borlagdan
  • 3,318
  • 5
  • 28
  • 51
0

You can use the tools available,although it is better to do it manually. So you use tools to get the syntax right atleast

Here are some reference

Where can I find a Java to C# converter?

How should I convert Java code to C# code?

Community
  • 1
  • 1