Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Is there a better way to do this?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 MrMaker   Members   -  Reputation: 128

Like
0Likes
Like

Posted 19 July 2013 - 07:30 PM

Is there a better way of doing this more efficiently and better way? It runs and calculates the area but I think there is a better way.

 

Code Below:

 

namespace LearningCsharp
{
    class Program
    {
        static void Main(string[] args)
        {
           
            Console.WriteLine("Enter length");
 
            string length = Console.ReadLine();
 
            int.Parse(length);
 
            int newLength = Convert.ToInt32(length);
 
            Console.WriteLine("Enter width");
 
            string width = Console.ReadLine();
 
            int.Parse(width);
 
            int newWidth = Convert.ToInt32(width);
 
            Console.WriteLine(newLength * newWidth + "cm");
 
            Console.ReadLine();
        }
    }
}


Sponsor:

#2 Pink Horror   Members   -  Reputation: 1521

Like
0Likes
Like

Posted 19 July 2013 - 07:33 PM

Delete the int.Parse lines.



#3 Nypyren   Crossbones+   -  Reputation: 6095

Like
0Likes
Like

Posted 19 July 2013 - 10:31 PM

Convert.ToInt32(string) and int.Parse(string) are identical. Convert.ToInt32's main use is that you can pass it things besides strings and convert those to an int as well.

Choose one or the other but not both.

#4 NightCreature83   Crossbones+   -  Reputation: 3360

Like
0Likes
Like

Posted 21 July 2013 - 02:03 AM

Also Parse is not exception safe so you would have to use a try catch clause around it. There is a TryParse function as well that is exception safe.


Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS