❔ How to call a method that uses a struct in it's definition?
I program in C# with Unity most-often, I am branching out to 'normal' C# and am stuck on connecting a method to main in a practice project. is used to define this method: and I cannot find a way to run this in Main to actually begin working out the results. Any help would be greatly appreciated!
46 Replies
where does
oIncome
live, inside sInformation
?Holy goddamn Hungarian notation lmao
lol yeah, was going to mention that later...
If it's not a static method, you'll need an instance of this struct
Haha yeah the formatting on this is weird, oIncome is it's own method
Wym "it's own"?
right, but does it live within the definition for sInformation?
Methods are defined inside of classes or structs
Unless we're talking
Program.cs
with top-level statementswe are, this is in
my bad
ah, that's totally fine
It's not a static method
only thing outside of class is the struct
So you'll need to instantiate
Program
Or make it static
Or make a non-static wrapper for Main
changing the method definition to
static sInformation oIncome...
will be the simplest of those options$nonstaticmain
Or, yeah, make this method static
(and, for all that is holy, get rid of those weird prefixes lol)
i mainly don't want to overload them with information when they probably don't know what static means
I mean, in my previous experience static just means it cannot be a value that can be changed, unless it means something else in this context
static
methods exist on the type itself
Not on an instance of that typestatic does not mean 'readonly'
it has a very specific meaning in c# that is generally counterintuitive for beginners
are you familiar with basic OOP concepts of creating instances of classes?
I think so? In essence, is there a minimally invasive way to be able to run this oIncome method?
I can post the whole script if that helps
sure, you can post your whole Program.cs
the simplest way is likely to add the
static
keyword to the definition of your methodok i will get that up in a minute, thank you
$paste
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!
might be useful
ill mention the purpose of this app is to be open to improvement, so most of this code came as is
of course
there might be discrepency in naming, im in the middle of renaming some variables here
Yeah, just call
oIncome
in Main
as isok, but what about the parameters? this is everything in the code, did i need to create a list for the actual method params?
Well, yeah
yes, you will need to create arrays for the values you want to pass to the method
You need the params the method needs
would it be best practice to define that before main?
Why?
Do you need them everywhere in the class?
Or do you need them in the main?
well, we have required, but I don't have anything to put into the method. This would be a int[] containing random food items like burger or soda etc for the script to run through
So create a local variable
I guess i just mean, with the way this code is presented, what the best practice would be, so a local variable in main to call the method?
And this array seems to be holding integers, not ingredients, btw
Yeah
right
Just local variables
the ints would represent food items my bad
So this doesn't seem to like the bracketing in this setup :
it says } expected for the main function
Local variables cannot be public or private
Local variables are... local
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.