C
C#β€’8mo ago
Obada

❔ okay what the flip (functions n stuff..)

so many issues..
No description
185 Replies
Obada
Obadaβ€’8mo ago
No description
Obada
Obadaβ€’8mo ago
No description
Pobiega
Pobiegaβ€’8mo ago
its because you are using top level statements dont use TLS if you are planning on declaring methods in your Program.cs file
Obada
Obadaβ€’8mo ago
No description
Obada
Obadaβ€’8mo ago
idk what that means oh wait
Pobiega
Pobiegaβ€’8mo ago
do this:
public class Program
{
public static void Main()
{

}
}
public class Program
{
public static void Main()
{

}
}
inside Main is where you write your code, and you declare your methods in Program
Obada
Obadaβ€’8mo ago
oh i thought i chose the thingie that didnt need that 7.0 something but ty
SG97
SG97β€’8mo ago
you did TLS = top level statements
Obada
Obadaβ€’8mo ago
sooo NOT to do this..
No description
Pobiega
Pobiegaβ€’8mo ago
I'm gonna need you to actually read what is written
MODiX
MODiXβ€’8mo ago
Pobiega
inside Main is where you write your code, and you declare your methods in Program
Quoted by
React with ❌ to remove this embed.
Obada
Obadaβ€’8mo ago
oh wait yea o remeber
Pobiega
Pobiegaβ€’8mo ago
you are declaring div inside Main
Obada
Obadaβ€’8mo ago
my bad
SinFluxx
SinFluxxβ€’8mo ago
And when you do declare div don't include the ; at the end of the declaration, i.e. do:
public static int div()
{
}
public static int div()
{
}
not:
public static int div();
{
}
public static int div();
{
}
Thinker
Thinkerβ€’8mo ago
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.
if (number % 2 == 0)
{
number = number / 2;
div(number);
count++;
}
if (number % 2 == 0)
{
number = number / 2;
div(number);
count++;
}
Obada
Obadaβ€’8mo ago
aight so few changes
No description
Obada
Obadaβ€’8mo ago
but still error
Thinker
Thinkerβ€’8mo ago
looks better now However your method doesn't return anything
Obada
Obadaβ€’8mo ago
ah i knew return had some use but didnt know it was necessary
Thinker
Thinkerβ€’8mo ago
You specified that your method should return int And also, a method handling division should really not be writing to the console.
Obada
Obadaβ€’8mo ago
can i js
No description
Obada
Obadaβ€’8mo ago
at the end of else oh hmm
Thinker
Thinkerβ€’8mo ago
Now you're gonna always return 0
Obada
Obadaβ€’8mo ago
...
No description
Obada
Obadaβ€’8mo ago
ye i figured that wasnt right
Thinker
Thinkerβ€’8mo ago
that looks much better
Obada
Obadaβ€’8mo ago
Div still aint fuckin w me tho neither is count rn as well
Thinker
Thinkerβ€’8mo ago
What does the error say?
Obada
Obadaβ€’8mo ago
ig maybe i should move int count = 0; outside of the function but then it will be permanently changed when we increment count.. no?
Obada
Obadaβ€’8mo ago
No description
Thinker
Thinkerβ€’8mo ago
that is... odd
Obada
Obadaβ€’8mo ago
one might say its..
No description
Thinker
Thinkerβ€’8mo ago
Do you have any other files in your project?
Obada
Obadaβ€’8mo ago
nope could it have to be anything with the "if" not returning anything? but im thinking it shouldnt have to
Thinker
Thinkerβ€’8mo ago
no that error only occurs if you have two identical methods in the same class try restart VS perhaps, could be a bug
Obada
Obadaβ€’8mo ago
well well well it actually worked
No description
Obada
Obadaβ€’8mo ago
wait bru why this aint green
No description
Obada
Obadaβ€’8mo ago
other button pulls up whatever this is
No description
Pobiega
Pobiegaβ€’8mo ago
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
Obada
Obadaβ€’8mo ago
oh ye i saved it as .cs n just clicked on it
Obada
Obadaβ€’8mo ago
bruh i thought closing the other auto created file would just run this one
No description
Pobiega
Pobiegaβ€’8mo ago
?
Obada
Obadaβ€’8mo ago
i made a new project it automatically made a program.cs i opened this file, closed program.cs then tried to run it πŸ’€
Thinker
Thinkerβ€’8mo ago
Are you surprised that running another project did not run your current project
SG97
SG97β€’8mo ago
opening a file in VS has almost no meaning you open a project / solution containing one or more projects
Obada
Obadaβ€’8mo ago
😬 alright noted so how does one usually run a file to see if it works? just copy paste it over to Program?
SG97
SG97β€’8mo ago
either a throwaway project / Rider / LinqPad
Obada
Obadaβ€’8mo ago
what r the last 2
SG97
SG97β€’8mo ago
Rider is an IDE like VS LinqPad is a sorta small tool for running code / interacting with database
Thinker
Thinkerβ€’8mo ago
None of which are free
Obada
Obadaβ€’8mo ago
oh zamn
SG97
SG97β€’8mo ago
RoslynPad is free I think thought LinqPad had a free tier also
Obada
Obadaβ€’8mo ago
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
SG97
SG97β€’8mo ago
for me personally, yes
Thinker
Thinkerβ€’8mo ago
yes, but C# doesn't work like that Although you can have script files
Pobiega
Pobiegaβ€’8mo ago
its only really needed when you are brand new
SG97
SG97β€’8mo ago
harold
Obada
Obadaβ€’8mo ago
its a program made for programmers, surely with all the capabilities the world of coding has to offer, they could figure a way out πŸ€”
Pobiega
Pobiegaβ€’8mo ago
You don't understand
Obada
Obadaβ€’8mo ago
i realized that too, but it would still make a neat feature, no? πŸ€” most likely
Pobiega
Pobiegaβ€’8mo ago
This isn't javascript, or python.
Thinker
Thinkerβ€’8mo ago
It's not that they haven't figured it out, it's just that the way C# works, it's just not a thing
Obada
Obadaβ€’8mo ago
well anyways now there is this problem
No description
Obada
Obadaβ€’8mo ago
i tried to copy the in count = 0; to outside of the function but still didnt work
Thinker
Thinkerβ€’8mo ago
You declared count in Div, it doesn't exist in Main
Obada
Obadaβ€’8mo ago
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
Pobiega
Pobiegaβ€’8mo ago
calling a method doesnt "copy" paste the code into the calling
Obada
Obadaβ€’8mo ago
wait
Obada
Obadaβ€’8mo ago
No description
Thinker
Thinkerβ€’8mo ago
What did you change exactly?
Obada
Obadaβ€’8mo ago
nothing :/
Obada
Obadaβ€’8mo ago
before
No description
Obada
Obadaβ€’8mo ago
after
No description
Obada
Obadaβ€’8mo ago
now its just in program file
No description
SG97
SG97β€’8mo ago
divider.cs is in Miscellaneous files
Thinker
Thinkerβ€’8mo ago
Again you declared count in Div, it doesn't exist in Main
Obada
Obadaβ€’8mo ago
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
Obada
Obadaβ€’8mo ago
No description
Obada
Obadaβ€’8mo ago
πŸ’€ it simply doesnt work
Pobiega
Pobiegaβ€’8mo ago
show the code because
ok so i made the code compilable
probably means "I moved shit around until it compiles, but makes no logical sense"
Thinker
Thinkerβ€’8mo ago
You probably want to return two things from Div, the final number and how many times it was divided.
Obada
Obadaβ€’8mo ago
ye thats what im trying
Obada
Obadaβ€’8mo ago
No description
Obada
Obadaβ€’8mo ago
well i changed the else here to else if but now its not compilable
Obada
Obadaβ€’8mo ago
this is the error
No description
Pobiega
Pobiegaβ€’8mo ago
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 name
Obada
Obadaβ€’8mo ago
ye i suspected that but i had no clue how to fix
Pobiega
Pobiegaβ€’8mo ago
Well, look at what Thinker said again
Obada
Obadaβ€’8mo ago
ohhh i understood it differently
Pobiega
Pobiegaβ€’8mo ago
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 another
Obada
Obadaβ€’8mo ago
this feels wrong
No description
Obada
Obadaβ€’8mo ago
(and well is wrong)
Pobiega
Pobiegaβ€’8mo ago
its very close to right thou
Obada
Obadaβ€’8mo ago
does it like.. mean it will still only retunr one value? "gets the value.. ..second element"
Pobiega
Pobiegaβ€’8mo ago
you're getting ahead of yourself. Fix the "count does not exist in the current context" error first.
Obada
Obadaβ€’8mo ago
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..?
No description
Pobiega
Pobiegaβ€’8mo ago
look at where count is defined its in a different scope than your return
Obada
Obadaβ€’8mo ago
idk anymore
No description
Pobiega
Pobiegaβ€’8mo ago
No description
Pobiega
Pobiegaβ€’8mo ago
the red row is declared in the green scope but you are trying to access it in the yellow scope
Obada
Obadaβ€’8mo ago
i changed a bunch
No description
Obada
Obadaβ€’8mo ago
now its in the parameter of the function
Pobiega
Pobiegaβ€’8mo ago
bro Have you considered not just randomly changing things?
Obada
Obadaβ€’8mo ago
😦
Pobiega
Pobiegaβ€’8mo ago
You need to understand variable scope stop trying to avoid that also, ints are value types and mutating it in a method doesnt do anything I can give you 10-15 minutes of VC to demonstrate the issue
Obada
Obadaβ€’8mo ago
im in a study quitly section, with no headphones...
Pobiega
Pobiegaβ€’8mo ago
Β―\_(ツ)_/Β― your loss
Obada
Obadaβ€’8mo ago
also i understand the meaning of variabel scope, but i cant figure it out completely
MODiX
MODiXβ€’8mo ago
Pobiega
Quoted by
From Pobiega
React with ❌ to remove this embed.
Pobiega
Pobiegaβ€’8mo ago
I mean you 100% ignored this
Obada
Obadaβ€’8mo ago
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?
Pobiega
Pobiegaβ€’8mo ago
What scope covers both green and yellow?
Thinker
Thinkerβ€’8mo ago
$scopes
MODiX
MODiXβ€’8mo ago
scope A {
thing a;
scope B {
thing b;
}
}
scope A {
thing a;
scope B {
thing b;
}
}
thing a is available in scope A and scope B thing b is available only in scope B
Obada
Obadaβ€’8mo ago
the one for the whole function..?
No description
Pobiega
Pobiegaβ€’8mo ago
yes
Obada
Obadaβ€’8mo ago
bro i did that before n still didnt work
Pobiega
Pobiegaβ€’8mo ago
look you have like... 12321512343 issues in your code right now you can't expect fixing one to fix the other 12321512342
Obada
Obadaβ€’8mo ago
dam i see
Obada
Obadaβ€’8mo ago
well uhm
No description
Obada
Obadaβ€’8mo ago
is thsi what u meant?
Pobiega
Pobiegaβ€’8mo ago
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 ;D
Obada
Obadaβ€’8mo ago
i 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
Pobiega
Pobiegaβ€’8mo ago
public static int Div(... means the return type is int that means your method can and MUST return one int, and one int only
Obada
Obadaβ€’8mo ago
oh
Pobiega
Pobiegaβ€’8mo ago
if you want to return two values, you'd have to indicate that in your return type
Obada
Obadaβ€’8mo ago
ye teach said tmr we will go though that part of functions right now i use them without much knowledge i must admit
Pobiega
Pobiegaβ€’8mo ago
okay I'll just spoonfeed you the returntype for now then (int number, int count) thats a tuple with two named integers
Obada
Obadaβ€’8mo ago
No description
Pobiega
Pobiegaβ€’8mo ago
that would be an array. not a great way to return 2 values great for returning 0..N values
Obada
Obadaβ€’8mo ago
ye ngl i took it from google cuz idk it did not sit w me that it was an array but
Pobiega
Pobiegaβ€’8mo ago
use the tuple
Obada
Obadaβ€’8mo ago
$tuple
Pobiega
Pobiegaβ€’8mo ago
?
Obada
Obadaβ€’8mo ago
dam they dont got one my memory is a bit weak related to tuples
Pobiega
Pobiegaβ€’8mo ago
No description
Pobiega
Pobiegaβ€’8mo ago
more like your reading skills? O_O
Obada
Obadaβ€’8mo ago
ah πŸ’€
Thinker
Thinkerβ€’8mo ago
A tuple in this case is two values grouped together And in this case both also have their own name
Pobiega
Pobiegaβ€’8mo ago
Also, I find it somewhat funny we're 3 swedes at this point πŸ˜› πŸ‡ΈπŸ‡ͺ
Obada
Obadaβ€’8mo ago
No description
Pobiega
Pobiegaβ€’8mo ago
yep
Obada
Obadaβ€’8mo ago
i must run now to have enough time to eat lunch
Pobiega
Pobiegaβ€’8mo ago
om nom
Obada
Obadaβ€’8mo ago
nah im js eating some random pizza cuz its 60kr kinda a steal
Obada
Obadaβ€’8mo ago
i cant even guess what this means
No description
Obada
Obadaβ€’8mo ago
why does it wanna convert from int to int
Pobiega
Pobiegaβ€’8mo ago
you can't return a single value when you've set your return type to be two values
Obada
Obadaβ€’8mo ago
dont make sense
Pobiega
Pobiegaβ€’8mo ago
its not its saying int to (int number, int count)
Obada
Obadaβ€’8mo ago
ah oh
Pobiega
Pobiegaβ€’8mo ago
you really need to pay more attention lol
Obada
Obadaβ€’8mo ago
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
Pobiega
Pobiegaβ€’8mo ago
you still need to return a valid value
Pobiega
Pobiegaβ€’8mo ago
if you want to allow returning only one of the values, then use nullable ints or better, rewrite the method :p
Obada
Obadaβ€’8mo ago
there was this now but i fixed it myself 🧠
No description
Obada
Obadaβ€’8mo ago
i think i did want 2 well i did start off without count then added it YOO i solved the other problem
Obada
Obadaβ€’8mo ago
No description
Obada
Obadaβ€’8mo ago
yea...
No description
Pobiega
Pobiegaβ€’8mo ago
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
Obada
Obadaβ€’8mo ago
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
Pobiega
Pobiegaβ€’8mo ago
use the debugger and find out stack overflow is almost guraranteed to be the recursion thou
Obada
Obadaβ€’8mo ago
hmm how u use the debugger again
Pobiega
Pobiegaβ€’8mo ago
$debug
MODiX
MODiXβ€’8mo ago
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.
Obada
Obadaβ€’8mo ago
bruh i knew this had some debugger thing idk why i thought u mean tsum else
No description
Obada
Obadaβ€’8mo ago
well this doesnt do much is just
No description
Thinker
Thinkerβ€’8mo ago
Well, you'll have to fix the errors
Obada
Obadaβ€’8mo ago
and this what heppens if i ru nthe latest version
No description
Obada
Obadaβ€’8mo ago
i thought the debubber would help w that
Thinker
Thinkerβ€’8mo ago
the debugger will help with logical errors, not syntax errors
Obada
Obadaβ€’8mo ago
No description
Obada
Obadaβ€’8mo ago
got clue what this means its same as before ill try restarting again, tho i alr tried that
Obada
Obadaβ€’8mo ago
got more problems now πŸ’€
No description
Obada
Obadaβ€’8mo ago
im assuming this is without top level statment but i am using them which is what the problem is ?
Thinker
Thinkerβ€’8mo ago
do you have any other files
Obada
Obadaβ€’8mo ago
nope loyal to only one file πŸ™
Thinker
Thinkerβ€’8mo ago
again, odd
Obada
Obadaβ€’8mo ago
sooo its not this?
Obada
Obadaβ€’8mo ago
i thought i solved my first error on my own
Obada
Obadaβ€’8mo ago
so this works, ive seen how chat gpt made a much better code tho
No description
Accord
Accordβ€’8mo ago
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.