Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Simple Counting

(Aug 21, 2019 01:06 PM)BeatMatch Wrote:  
Code
void RunMainMenu(string input)
   {
       bool isValidNumber = (input == "1" || input == "2" || input == "3");
       if (isValidNumber)
       {
           level = int.Parse(input);
           AskForPassword();
        }

That's the most disgusting code I've ever read
(Aug 22, 2019 12:36 PM)GraeskMytologi Wrote:  That's the most disgusting code I've ever read

good for you if you can tell me, even what it does or what it calls, considering, there is more to that.
[Image: Inqoz4o.gif]

3D Artist and DJ Check out my portfolio
(Aug 22, 2019 11:23 AM)BeatMatch Wrote:  I seem to be lost considering, my code works umm no sir you are mistaken
lol get off the thread already please
@OP, like i said simply use int.TryParse (or uint if you dont want to allow negatives or need a higher value [or even ulong if you need it])
usage:
Code
bool isNum = int.TryParse("123", out int value);
here you'll have two variables.
  • isNum (a bool value. it'll give a false value if number couldn't be parsed or number is out of 32-bit values (if its one number over/less 2^16))

  • value. if isNum is false it'll result in 0. otherwise it'll result in the output



“I wish I could say everything in one word. I hate all the things that can happen between the beginning of a sentence and the end.” ~ Leonard Cohen
(Aug 23, 2019 08:29 AM)vaiperboy Wrote:  lol get off the thread already please
@OP, like i said simply use int.TryParse (or uint if you dont want to allow negatives or need a higher value [or even ulong if you need it])
usage:
Code
bool isNum = int.TryParse("123", out int value);
here you'll have two variables.
  • isNum (a bool value. it'll give a false value if number couldn't be parsed or number is out of 32-bit values (if its one number over/less 2^16))

  • value. if isNum is false it'll result in 0. otherwise it'll result in the output



use guys are just repeating what has been said lol read all the posts 🤣
[Image: Inqoz4o.gif]

3D Artist and DJ Check out my portfolio
(Aug 22, 2019 12:59 PM)BeatMatch Wrote:  good for you if you can tell me, even what it does or what it calls, considering, there is more to that.

Well it clearly does up to 3 consecutive string comparisons, then parses the input string to an int and calls the AskForPassword() method.
Not sure why that matters, that is still the most disgusting code I've ever read
(Aug 21, 2019 01:03 PM)unknown321 Wrote:  even so while trying this method still getting
System.FormatException: 'Input string was not in a correct format.'
on line
int TotalScore = Int32.Parse(txtScoreTotal.Text);

Make sure you're entering numbers in the box (I know you probably are)

you can try

Convert.ToInt32(someTextBox.Text);
@OP I'm not sure if you have been able to get this to work but I think what may be going on is the text box has not been updated with the text input you put in your gui. That exception occurs when the input text is blank.

Code
decimal score = Convert.ToDecimal("");

the above will cause your exception.

My suggestion is two parts.

1. use the previously noted
Code
if(Double.TryParse(txtScoreTotal.Text, out double number)){
//do stuff
}

and

2. check your bindings because it appears your Text has not updated properly to contain the input you desire.

Good luck!

Edit:

to test if its your input just try to print it out and see its value or breakpoint it in debug mode.
(Aug 24, 2019 02:05 PM)CynicalCheshire Wrote:  @OP I'm not sure if you have been able to get this to work but I think what may be going on is the text box has not been updated with the text input you put in your gui. That exception occurs when the input text is blank.

Code
decimal score = Convert.ToDecimal("");

the above will cause your exception.

My suggestion is two parts.

1. use the previously noted
Code
if(Double.TryParse(txtScoreTotal.Text, out double number)){
//do stuff
}

and

2. check your bindings because it appears your Text has not updated properly to contain the input you desire.

Good luck!

Edit:

to test if its your input just try to print it out and see its value or breakpoint it in debug mode.

still getting same error with the code you told me to try
[Image: cIiTrNH.png]
(Aug 24, 2019 02:05 PM)CynicalCheshire Wrote:  @OP I'm not sure if you have been able to get this to work but I think what may be going on is the text box has not been updated with the text input you put in your gui. That exception occurs when the input text is blank.

Code
decimal score = Convert.ToDecimal("");

the above will cause your exception.

My suggestion is two parts.

1. use the previously noted
Code
if(Double.TryParse(txtScoreTotal.Text, out double number)){
//do stuff
}

and

2. check your bindings because it appears your Text has not updated properly to contain the input you desire.

Good luck!

Edit:

to test if its your input just try to print it out and see its value or breakpoint it in debug mode.

double and decimal are different types

(Aug 22, 2019 08:29 AM)BeatMatch Wrote:  no, why would i do that i was showing him an example of a bool, clearly he is new to coding, hence im not actually just using c# im also running it through a game engine and in only, wanted to use them 3 hence why i use input and not textbox.text because i have declared it somewhere else,

i already told him to try to int pars, but dont you worry about my code bud it runs smooth and does a job what i want it to do obviously there is more missing and thats just a snippet

:/ .. You wouldn't even need compiler optimization to improve code like that. An elementary student could probably optimize it by hand to produce more efficient code.

Doing this for every string representation of a number is not only going to increase conditional checks, it won't validate against arithmetic overflow.

Counting input, sum, and average is a very trivial task. What's wrong with a NumericUpDown control? Also, people who are suggesting to use the Convert class over the Parse()/TryParse() methods don't understand the difference in what these methods are doing behind the scenes.


(Aug 25, 2019 11:58 AM)unknown321 Wrote:  still getting same error with the code you told me to try

If you're getting an error, you're doing something wrong at runtime with your input. What is an example input? "1234" should parse with Parse() and TryParse() with zero issues.

My guess is that you're using the wrong textbox... txtScore.Text instead? TryParse() won't throw an exception either.

One other issue here is that you're using the control's display text to continue with your calculations which is the opposite of how software is normally developed. UI is for display only, you bind data with your controls, or update it when your internal data changes. Parsing data that has been calculated and converted to a string to be displayed on the UI to be taken again, parsed back to the relevant type, and used in other expressions for calculations is wrong.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by systxApr 20, 2017 11:20 PM 18 627 Apr 20, 2017 11:20 PM
Last Post: Goodies
[Shell] by lneidorfMar 4, 2017 05:58 PM 1 126 Mar 4, 2017 05:58 PM
Last Post: b00st3d
[Python] by billybluiceezOct 28, 2016 04:58 PM 6 208 Oct 28, 2016 04:58 PM
Last Post: billybluiceez