Item not added to list with classes? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have these two classes:
public class SpawnStringClass{
public List<PlayerSpawn> playerSpawn;
}
public class PlayerSpawn
{
public string PlayerName;
public float posx;
public float posz;
}
I created a variable Referencing to the first class:
SpawnStringClass SpawnClass = new SpawnStringClass(){playerSpawn = new List<PlayerSpawn>()};
var item = new PlayerSpawn
{
PlayerName = player.NickName.ToString(),
posx = SpawnX,
posz = SpawnZ
};
SpawnClass.playerSpawn.Add(item);
Now when I try to convert the Spawn string class with the list to json it doesn'T work.
SpawnString = JsonUtility.ToJson(SpawnClass);
Debug.Log(SpawnString);
Does anyone know why?
The classes are outside of the main mono behaviour class and the variable player.nickname, SpawnX and SpawnZ are declared.
Thanks in advance!

The built-in JsonUtility uses the also built-in Unity serializer.
Refer to Script Serialization and in particular the section How to ensure a custom class can be serialized where you can find
Ensure it:
Has the [Serializable] attribute
Is not abstract
Is not static
Is not generic, though it may inherit from a generic class
Solution: Your both classes have to be attributed [Serializable]
[Serializable]
public class SpawnStringClass
{
public List<PlayerSpawn> playerSpawn;
}
[Serializable]
public class PlayerSpawn
{
public string PlayerName;
public float posx;
public float posz;
}

Related

How can I create a reference that can contain both a stack and a list without needing a cast in c#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'd like to be able to have a reference to a collection in a superclass that will need to contain either a stack or a list in a subclass, and I am having trouble understanding how to make that work, any ideas?
Something like:
public class Group
{
Collection<Human> group;
}
public class PeopleStack : Group
{
public PeopleStack()
{
this.group = new Stack<Human>();
}
}
public class Crowd : Group
{
public Crowd()
{
this.group = new List<Human>();
}
}
The Stack is defined as
public class Stack<T> :
System.Collections.Generic.IEnumerable<T>,
System.Collections.Generic.IReadOnlyCollection<T>,
System.Collections.ICollection
and the List is
public class List<T> :
System.Collections.Generic.ICollection<T>,
System.Collections.Generic.IEnumerable<T>,
System.Collections.Generic.IList<T>,
System.Collections.Generic.IReadOnlyCollection<T>,
System.Collections.Generic.IReadOnlyList<T>,
System.Collections.IList
So you can use one of the strong (generic) types IEnumerable<Human> or IReadOnlyCollection<Human> for group.
And of course that should be protected etc, but I suppose you know that.

Java style polymorphism in C#? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to use Java style polymorphism in C#. Is it possible?
Here is an example that does not compile
using System;
namespace HelloWorld
{
public class Program
{
public static void Main (string[] args)
{
Triangle triangle = new Triangle(2);
Square square = new Square(3);
printID(square);
}
public void printID(Shape s){
Console.WriteLine ("id is " + s.id);
}
}
public class Shape{
public int id;
}
public class Triangle: Shape{
float b;
float height;
float area(){
return b*height/2;
}
public Triangle(int k){
id=k;
}
}
public class Square: Shape{
float side;
float area(){
return side*side;
}
public Square(int k){
id=k;
}
}
}
The message is
MyClass.cs(11,4): error CS0120: An object reference is required to access non-static member `HelloWorld.Program.printID(HelloWorld.Shape)'
Thanks!
Error is not related to polymorphism - you are calling non-static method from static method Main. You should make printID static as well.
public static void printID(Shape s){
Console.WriteLine("id is " + s.id);
}
Also I suggest you to:
Stick with C# naming guidelines when you are writing C# code. Methods and properties should have PascalCase names.
Use properties instead of public fields
If any shape should have and id, consider to create public Shape(int id) constructor in base class and call that constructor from derived classes via : base(id)
Improve naming - if you are passing id, then call variable id instead of k.

c# interface for static that create on the class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i have this class
class Class1
{
public static List<int> abc = new List<int>();
}
so now i can access from outside the class :
public void x()
{
Class1.abc.Add(2);
}
what i want to do is :
I want "Class1" implement from INTERFACE and implement this "abc"
that i can do Class1.abc.Add(2);
i mean that the abc will be on interface..
I already try to do this but i not without any success
how can i do this please?
thanks!
As others have said, only objects can implement interfaces in C#. See Why Doesn't C# Allow Static Methods to Implement an Interface? for a good explanation.
Instead, you could use the Factory (an object that use used to create other objects) or Singleton patterns (use a single instance of an object). These could implement an interface including the "Add" method you mentioned.
For example, instead of:
class Class1
{
public static List<int> abc = new List<int>();
}
Class1.abc.Add(1); // Add numbers
... have something like ...
interface IListInterface
{
List<int> List;
}
class Lists: IListInterface
{
public Lists()
{
List = new List<int>();
}
public List<int> List
{
get;
}
}
// Using the above
public void AddToList(IListInterface lists, int a)
{
lists.List.Add(a);
}
This standardize your access to the lists by using an interface and allows you to swap the implementation of the list interface without affecting the using code, useful for automated testing.

What is the difference between these two c# abstract classes? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
So I was following a c# tutorial the otherday (still a begginer) and I saw how the class ATMState was defined:
public abstract class ATMState
{
private ATM atm;
public ATM Atm
{
get { return atm; }
set { atm = value; }
}
private int dummyCashPresent = 1000;
public int DummyCashPresent
{
get { return dummyCashPresent; }
set { dummyCashPresent = value; }
}
public abstract string GetNextScreen();
}
To be honest, I find those declaraions rather confusing, so I decided to redo it in my own way:
public abstract class ATMState
{
public ATM Atm { get; set; }
public int DummyCashPresent { get; set; }
public ATMState()
{
DummyCashPresent = 1000;
}
public abstract string GetNextScreen();
}
What is the difference? Which one is better and why?
What is the difference?
You're using "automatic properties", the sample code is not. Perhaps the sample was written before automatic properties were introduced to C#.
Which one is better and why?
Generally automatic properties are better: more compact code is less to read and understanding. If you have no logic to put in the getter or setter you should be using automatic properties.
However, currently1 you cannot set a breakpoint on either the getter or setter of an automatic property, thus to be able to track accesses one sometimes need to break out with an explicit backing field.
1 I believe this will change in VS2015.

How can i create dynamic variables on the fly in c# using Reflection? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm creating a simulator of ecosystems where species can be used to simulate various diseases, my problem is that I start using 4 species but if I need more ... I need more variables to store, my question is, Is there any way through Reflection to let me create dynamic variables during the execution of an event in my program? Thank you! i'm using Windows Presentation Foundation and C#
The normal way to handle this is to have a base class for your disease species and then use a collection to hold them all:
public abstract class DiseaseBase
{
public abstract void Spread();
}
public class Anthrax : DiseaseBase
{
public override void Spread()
{
GetPostedToPolitician();
}
}
public class BirdFlu : DiseaseBase
{
public override void Spread()
{
Cluck();
SneezeOnHuman();
}
}
public class SwineFlu : DiseaseBase
{
public override void Spread()
{
//roll in mud around other piggies
}
}
public class ManFlu : DiseaseBase
{
public override void Spread()
{
//this is not contagious
//lie in bed and complain
//get girlfriend to make chicken soup
//serve chicken soup with beer and baseball/football/[A-Za-z0-9]+Ball
}
}
public List<DiseaseBase> DiseaseCollection = new List<Disease>();
So everything gets stored in the collection as the base class (DiseaseBase), and with the appropriate use of abstract methods in the base and/or interfaces you can always handle each disease instance as the base object.

Categories

Resources