G+_Pete Murphy Posted February 16, 2014 Share Posted February 16, 2014 I have a bit of a question, is there a place I can post to ask for a discussion on certain things I just find interesting in the code but are not really part of the ongoing discussion (yet) as Parde and Shannon are quite rightly so keeping it simple (which is fantastic) . For example all those include statements at the start of the code or just to discuss what that TryParse statement is actually doing? I mean I am looking for a place for us to just discuss things in the coding101 community as a fun thinking area. - Would this be the place or perhaps there is a forum site somewhere for just general tangent discussions? forgive me for this but I am a bit of a Newbie when it comes to all this, I am embarrassed to say it but I am still trying to work out how Facebook and Twitter actually work ;-) Link to comment Share on other sites More sharing options...
G+_Shannon Morse Posted February 16, 2014 Share Posted February 16, 2014 Never be embarrassed to speak out when you don't understand something. If I didn't ask questions I wouldn't be where I am today. That question you're itching to ask usually ends up being the one everyone else is embarrassed to ask. They don't want to look stupid, but you end up looking confident. ;) I'm gonna work on some subsections for discussions like this during this work week. The Google+ community is the best place to post things like this :) Link to comment Share on other sites More sharing options...
G+_John Sullivan Posted February 16, 2014 Share Posted February 16, 2014 Hi Pete, Did you get answers to your other questions? I'm not sure what you mean by "includes", but if you're asking about the "using" statements at the top of the code page, those are just areas of pre-defined terms that the computer can lookup. For instance, if you type: int or bool or string or something, the computer doesn't really know what they are. But it can look up those terms in the using System; (and other areas) so that it knows what you're talking about. As for TryParse, I asked about that also, but Lou says he'll explain it later. I've read a couple of books on C# and had not seen that, so Googled for it. Parse is when you try to change one data type into another, like changing a string into an integer. TryParse just tells the computer to try it. There's a built-in TryParse option for the different data types, so you can just type: int.TryParse and the computer will understand. Lou uses a "!" to mean "not", and his line of code is saying something like "try to change this string of text into an integer, and if you can't, then show the following error message. That's the best explanation I can give at this point. Hope it helps. Link to comment Share on other sites More sharing options...
G+_Pete Murphy Posted February 17, 2014 Author Share Posted February 17, 2014 Just a note of thanks John, you understood me perfectly about including the using statements at the start, I did also do a bit of a google search about the TryParse command but found most of the answers to be too technical. Your explanation is pretty good. Many thanks for taking the time to help me out with this - Pete Link to comment Share on other sites More sharing options...
G+_Cosmic Ray Posted February 17, 2014 Share Posted February 17, 2014 For anyone who might be interested in this, perhaps I can add a bit: Lou uses TryParse in a couple of his examples, one of which is in Episode 2, about 29 minutes and 15 seconds in. He is trying to convert a string (string of text) into an integer (a whole number, no fraction or decimal) He uses Console.ReadLine() to get the input from the User, which he calls "enteredNumber". This is a string, and he wants to convert it to an integer, which he will call "wholeNumber". What's interesting about his code is that he also wants to check to see if the User's input string can be converted to an integer (that is, is it a valid input). If not, he sends an error message back to the User. He uses int.TryParse, which simply means (Try to Parse this into an Integer) He tells the computer to try to Parse the User's input "enteredNumber" into an integer variable called "wholeNumber". ( Notice that wholeNumber is an int that he defined in the line above TryParse.) But he is also putting the conversion in an IF statement, and is adding a " ! " before it. So he is saying " Try to convert enteredNumber string into an integer. If you can, then call it "wholeNumber". If you can NOT convert it, then show an error message. The error that he wants to show to the User is enclosed in { } below that. Notice the word "out" in his TryParse statement, which tells the computer to use the variable "wholeNumber" which he had defined in the line above TryParse. Link to comment Share on other sites More sharing options...
G+_Pete Murphy Posted March 5, 2014 Author Share Posted March 5, 2014 I do have one follow up question though – why do I have to go to all that effort to get an integer from the user where I first get a string then sanitize it and use TryParse to turn it into an integer. It just seems like a lot of extra steps. I mean, this seems to me to be really the long way around. I understand that you need to do this for good practice reasons to make sure someone doesn’t break your code (as Padre said by typing, for example , ‘Balloon‘ instead of actually entering a whole number as expected) but why does it appear that C# is so strict on this point. I mean I was pulling my hair out trying to get the code correct and getting frustrated that this below just would not work…. Console.Write("\nPlease enter an integer value: "); int enteredNumber = Console.ReadLine(); As say I understand the need to check/scrutinize/sanitize entered values before proceeding but what if, for example, if I somehow trusted the user to not make mistakes or enter the wrong value – like the ‘user’ was actually another computer such as a calculator or some other software that was guaranteed to return an integer value? I would just like to know why I spend a couple of days struggling with why the simple code above would not work? (I would have expected it would have still worked even if it was indeed bad coding practice). Once again many thanks in advance. Pete Link to comment Share on other sites More sharing options...
G+_Pete Murphy Posted March 5, 2014 Author Share Posted March 5, 2014 oops - posted that to the wrong place - this was a follow up question to a different post :-) Link to comment Share on other sites More sharing options...
Recommended Posts