Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Help understanding code

marba

Best Answer WingedPanther73, 26 May 2015 - 08:25 AM

The assumption is that you'll have something like the following in cInputt: "convert 15#1"

index1 is the location of the " ", right before the 15#1, 8 (probably)

index2 is the location of the "#" in the middle of 15#1, 11(probably)

Subtring(index1,index2-index1) ends up being (in this case) Substring(8,3), so it grabs 3 characters starting at position 8.

So s1 ends up being "15" (" 15" after trimming)

s2 ends up being "1" (the characters 1 position after #)

Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 Marba

Marba

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 26 May 2015 - 05:17 AM

Can someone explain me in details this method ?! It would be very helpful for me :)

 

 

public string Convert(string cInputt)

        {

            int index1 = cInputt.IndexOf(" ");

            int index2 = cInputt.IndexOf("#");

            string s1 = cInputt.Substring(index1, index2 - index1).Trim();

            string s12 = cInputt.Substring(index2 + 1).Trim();

            double Nr1;

            double Nr2 = int.Parse(s12);

            string sas = "";

            if (s1 == "1")

            {

                Nr1 = Nr2;

                Nr2 = 0.39 * Nr2;

                sas = "In " + Nr1.ToString() + " cm has " + Nr2.ToString() + " inches";

            }

            else

                if (s1 == "2")

                {

                    Nr1 = Nr2;

                    Nr2 = 25.4 * Nr2;

                    sas = "In" + Nr1.ToString() + " inches has  " + Nr2.ToString() + " cm";

                }

                else

                    if (s1 == "3")

                    {

                        Nr1 = Nr2;

                        Nr2 = 0.62 * Nr2;

                        sas = "In " + Nr1.ToString() + " km has " + Nr2.ToString() + " milie";

                    }

                    else

                        if (s1 == "4")

                        {

                            Nr1 = Nr2;

                            Nr2 = 1.60 * Nr2;

                            sas = "In" + Nr1.ToString() + " milie has " + Nr2.ToString() + " km";

                        }

                        else

                            if (s1 == "5")

                            {

                                Nr1 = Nr2;

                                Nr2 = 0.3348 * Nr2;

                                sas = "In " + Nr1.ToString() + " feet has " + Nr2.ToString() + " metres";

                            }

                            else

                                if (s1 == "6")

                                {

                                    Nr1 = Nr2;

                                    Nr2 = 2.98 * Nr2;

                                    sas = "In " + Nr1.ToString() + " metres has " + Nr2.ToString() + " feet";

                                }

 

            return sas;

        }



#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 26 May 2015 - 06:45 AM

We're missing some information, but it basically checks what conversion you want to do, does that conversion, and prints the results. Can you be more precise about which lines don't make sense?


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Marba

Marba

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 26 May 2015 - 07:10 AM

   int index1 = cInputt.IndexOf(" ");

            int index2 = cInputt.IndexOf("#");

            string s1 = cInputt.Substring(index1, index2 - index1).Trim();

            string s12 = cInputt.Substring(index2 + 1).Trim();

 

I can't understand the third sentence.. " cInputt.Substring(index1, index2-index1).Trim(); "  



#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 26 May 2015 - 08:25 AM   Best Answer

The assumption is that you'll have something like the following in cInputt: "convert 15#1"

index1 is the location of the " ", right before the 15#1, 8 (probably)

index2 is the location of the "#" in the middle of 15#1, 11(probably)

Subtring(index1,index2-index1) ends up being (in this case) Substring(8,3), so it grabs 3 characters starting at position 8.

So s1 ends up being "15" (" 15" after trimming)

s2 ends up being "1" (the characters 1 position after #)


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 Marba

Marba

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 26 May 2015 - 10:48 AM

Thank you for your help :) you are a life saver.  :thumbup:






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download