C
C#ā€¢5mo ago
Deleted user

āœ… Self learning c#

self teaching my self c#
No description
162 Replies
Deleted user
Deleted userOPā€¢5mo 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
Deleted user
Deleted userOPā€¢5mo ago
how do i do that?
Angius
Angiusā€¢5mo 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ā€¢5mo 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ā€¢5mo ago
anything coming from a user should obviously not be trusted
Deleted user
Deleted userOPā€¢5mo ago
int withDrawl = int.Parse(Console.ReadLine()); this?
The Fog from Human Resources
Yes!!!
Pobiega
Pobiegaā€¢5mo 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ā€¢5mo 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
Deleted user
Deleted userOPā€¢5mo ago
No description
Deleted user
Deleted userOPā€¢5mo ago
? how to do TryParse
Angius
Angiusā€¢5mo ago
$tryparse
MODiX
MODiXā€¢5mo 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ā€¢5mo 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
Deleted user
Deleted userOPā€¢5mo ago
No description
Pobiega
Pobiegaā€¢5mo ago
whats the purpose of that second if?
Deleted user
Deleted userOPā€¢5mo ago
i just didnt delete it
Pobiega
Pobiegaā€¢5mo ago
if (a == a) is a fairly silly comparison
The Fog from Human Resources
You can never be too sure.
Angius
Angiusā€¢5mo 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ā€¢5mo ago
lets not
Deleted user
Deleted userOPā€¢5mo ago
No description
Pobiega
Pobiegaā€¢5mo ago
that'll work - but please fix your formatting šŸ˜„
Deleted user
Deleted userOPā€¢5mo ago
can u show me proper formatting?
Angius
Angiusā€¢5mo ago
$prettycode
MODiX
MODiXā€¢5mo 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ā€¢5mo ago
ctrl+k, ctrl+d
Angius
Angiusā€¢5mo 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ā€¢5mo ago
well yeah, the VS keyboard shortcut menu
The Fog from Human Resources
Nono I mean for the bot
Pobiega
Pobiegaā€¢5mo ago
go to mod.gg its there, called "tags"
Deleted user
Deleted userOPā€¢5mo ago
No description
Pobiega
Pobiegaā€¢5mo ago
much better.
Deleted user
Deleted userOPā€¢5mo ago
doesnt look much different lmao
Angius
Angiusā€¢5mo ago
Would've looked better with braces
Pobiega
Pobiegaā€¢5mo 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ā€¢5mo ago
Gotta log in with Discord IIRC
Pobiega
Pobiegaā€¢5mo ago
yep
Pobiega
Pobiegaā€¢5mo ago
should be a "log in" button in the top right
The Fog from Human Resources
Yes yes
Pobiega
Pobiegaā€¢5mo ago
fair warning, there are 941 tags atm so you have a bit of browsing to do
The Fog from Human Resources
:SCshocked:
Deleted user
Deleted userOPā€¢5mo 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
Deleted user
Deleted userOPā€¢5mo ago
heres code i got rn
Angius
Angiusā€¢5mo ago
We have a lot of useful and less-so tags lol $chat
Pobiega
Pobiegaā€¢5mo ago
got some unused using statements. your IDE should be able to clean those up for you
Deleted user
Deleted userOPā€¢5mo 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ā€¢5mo ago
as said, the IDE can do it for you. Just click one of the gray ones and pick "remove unused usings"
Deleted user
Deleted userOPā€¢5mo ago
ok
Pobiega
Pobiegaā€¢5mo ago
I'd recommend removing your thread.sleeps thou artificial pauses are just annoying
Deleted user
Deleted userOPā€¢5mo ago
okay its so text can be read
Pobiega
Pobiegaā€¢5mo 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
Deleted user
Deleted userOPā€¢5mo ago
No description
Deleted user
Deleted userOPā€¢5mo 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
Deleted user
Deleted userOPā€¢5mo ago
No description
Deleted user
Deleted userOPā€¢5mo ago
how do i make it where i text on same line right after the : it makes me type under
Angius
Angiusā€¢5mo 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ā€¢5mo ago
The latter appends a newline at the end of the string
Deleted user
Deleted userOPā€¢5mo ago
okay good got that looking better, now how can i make it make mutliple ways of spelling in the string
No description
Deleted user
Deleted userOPā€¢5mo ago
string Mode1 = ["word1" , "word2"] like that?
Angius
Angiusā€¢5mo 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
Deleted user
Deleted userOPā€¢5mo ago
No description
Deleted user
Deleted userOPā€¢5mo ago
found this way
Angius
Angiusā€¢5mo ago
Ah, that's what you mean Then yeah, an array can store multiple values
Deleted user
Deleted userOPā€¢5mo ago
the IDE has an area to see github examples which helps
Deleted user
Deleted userOPā€¢5mo ago
No description
Deleted user
Deleted userOPā€¢5mo ago
well now underlined
Angius
Angiusā€¢5mo 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ā€¢5mo 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
Deleted user
Deleted userOPā€¢5mo ago
No description
Deleted user
Deleted userOPā€¢5mo ago
got it
Pobiega
Pobiegaā€¢5mo 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
Deleted user
Deleted userOPā€¢5mo ago
i know im making this have alot of crap trying to force my self to memorize this stuff
Pobiega
Pobiegaā€¢5mo ago
thats fine. practice is very important
Deleted user
Deleted userOPā€¢5mo ago
Ye I like how this auto goes to exe too python could never
The Fog from Human Resources
Python Bad :soPortuguese:
Deleted user
Deleted userOPā€¢5mo ago
tired of using that lang useless
The Fog from Human Resources
Yes!!! Become CSharper Make life easier.
Pobiega
Pobiegaā€¢5mo 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
Deleted user
Deleted userOPā€¢5mo ago
i really wanted to learn c++ too complicated
Deleted user
Deleted userOPā€¢5mo 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ā€¢5mo ago
agreed
The Fog from Human Resources
Python is a very lovable language indeed but it's so horribly taught :SCgetoutofmyhead:
Angius
Angiusā€¢5mo 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:
Deleted user
Deleted userOPā€¢5mo ago
i only used python for Discord bots
The Fog from Human Resources
What's LOGO :thinker:
Pobiega
Pobiegaā€¢5mo 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ā€¢5mo ago
we have a dedicated channel for discord bot development #discord-dev
Angius
Angiusā€¢5mo 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ā€¢5mo ago
LIFT PEN Yeah
Deleted user
Deleted userOPā€¢5mo ago
Writing a discord bot in C# would be weird... love it
Pobiega
Pobiegaā€¢5mo ago
I personally am a huge proponent of the Remora discord library, but its... fairly technical
Angius
Angiusā€¢5mo 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ā€¢5mo ago
Eclipse or Netbeans?
Pobiega
Pobiegaā€¢5mo 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ā€¢5mo 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:
Deleted user
Deleted userOPā€¢5mo ago
how do i call this?
No description
Pobiega
Pobiegaā€¢5mo ago
wdym?
Deleted user
Deleted userOPā€¢5mo ago
No description
Pobiega
Pobiegaā€¢5mo ago
You don't manually call Main, ever
Deleted user
Deleted userOPā€¢5mo ago
How should i go about restarting?
Angius
Angiusā€¢5mo ago
A loop, usually
Pobiega
Pobiegaā€¢5mo ago
with a loop
Angius
Angiusā€¢5mo ago
A while(true) for example Or better, a
var running = true;
while (running)
{
// do stuff
}
var running = true;
while (running)
{
// do stuff
}
Deleted user
Deleted userOPā€¢5mo ago
okay i see what about colors for specfic text
Angius
Angiusā€¢5mo ago
Console.ForegroundColor IIRC
Deleted user
Deleted userOPā€¢5mo ago
nonono
Pobiega
Pobiegaā€¢5mo ago
yes yes yes
Deleted user
Deleted userOPā€¢5mo ago
like for example i only want the int green and other text on same line default
Angius
Angiusā€¢5mo ago
Huh? Ah Well
Pobiega
Pobiegaā€¢5mo 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ā€¢5mo 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ā€¢5mo ago
I'd write a helper method
Angius
Angiusā€¢5mo ago
Yeah Or Spectre.Console
Deleted user
Deleted userOPā€¢5mo ago
like word blaze to be green it would be red
Pobiega
Pobiegaā€¢5mo 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ā€¢5mo ago
Then like what I wrote Or like Pobiega's helper method way Or with the Spectre.Console package
Deleted user
Deleted userOPā€¢5mo ago
No description
Pobiega
Pobiegaā€¢5mo 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ā€¢5mo ago
Yes, I know what you mean My answer stands
Pobiega
Pobiegaā€¢5mo ago
and I agree with you šŸ™‚
Pobiega
Pobiegaā€¢5mo ago
No description
Pobiega
Pobiegaā€¢5mo ago
just to demonstrate how it could look with a helper
Deleted user
Deleted userOPā€¢5mo ago
No description
Deleted user
Deleted userOPā€¢5mo ago
got it
Pobiega
Pobiegaā€¢5mo ago
don't forget to reset the colors
Deleted user
Deleted userOPā€¢5mo ago
i did
Pobiega
Pobiegaā€¢5mo 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
Deleted user
Deleted userOPā€¢5mo ago
I just wanted to add it for testing but ill remeber that for later
Deleted user
Deleted userOPā€¢5mo ago
doing t he withdrawl rn
No description
Deleted user
Deleted userOPā€¢5mo ago
i think math is fine idk if theres better ways
Pobiega
Pobiegaā€¢5mo ago
you are not saving the new balance there and you can also withdraw more than you have or even a negative number
Deleted user
Deleted userOPā€¢5mo ago
okay
Deleted user
Deleted userOPā€¢5mo ago
No description
Pobiega
Pobiegaā€¢5mo 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
Deleted user
Deleted userOPā€¢5mo ago
No description
Deleted user
Deleted userOPā€¢5mo ago
this work?
Pobiega
Pobiegaā€¢5mo ago
no, you are testing the same thing twice you dont actually test for negative withdrawal
Deleted user
Deleted userOPā€¢5mo ago
iā€™m so hungover ima take a quick break from my pc
Want results from more Discord servers?
Add your server