C
C#2y ago
Cringe

❔ 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
Pobiega
Pobiega2y ago
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.
Cringe
CringeOP2y ago
Ok let me show u what I mean
Pobiega
Pobiega2y ago
Sure
Cringe
CringeOP2y ago
This si written in the class CatDataStorage
Pobiega
Pobiega2y ago
Yep, I can see that 🙂
Cringe
CringeOP2y ago
Cringe
CringeOP2y ago
I have bad Internet connection give it time to load the pictures I want to do that but on a larger scale
Pobiega
Pobiega2y ago
You don't need to take pictures btw you can just copy the code and use $code to show it
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/
Cringe
CringeOP2y ago
I don't have access to discord on my device because of the admins So I'm on my phone
Pobiega
Pobiega2y ago
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 fine
Cringe
CringeOP2y ago
But when I remove static I can't call it from the main code
Pobiega
Pobiega2y ago
Sure you can, just not the way you think
Cringe
CringeOP2y ago
Oh Then how
Pobiega
Pobiega2y ago
public static void Main()
{
CatDataStorage cat = new CatDataStorage();
cat.outputcatdata();
}
public static void Main()
{
CatDataStorage cat = new CatDataStorage();
cat.outputcatdata();
}
I would highly suggest you rename CatDataStorage to just Cat btw and outputcatdata to PrintCatData
Cringe
CringeOP2y ago
Ok It's still asking for an object reference for the non static feild
Pobiega
Pobiega2y ago
public void PrintCatData()
{
Console.WriteLine($"Name: {CatName}");
}
public void PrintCatData()
{
Console.WriteLine($"Name: {CatName}");
}
?
Cringe
CringeOP2y ago
I'm calling it from a switch acting as the main menue
Pobiega
Pobiega2y ago
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 anything
Cringe
CringeOP2y ago
I'll show you the switch
Pobiega
Pobiega2y ago
sure
Cringe
CringeOP2y ago
Pobiega
Pobiega2y ago
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 cases
Cringe
CringeOP2y ago
Ok 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
Pobiega
Pobiega2y ago
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
public static void Main()
{
List<Cat> cats = new List<Cat>();

while (true)
{
PrintMenu();
var choice = Helper.Get<int>("Enter your menu choice:");
switch (choice)
{
case 1:
{
Cat newCat = Cat.FromConsoleInput();
cats.Add(newCat);
Console.WriteLine($"Cat {newCat.Name} added!");
break;
}
case 2:
{
Console.WriteLine($"There are {cats.Count} cats in the list.");
for (int i = 0; i < cats.Count; i++)
{
Console.WriteLine($"{i}. {cats[i].Name}");
}

break;
}
case 3:
{
Console.WriteLine("Sorry, not implemented yet.");
break;
}
case 4:
return;
}
}
}

private static void PrintMenu()
{
Console.WriteLine("1. Add new cat");
Console.WriteLine("2. Print information about all cats");
Console.WriteLine("3. Show detailed information about a cat");
Console.WriteLine("4. Quit");
}
public static void Main()
{
List<Cat> cats = new List<Cat>();

while (true)
{
PrintMenu();
var choice = Helper.Get<int>("Enter your menu choice:");
switch (choice)
{
case 1:
{
Cat newCat = Cat.FromConsoleInput();
cats.Add(newCat);
Console.WriteLine($"Cat {newCat.Name} added!");
break;
}
case 2:
{
Console.WriteLine($"There are {cats.Count} cats in the list.");
for (int i = 0; i < cats.Count; i++)
{
Console.WriteLine($"{i}. {cats[i].Name}");
}

break;
}
case 3:
{
Console.WriteLine("Sorry, not implemented yet.");
break;
}
case 4:
return;
}
}
}

private static void PrintMenu()
{
Console.WriteLine("1. Add new cat");
Console.WriteLine("2. Print information about all cats");
Console.WriteLine("3. Show detailed information about a cat");
Console.WriteLine("4. Quit");
}
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()
Cringe
CringeOP2y ago
Omg thank you This really helps Sorry for the late response I had classes until now But this is amazing 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