C
C#2y ago
palekf

❔ 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
Pobiega
Pobiega2y ago
it will not "know" about things in other methods
palekf
palekfOP2y ago
how to tell
Pobiega
Pobiega2y ago
you'll get compilation errors? 🙂
palekf
palekfOP2y ago
how to import a int/string in a other method
Doombox
Doombox2y ago
public void DoThing(string someValue){ } pass what you want to use in the method as a parameter
ero
ero2y ago
arguments
palekf
palekfOP2y ago
thank you @Ero @Task.WhenAll
Pobiega
Pobiega2y ago
ero
ero2y ago
they meant "how to tell the method about the variable" not "how to tell that it doesn't work"
Pobiega
Pobiega2y ago
palekf
palekfOP2y ago
thank you too
ero
ero2y ago
what are those warnings though
Pobiega
Pobiega2y ago
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
ero
ero2y ago
what? what passive aggressive comments
Pobiega
Pobiega2y ago
ero
ero2y ago
yeah that's just fact and clarification
Pobiega
Pobiega2y ago
Read the initial post. I think its a good idea to show the initial state and how to fix the problem
ero
ero2y ago
what
palekf
palekfOP2y ago
static public int Poop(string countPoopString) { try { int countPoopInt = Int16.Parse(countPoopString); } catch { Console.WriteLine("Konverting dont available"); } return countPoopInt ; }
TheRanger
TheRanger2y ago
$code
MODiX
MODiX2y ago
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/
TheRanger
TheRanger2y ago
odd method name there
palekf
palekfOP2y ago
where
TheRanger
TheRanger2y ago
u called ur method Poop
palekf
palekfOP2y ago
yes
TheRanger
TheRanger2y ago
which means this 💩
palekf
palekfOP2y ago
and? its dor testing for
TheRanger
TheRanger2y ago
so im saying its odd, u asked where
palekf
palekfOP2y ago
what means odd
Pobiega
Pobiega2y ago
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 return
palekf
palekfOP2y ago
what do you mean with scope
Pobiega
Pobiega2y ago
$scopes
MODiX
MODiX2y 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
Pobiega
Pobiega2y ago
anywhere you see {, that creates a new scope } closes the current scope so the stuff inside the try is not availble outside it
palekf
palekfOP2y ago
so how to get it outside
Pobiega
Pobiega2y ago
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
palekf
palekfOP2y ago
static public int Poop(string countPoopString) {
int countPoopInt
try {
int countPoopInt = Int16.Parse(countPoopString);
}
catch {
Console.WriteLine("Konverting dont available");
}
return countPoopInt ;
}
static public int Poop(string countPoopString) {
int countPoopInt
try {
int countPoopInt = Int16.Parse(countPoopString);
}
catch {
Console.WriteLine("Konverting dont available");
}
return countPoopInt ;
}
Pobiega
Pobiega2y ago
better, but you have duplicate declarations now
TheRanger
TheRanger2y ago
Int16 btw
Pobiega
Pobiega2y ago
Ah yes, don't use that. just int.Parse instead
palekf
palekfOP2y ago
ok
TheRanger
TheRanger2y ago
int CountPoopInt; // this is a declaration
CountPoopInt = 1; // this is an assignment
int CountPoopInt = 1; // this is a declaration and assignment combined
int CountPoopInt; // this is a declaration
CountPoopInt = 1; // this is an assignment
int CountPoopInt = 1; // this is a declaration and assignment combined
palekf
palekfOP2y ago
that I know, but the parse is not recocnized now
TheRanger
TheRanger2y ago
how so? u need to tell us what error messages u see and show us ur current code
palekf
palekfOP2y ago
the name "int" isnt there in the actual konext
TheRanger
TheRanger2y ago
copy paste the error message
palekf
palekfOP2y ago
Im a german so the error message is in german
TheRanger
TheRanger2y ago
doesnt matter, i can see the error code number
palekf
palekfOP2y ago
CS0103
TheRanger
TheRanger2y ago
error message can help too since it can tell which variable it has the issue with and show ur current code
palekf
palekfOP2y ago
ok wait
static public int Poop(string countPoopString) {
int countPoopInt;
try {
int countPoopInt = Int.Parse(countPoopString);
}
catch {
Console.WriteLine("Konvertierung nicht möglich");
}
return countPoopInt;
}

}
static public int Poop(string countPoopString) {
int countPoopInt;
try {
int countPoopInt = Int.Parse(countPoopString);
}
catch {
Console.WriteLine("Konvertierung nicht möglich");
}
return countPoopInt;
}

}
TheRanger
TheRanger2y ago
its int.Parse(countPoopString); not Int.Parse(countPoopString); i is small
palekf
palekfOP2y ago
oh yeah Ill be away for a short time, thank you
Accord
Accord2y 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