❔ method
Hello,
If I create a method, does the method know about things in the Main method? Or am I supposed to tell the method?
54 Replies
it will not "know" about things in other methods
how to tell
you'll get compilation errors? 🙂
how to import a int/string in a other method
public void DoThing(string someValue){ }
pass what you want to use in the method as a parameterarguments
thank you @Ero @Task.WhenAll
they meant "how to tell the method about the variable"
not "how to tell that it doesn't work"
thank you too
what are those warnings though
suggestions (var over int, public could be private). Not relevant to the topic.
also, chill with the passive aggressive comments while someone is helping lol
what?
what passive aggressive comments
yeah that's just
fact
and clarification
Read the initial post.
I think its a good idea to show the initial state and how to fix the problem
what
static public int Poop(string countPoopString) {
try {
int countPoopInt = Int16.Parse(countPoopString);
}
catch {
Console.WriteLine("Konverting dont available");
}
return countPoopInt ;
}
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/odd method name there
where
u called ur method
Poop
yes
which means this 💩
and?
its dor testing
for
so im saying its odd, u asked where
what means odd
weird, unusual
anyways, your code has a problem.. you are declaring a variable in the try that might not succeed.
countPoopInt
is declared in the wrong "scope" for you to use it as the returnwhat do you mean with scope
$scopes
thing a
is available in scope A
and scope B
thing b
is available only in scope B
anywhere you see
{
, that creates a new scope
}
closes the current scope
so the stuff inside the try is not availble outside itso how to get it outside
You could split the declaration and the assignment
so you declare the variable outside the try, and assign to it inside.
you'll need to give it a default value thou, in case the try fails
better, but you have duplicate declarations now
Int16 btw
Ah yes, don't use that. just
int.Parse
insteadok
that I know, but the parse is not recocnized now
how so?
u need to tell us what error messages u see
and show us ur current code
the name "int" isnt there in the actual konext
copy paste the error message
Im a german so the error message is in german
doesnt matter, i can see the error code number
CS0103
error message can help too since it can tell which variable it has the issue with
and show ur current code
ok wait
its
int.Parse(countPoopString);
not Int.Parse(countPoopString);
i is smalloh
yeah
Ill be away for a short time, thank you
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.