❔ struggling with classes again
I want to store this data into an array but so I can select the cats information and have it stored, however when the function is static I can't use the set variables. I am relatively new so any help would be great.
27 Replies
The code shown does not feature a static method, and even if its a fair bit cursed and violates most principles of good C#, it should work.
Ok let me show u what I mean
Sure
This si written in the class CatDataStorage
Yep, I can see that 🙂
I have bad Internet connection give it time to load the pictures
I want to do that but on a larger scale
You don't need to take pictures btw
you can just copy the code
and use $code to show it
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/I don't have access to discord on my device because of the admins
So I'm on my phone
Okay. Well, the problem with that last method is that a
static
method doesnt belong to an instance of the class, it belongs to the class itself
it can thus not access instance fields
In this case, just remove the static
keyword and you're fineBut when I remove static I can't call it from the main code
Sure you can, just not the way you think
Oh
Then how
I would highly suggest you rename
CatDataStorage
to just Cat
btw
and outputcatdata
to PrintCatData
Ok
It's still asking for an object reference for the non static feild
?
I'm calling it from a switch acting as the main menue
Thats fine, you just need to understand that
Cat
(or CatDataStorage
) represents a single cat, and must be instantiated (with the new
keyword) to do anythingI'll show you the switch
sure
Right, you cant use it like that
Cat.PrintCatData()
is how you call a static method
you also cant just make your newCat
in the first case and then do nothing with it
you need to think about object lifetimes
In this case, it looks like your program currently only handles a single cat
so you'll need to have a Cat
variable somewhere in a higher scope, where you can access it from the other casesOk
I was planning on storing each cat in an array
However I don't know how to store a large number of data within an array
Sorry if this is frustrating for you. I'm just very confused at this
so, a few things to consider
arrays are fixed size, meaning you need to know how many cats you want to store
and you also need to keep track of how many you currently have.
Annoying. I suggest you use
List<Cat>
instead, which is very similar but has dynamic size
Here is a basic structure to help you
ignore Helper.Get
, its a function I've made myself that you wont have access to. its just a fancy way of doing the integer conversion you do
and Cat.FromConsoleInput
is equivivalent to your new Cat()
Omg thank you
This really helps
Sorry for the late response I had classes until now
But this is amazing 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.