185 Replies
its because you are using top level statements
dont use TLS if you are planning on declaring methods in your Program.cs file
idk what that means
oh wait
do this:
inside
Main
is where you write your code, and you declare your methods in Program
oh i thought i chose the thingie that didnt need that
7.0 something
but ty
you did
TLS = top level statements
sooo NOT to do this..
I'm gonna need you to actually read what is written
Pobiega
inside
Main
is where you write your code, and you declare your methods in Program
Quoted by
<@105026391237480448> from #okay what the flip (functions n stuff..) (click here)
React with β to remove this embed.
oh wait
yea o remeber
you are declaring
div
inside Main
my bad
And when you do declare div don't include the
;
at the end of the declaration, i.e. do:
not:
It's also important to remember that indentation in C# doesn't do anything. You're missing a pair of
{}
around the code inside your if statement. aight so few changes
but still error
looks better now
However your method doesn't return anything
ah
i knew return had some use but didnt know it was necessary
You specified that your method should return
int
And also, a method handling division should really not be writing to the console.can i js
at the end of else
oh
hmm
Now you're gonna always return 0
...
ye i figured that wasnt right
that looks much better
Div still aint fuckin w me tho
neither is count rn as well
What does the error say?
ig maybe i should move int count = 0; outside of the function but then it will be permanently changed when we increment count.. no?
that is... odd
one might say its..
Do you have any other files in your project?
nope
could it have to be anything with the "if" not returning anything?
but im thinking it shouldnt have to
no that error only occurs if you have two identical methods in the same class
try restart VS perhaps, could be a bug
well well well it actually worked
wait bru why this aint green
other button pulls up whatever this is
you didn't open a project it looks like
you must open it via the csproj or sln file
not a folder, or .cs file
oh ye i saved it as .cs n just clicked on it
bruh i thought closing the other auto created file would just run this one
?
i made a new project
it automatically made a program.cs
i opened this file, closed program.cs
then tried to run it π
Are you surprised that running another project did not run your current project
opening a file in VS has almost no meaning
you open a project / solution containing one or more projects
π¬
alright
noted
so how does one usually run a file to see if it works? just copy paste it over to Program?
either a throwaway project / Rider / LinqPad
what r the last 2
Rider is an IDE like VS
LinqPad is a sorta small tool for running code / interacting with database
None of which are free
oh zamn
RoslynPad is free I think
thought LinqPad had a free tier also
now this might be js me, im new so idk how much i can say bout this but it seems that a feature that allows to run just a file is a rather effecient one, no?
so much easier for testing
for me personally, yes
yes, but C# doesn't work like that
Although you can have script files
its only really needed when you are brand new
its a program made for programmers, surely with all the capabilities the world of coding has to offer, they could figure a way out π€
You don't understand
i realized that too, but it would still make a neat feature, no? π€
most likely
This isn't javascript, or python.
It's not that they haven't figured it out, it's just that the way C# works, it's just not a thing
well anyways now there is this problem
i tried to copy the in count = 0; to outside of the function
but still didnt work
You declared
count
in Div
, it doesn't exist in Main
ye i figured byt even when i put it outside
it doesnt fix it
unless it explicitly needs to be in main?
which should still work since we call the function in main..?
yup it worked
ty
calling a method doesnt "copy" paste the code into the calling
wait
What did you change exactly?
nothing :/
before
after
now its just in program file
divider.cs
is in Miscellaneous filesAgain you declared
count
in Div
, it doesn't exist in Main
ye but i also put it in mnay other palces n stll caused problems
so i js ctrl + z
ok so i made the code compilable
but
π
it simply doesnt work
show the code
because
ok so i made the code compilableprobably means "I moved shit around until it compiles, but makes no logical sense"
You probably want to return two things from
Div
, the final number and how many times it was divided.ye thats what im trying
well i changed the else here to else if
but now its not compilable
this is the error
Okay, its apparent that you don't understand variable scopes
the
count
variable declared inside Div
is not in any way related to the one in Main
they are separate, unrelated variables that just have the same nameye i suspected that but i had no clue how to fix
Well, look at what Thinker said again
ohhh
i understood it differently
You probably want to return two things from Div
, the final number and how many times it was divided.
since both count
and number
(after division) are local to Div
, the only way to access them is to return both of them in one way or anotherthis feels wrong
(and well is wrong)
its very close to right thou
does it like.. mean it will still only retunr one value?
"gets the value.. ..second element"
you're getting ahead of yourself. Fix the "count does not exist in the current context" error first.
well i dont get how, but if i have to link it to previous mistakes then i would return it in the "if" case as well..?
look at where
count
is defined
its in a different scope than your returnidk anymore
the red row is declared in the green scope
but you are trying to access it in the yellow scope
i changed a bunch
now its in the parameter of the function
bro
Have you considered not just randomly changing things?
π¦
You need to understand variable scope
stop trying to avoid that
also,
int
s are value types and mutating it in a method doesnt do anything
I can give you 10-15 minutes of VC to demonstrate the issueim in a study quitly section, with no headphones...
Β―\_(γ)_/Β―
your loss
also i understand the meaning of variabel scope, but i cant figure it out completely
Pobiega
Quoted by
<@105026391237480448> from #okay what the flip (functions n stuff..) (click here)
React with β to remove this embed.
I mean you 100% ignored this
and the microdoft description is a bit weird
i do see what u mean but i dont know how to fix
if i define count there, wouldnt it be not effect but the "if" statment?
What scope covers both green and yellow?
$scopes
thing a
is available in scope A
and scope B
thing b
is available only in scope B
the one for the whole function..?
yes
bro i did that before n still didnt work
look
you have like... 12321512343 issues in your code right now
you can't expect fixing one to fix the other 12321512342
dam
i see
well uhm
is thsi what u meant?
yes
okay, now remove the
int count
from your method parameters
and to return two values, we can't have the return type set to int
can we?
also, you 100% have a bug in the logic of this method. I don't know what its supposed to do, but I can assure you its not working ;Di uhm didnt know that
it did work once and it indeed did not do what i thought it would do
and broke it while trying to fix
public static int Div(...
means the return type is int
that means your method can and MUST return one int, and one int onlyoh
if you want to return two values, you'd have to indicate that in your return type
ye teach said tmr we will go though that part of functions
right now i use them without much knowledge i must admit
okay I'll just spoonfeed you the returntype for now then
(int number, int count)
thats a tuple with two named integersthat would be an array.
not a great way to return 2 values
great for returning 0..N values
ye ngl i took it from google cuz idk
it did not sit w me that it was an array but
use the tuple
$tuple
?
dam they dont got one
my memory is a bit weak related to tuples
more like your reading skills? O_O
ah
π
A tuple in this case is two values grouped together
And in this case both also have their own name
Also, I find it somewhat funny we're 3 swedes at this point π
πΈπͺ
yep
i must run now to have enough time to eat lunch
om nom
nah im js eating some random pizza
cuz its 60kr
kinda a steal
i cant even guess what this means
why does it wanna convert from int to int
you can't return a single value when you've set your return type to be two values
dont make sense
its not
its saying
int
to (int number, int count)
ah
oh
you really need to pay more attention lol
but what if when one of the if statments happen i only care about one value
wait nvm then i js dont need to usae the other
you still need to return a valid value
if you want to allow returning only one of the values, then use nullable ints
or better, rewrite the method :p
there was this now but i fixed it myself π§
i think
i did want 2
well
i did start off without count
then added it
YOO i solved the other problem
yea...
mhm π
your method is calling itself, and never stops
that tends to lead to that
also, I doubt you want to pass in count as a parameter
maaaaaaaan π
why does it never stop
say we got 8
it becoimes 4
then 2
then 1
then the "if" stament never happens and we go to else
use the debugger and find out
stack overflow is almost guraranteed to be the recursion thou
hmm
how u use the debugger again
$debug
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
bruh i knew this had some debugger thing idk why i thought u mean tsum else
well this doesnt do much is just
Well, you'll have to fix the errors
and this what heppens if i ru nthe latest version
i thought the debubber would help w that
the debugger will help with logical errors, not syntax errors
got clue what this means
its same as before
ill try restarting again, tho i alr tried that
got more problems now π
im assuming this is without top level statment
but i am using them
which is what the problem is
?
do you have any other files
nope
loyal to only one file π
again, odd
sooo its not this?
i thought i solved my first error on my own
so this works, ive seen how chat gpt made a much better code tho
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.