C
C#β€’14mo ago
Obada

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

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