C
C#ā€¢3mo ago
Crunchy

āœ… Self learning c#

self teaching my self c#
No description
162 Replies
Crunchy
Crunchyā€¢3mo ago
can someone explain the fix here?
The Fog from Human Resources
Console.ReadLine returns a string You would need to convert it to an int first
Crunchy
Crunchyā€¢3mo ago
how do i do that?
Angius
Angiusā€¢3mo ago
int.TryParse() is the most proper way
The Fog from Human Resources
Also you don't need the if statement if you compare the same value This
Angius
Angiusā€¢3mo ago
Followed closely by int.Parse()
The Fog from Human Resources
int.Parse is if you're sure the input is a number TryParse If you want to double check :SCfeet:
Pobiega
Pobiegaā€¢3mo ago
anything coming from a user should obviously not be trusted
Crunchy
Crunchyā€¢3mo ago
int withDrawl = int.Parse(Console.ReadLine()); this?
The Fog from Human Resources
Yes!!!
Pobiega
Pobiegaā€¢3mo ago
well yeah, but if I am a meanie and type "cheese" your program will crash so thats where int.TryParse comes in
The Fog from Human Resources
:SCcheese:
Pobiega
Pobiegaā€¢3mo ago
your program suddenly has whats called a "fail state", or "unhappy flow" there are two cases, the user might enter a good value, or a bad value you need to handle that bad value properly
Crunchy
Crunchyā€¢3mo ago
No description
Crunchy
Crunchyā€¢3mo ago
? how to do TryParse
Angius
Angiusā€¢3mo ago
$tryparse
MODiX
MODiXā€¢3mo ago
When you don't know if a string is actually a number when handling user input, use int.TryParse (or variants, e.g. double.TryParse)
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
TryParse returns a bool, where true indicates successful parsing. Remarks: - Avoid int.Parse if you do not know if the value parsed is definitely a number. - Avoid Convert.ToInt32 entirely, this is an older method and Parse should be preferred where you know the string can be parsed. Read more here
Pobiega
Pobiegaā€¢3mo ago
you seem to be struggling with the concept of return types in general. I highly recommend reading up on what a strict type system is and how to work with it
Crunchy
Crunchyā€¢3mo ago
No description
Pobiega
Pobiegaā€¢3mo ago
whats the purpose of that second if?
Crunchy
Crunchyā€¢3mo ago
i just didnt delete it
Pobiega
Pobiegaā€¢3mo ago
if (a == a) is a fairly silly comparison
The Fog from Human Resources
You can never be too sure.
Angius
Angiusā€¢3mo ago
Should be if ((a == a) == true) then :when:
The Fog from Human Resources
:SCgetoutofmyhead: I wonder how far this can go before it becomes unreadable
Pobiega
Pobiegaā€¢3mo ago
lets not
Crunchy
Crunchyā€¢3mo ago
No description
Pobiega
Pobiegaā€¢3mo ago
that'll work - but please fix your formatting šŸ˜„
Crunchy
Crunchyā€¢3mo ago
can u show me proper formatting?
Angius
Angiusā€¢3mo ago
$prettycode
MODiX
MODiXā€¢3mo ago
To format your code in Visual Studio, Visual Studio Code, Rider, use the following shortcut:
Visual Studio CTRL + K + D
Rider / Resharper CTRL + ALT + L
Visual Studio Code SHIFT + ALT + F
Visual Studio CTRL + K + D
Rider / Resharper CTRL + ALT + L
Visual Studio Code SHIFT + ALT + F
NOTE: the first key must be held while doing it. https://cdn.discordapp.com/attachments/569261465463160900/899513918567890944/2021-10-18_01-26-35.gif
Pobiega
Pobiegaā€¢3mo ago
ctrl+k, ctrl+d
Angius
Angiusā€¢3mo ago
The IDE will do it for you
The Fog from Human Resources
Is there a list for all those speed dial commands.
Pobiega
Pobiegaā€¢3mo ago
well yeah, the VS keyboard shortcut menu
The Fog from Human Resources
Nono I mean for the bot
Pobiega
Pobiegaā€¢3mo ago
go to mod.gg its there, called "tags"
Crunchy
Crunchyā€¢3mo ago
No description
Pobiega
Pobiegaā€¢3mo ago
much better.
Crunchy
Crunchyā€¢3mo ago
doesnt look much different lmao
Angius
Angiusā€¢3mo ago
Would've looked better with braces
Pobiega
Pobiegaā€¢3mo ago
well, your methods closing brace is now properly aligned AND on its own row which it wasnt before so... thats a big improvement for readability
The Fog from Human Resources
No Page access
Angius
Angiusā€¢3mo ago
Gotta log in with Discord IIRC
Pobiega
Pobiegaā€¢3mo ago
yep
Pobiega
Pobiegaā€¢3mo ago
should be a "log in" button in the top right
The Fog from Human Resources
Yes yes
Pobiega
Pobiegaā€¢3mo ago
fair warning, there are 941 tags atm so you have a bit of browsing to do
The Fog from Human Resources
:SCshocked:
Crunchy
Crunchyā€¢3mo ago
PasteCode
bank449494 - PasteCode.dev
PasteCode.dev is a website where you can store any code online for quick sharing. You can paste code and share code online for free.
The Fog from Human Resources
Isn't there a tag for like rule 6 or smth specifically
Crunchy
Crunchyā€¢3mo ago
heres code i got rn
Angius
Angiusā€¢3mo ago
We have a lot of useful and less-so tags lol $chat
Pobiega
Pobiegaā€¢3mo ago
got some unused using statements. your IDE should be able to clean those up for you
Crunchy
Crunchyā€¢3mo ago
yeah i only need using system; and system.Threading; when i made my project it auto added them soo lazy to remove
Pobiega
Pobiegaā€¢3mo ago
as said, the IDE can do it for you. Just click one of the gray ones and pick "remove unused usings"
Crunchy
Crunchyā€¢3mo ago
ok
Pobiega
Pobiegaā€¢3mo ago
I'd recommend removing your thread.sleeps thou artificial pauses are just annoying
Crunchy
Crunchyā€¢3mo ago
okay its so text can be read
Pobiega
Pobiegaā€¢3mo ago
better to have a "press any key to continue" then imho what if 5 seconds wasnt long enough? what if Im ready to continue in 1 sec? press any key works for both of those scenarios, thread.sleep doesnt
Crunchy
Crunchyā€¢3mo ago
No description
Crunchy
Crunchyā€¢3mo ago
il add this to a few areas
The Fog from Human Resources
What if the user isn't ready to continue at all after seeing their bank balance. My brain isn't braining atm but I think this only takes effect when the balance is below 0 So like -1 and such
Crunchy
Crunchyā€¢3mo ago
No description
Crunchy
Crunchyā€¢3mo ago
how do i make it where i text on same line right after the : it makes me type under
Angius
Angiusā€¢3mo ago
Use .Write() not .WriteLine()
The Fog from Human Resources
I think you need to do some shenanigans with Console.Write since WriteLine will break the line
Angius
Angiusā€¢3mo ago
The latter appends a newline at the end of the string
Crunchy
Crunchyā€¢3mo ago
okay good got that looking better, now how can i make it make mutliple ways of spelling in the string
No description
Crunchy
Crunchyā€¢3mo ago
string Mode1 = ["word1" , "word2"] like that?
Angius
Angiusā€¢3mo ago
You could use a string comparison that ignores case Or you could use || for logical or Or you could use or for pattern matching * str.Equals("word", StringComparison.OrdinalIgnoreCase) * str == "word" || str == "WORD" * str is "word" or "WORD" The first one will cover "word", "Word", "wOrd", "woRd", "WOrD", and all other permutations
Crunchy
Crunchyā€¢3mo ago
No description
Crunchy
Crunchyā€¢3mo ago
found this way
Angius
Angiusā€¢3mo ago
Ah, that's what you mean Then yeah, an array can store multiple values
Crunchy
Crunchyā€¢3mo ago
the IDE has an area to see github examples which helps
Crunchy
Crunchyā€¢3mo ago
No description
Crunchy
Crunchyā€¢3mo ago
well now underlined
Angius
Angiusā€¢3mo ago
Well, yeah, can't compare an array of strings to a string, can we?
The Fog from Human Resources
Because you try to compare a string to an array
Angius
Angiusā€¢3mo ago
You can check if an array .Contains() a given element
The Fog from Human Resources
Array probably has a function called Contains or smth
Crunchy
Crunchyā€¢3mo ago
No description
Crunchy
Crunchyā€¢3mo ago
got it
Pobiega
Pobiegaā€¢3mo ago
btw, if you notice that you are doing a certain thing over and over, putting that code in its own method is a great idea. for example, printing a message and then expecting the user to return a value
Crunchy
Crunchyā€¢3mo ago
i know im making this have alot of crap trying to force my self to memorize this stuff
Pobiega
Pobiegaā€¢3mo ago
thats fine. practice is very important
Crunchy
Crunchyā€¢3mo ago
Ye I like how this auto goes to exe too python could never
The Fog from Human Resources
Python Bad :soPortuguese:
Crunchy
Crunchyā€¢3mo ago
tired of using that lang useless
The Fog from Human Resources
Yes!!! Become CSharper Make life easier.
Pobiega
Pobiegaā€¢3mo ago
languages are like tools. some tools are better for certain jobs python is imho a great scripting language, but not a great choice for full on program development
Crunchy
Crunchyā€¢3mo ago
i really wanted to learn c++ too complicated
Crunchy
Crunchyā€¢3mo ago
The Fog from Human Resources
:SCwhatiscatwant: The issue is that it's being shoved down the throats of beginners and then they get overwhelmed by X amount of packages and eventually start to learn third party dependencies more than the actual language
Pobiega
Pobiegaā€¢3mo ago
agreed
The Fog from Human Resources
Python is a very lovable language indeed but it's so horribly taught :SCgetoutofmyhead:
Angius
Angiusā€¢3mo ago
I'll never forget sitting down in the first programming class in college, after having tried some PHP and covering LOGO at school... and starting with fucking pointers :KEKW:
Crunchy
Crunchyā€¢3mo ago
i only used python for Discord bots
The Fog from Human Resources
What's LOGO :thinker:
Pobiega
Pobiegaā€¢3mo ago
yeah I'm pretty glad I already knew C# and PHP before I started with C in uni fucking pointers. and pointer arithmatic
The Fog from Human Resources
You'd probably love Discord.NET or the other lib some find is a better choice but I don't know the name rn
Pobiega
Pobiegaā€¢3mo ago
we have a dedicated channel for discord bot development #discord-dev
Angius
Angiusā€¢3mo ago
You have a turtle that leaves a trail, and you give it instructions like FORWARD 6 ROTATE CLOCKWISE 70
The Fog from Human Resources
Ah that thing
Angius
Angiusā€¢3mo ago
LIFT PEN Yeah
Crunchy
Crunchyā€¢3mo ago
Writing a discord bot in C# would be weird... love it
Pobiega
Pobiegaā€¢3mo ago
I personally am a huge proponent of the Remora discord library, but its... fairly technical
Angius
Angiusā€¢3mo ago
MODiX is open-source if you wanted a peek
The Fog from Human Resources
We use Java in school, it's chill but we use Java 6 or smth and we use a weird ass editor Luckily I got IntelliJ setup on the school laptop and my personal one
Angius
Angiusā€¢3mo ago
Eclipse or Netbeans?
Pobiega
Pobiegaā€¢3mo ago
let me guess, BlueJay?
The Fog from Human Resources
It'll be much easier We had blueJ before Horrible The current one is weirder I don't remember the name Java Editor is the name Just that
Pobiega
Pobiegaā€¢3mo ago
BlueJay is... actually genious, but you shouldnt learn it and only it, since its so... different ftr, bluejay lets you create instances of objects and call methods via the IDE gui, bypassing "Main" entirely great for testing stuff, but horrible for learning how to build actual programs
The Fog from Human Resources
Our current topic is SQL and databases which I'm kinda excited about since our last Java assignment I finished in the first lesson and had nothing else to do for like 4 weeks :SCgetoutofmyhead:
Crunchy
Crunchyā€¢3mo ago
how do i call this?
No description
Pobiega
Pobiegaā€¢3mo ago
wdym?
Crunchy
Crunchyā€¢3mo ago
No description
Pobiega
Pobiegaā€¢3mo ago
You don't manually call Main, ever
Crunchy
Crunchyā€¢3mo ago
How should i go about restarting?
Angius
Angiusā€¢3mo ago
A loop, usually
Pobiega
Pobiegaā€¢3mo ago
with a loop
Angius
Angiusā€¢3mo ago
A while(true) for example Or better, a
var running = true;
while (running)
{
// do stuff
}
var running = true;
while (running)
{
// do stuff
}
Crunchy
Crunchyā€¢3mo ago
okay i see what about colors for specfic text
Angius
Angiusā€¢3mo ago
Console.ForegroundColor IIRC
Crunchy
Crunchyā€¢3mo ago
nonono
Pobiega
Pobiegaā€¢3mo ago
yes yes yes
Crunchy
Crunchyā€¢3mo ago
like for example i only want the int green and other text on same line default
Angius
Angiusā€¢3mo ago
Huh? Ah Well
Pobiega
Pobiegaā€¢3mo ago
[green]500[/] moneys remaining in your [yellow]bank account[/] thats NOT how you do it btw, but thats what you are trying to make, right?
Angius
Angiusā€¢3mo ago
var oldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColore.Green;
Console.Write(420);
Console.ForegroundColor = oldColor;
Console.WriteLine(" blaze it");
var oldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColore.Green;
Console.Write(420);
Console.ForegroundColor = oldColor;
Console.WriteLine(" blaze it");
Is how you'd do it vanilla
Pobiega
Pobiegaā€¢3mo ago
I'd write a helper method
Angius
Angiusā€¢3mo ago
Yeah Or Spectre.Console
Crunchy
Crunchyā€¢3mo ago
like word blaze to be green it would be red
Pobiega
Pobiegaā€¢3mo ago
CC.Write(420, ConsoleColor.Green);
Console.Write(" moneys remaining in your ");
CC.Write("bank account", ConsoleColor.Yellow);
CC.Write(420, ConsoleColor.Green);
Console.Write(" moneys remaining in your ");
CC.Write("bank account", ConsoleColor.Yellow);
Angius
Angiusā€¢3mo ago
Then like what I wrote Or like Pobiega's helper method way Or with the Spectre.Console package
Crunchy
Crunchyā€¢3mo ago
No description
Pobiega
Pobiegaā€¢3mo ago
spectre actually supports the syntax I used above, with the colors inline in the string but you can easily make your own version of it, as demonstrated above
Angius
Angiusā€¢3mo ago
Yes, I know what you mean My answer stands
Pobiega
Pobiegaā€¢3mo ago
and I agree with you šŸ™‚
Pobiega
Pobiegaā€¢3mo ago
No description
Pobiega
Pobiegaā€¢3mo ago
just to demonstrate how it could look with a helper
Crunchy
Crunchyā€¢3mo ago
No description
Crunchy
Crunchyā€¢3mo ago
got it
Pobiega
Pobiegaā€¢3mo ago
don't forget to reset the colors
Crunchy
Crunchyā€¢3mo ago
i did
Pobiega
Pobiegaā€¢3mo ago
ah right still, thats now 3 rows that could be turned into 1 with a simple helper method if you plan to use colors a lot, or just in more than one place, thats a lot of extra rows
Crunchy
Crunchyā€¢3mo ago
I just wanted to add it for testing but ill remeber that for later
Crunchy
Crunchyā€¢3mo ago
doing t he withdrawl rn
No description
Crunchy
Crunchyā€¢3mo ago
i think math is fine idk if theres better ways
Pobiega
Pobiegaā€¢3mo ago
you are not saving the new balance there and you can also withdraw more than you have or even a negative number
Crunchy
Crunchyā€¢3mo ago
okay
Crunchy
Crunchyā€¢3mo ago
No description
Pobiega
Pobiegaā€¢3mo ago
if(withDrawal <= 0) // we can't withdraw negative or zero
{
Console.WriteLine("Invalid withdrawal amount.");
return;
}

if (balance < withDrawal) // we can't withdraw more than we have
{
Console.WriteLine("Insufficient funds.");
return;
}
if(withDrawal <= 0) // we can't withdraw negative or zero
{
Console.WriteLine("Invalid withdrawal amount.");
return;
}

if (balance < withDrawal) // we can't withdraw more than we have
{
Console.WriteLine("Insufficient funds.");
return;
}
you need to handle both these cases
Crunchy
Crunchyā€¢3mo ago
No description
Crunchy
Crunchyā€¢3mo ago
this work?
Pobiega
Pobiegaā€¢3mo ago
no, you are testing the same thing twice you dont actually test for negative withdrawal
Crunchy
Crunchyā€¢3mo ago
iā€™m so hungover ima take a quick break from my pc
Want results from more Discord servers?
Add your server