C
C#2y ago
Dineo

✅ how can i call a static void in an if statement

.
46 Replies
🎆Async-Void 》🎆
ok so show the code you have tried $paste
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
TheBoxyBear
TheBoxyBear2y ago
ClassName.StaticMethod() or just StaticMethod() if in the same class
Dineo
DineoOP2y ago
TheBoxyBear
TheBoxyBear2y ago
But you can't really call if in an if since it's void Not as part of the condition But it can be in the if body
🎆Async-Void 》🎆
the if statement needs to be in a method
TheBoxyBear
TheBoxyBear2y ago
That too
🎆Async-Void 》🎆
is this a console app with top level statements?
TheBoxyBear
TheBoxyBear2y ago
All instructions apart from initial assignment of members need to be in a method
Dineo
DineoOP2y ago
ok
Connor
Connor2y ago
I’m flabbergasted that you know about async/await, http clients, and serialization, but you don’t know how to create a method
Dineo
DineoOP2y ago
mweh its a proof of concept app for school so i didnt really fix my error
🎆Async-Void 》🎆
right, but is it a console app?
Dineo
DineoOP2y ago
no its a 🤢winforms project🤢
FestivalDelGelato
what should the if do
Dineo
DineoOP2y ago
say the joke
Dineo
DineoOP2y ago
Dineo
DineoOP2y ago
here is the full codebase i know its messy and has alot of memory leaks but i just need that joke thingy to work when sorry if im annoying you in some way
🎆Async-Void 》🎆
give me a minute
FestivalDelGelato
then just make it a lambda that calls the method and returns something you can use in the if
Dineo
DineoOP2y ago
like how ??
FestivalDelGelato
static void tell_the_joke() { /* stuff */ }

void caller()
{
if (new Func<bool>(() => { tell_the_joke(); return true; })())
throw new Exception("it works!");

bool or_like_this() { tell_the_joke(); return true; }
if (or_like_this())
throw new Exception("it works!");
}
static void tell_the_joke() { /* stuff */ }

void caller()
{
if (new Func<bool>(() => { tell_the_joke(); return true; })())
throw new Exception("it works!");

bool or_like_this() { tell_the_joke(); return true; }
if (or_like_this())
throw new Exception("it works!");
}
seems simple to me maybe i didn't get what you want
var uhm = () => { tell_the_joke(); return true; };
if (uhm())
throw new Exception("it works!");
var uhm = () => { tell_the_joke(); return true; };
if (uhm())
throw new Exception("it works!");
maybe you could even do it with Task.Run
Dineo
DineoOP2y ago
Dineo
DineoOP2y ago
something like that but i got 2 errors from this actually 3
Dineo
DineoOP2y ago
Dineo
DineoOP2y ago
ok
🎆Async-Void 》🎆
@Dineo remove the third argument
Dineo
DineoOP2y ago
wdym
🎆Async-Void 》🎆
the first error what does it say?
Dineo
DineoOP2y ago
ok i did that
Dineo
DineoOP2y ago
i fixed the other one so just need to fix the third error
🎆Async-Void 》🎆
where do you initialize it?
Dineo
DineoOP2y ago
in my static method but its below that method that shows the error if that helps
🎆Async-Void 》🎆
it isnt in scope
Dineo
DineoOP2y ago
soo :)))
🎆Async-Void 》🎆
I dont see it anywhere in the code you posted
Dineo
DineoOP2y ago
its here close to last lines below materialMultiLineTextBox1.AppendText(r + '\n'); line 260
🎆Async-Void 》🎆
right so the scope ends at the last } of the method also you arent deserializing to an object I have to go for awhile
Dineo
DineoOP2y ago
i fixed it :))) closed
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.
Dineo
DineoOP2y ago
/closed
Hugh
Hugh2y ago
You need to use "/close" rather than "/closed"
FusedQyou
FusedQyou2y ago
Holy shit how did you even manage to come this far This is some next level mess 🙃
ero
ero2y ago
chatgpt, what a question
Dineo
DineoOP2y ago
mweh

Did you find this page helpful?