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 4 years ago.
Improve this question
I'm very new to C# and please excuse me if i'm asking you stupid question.
I don't know how to add node to serialized class.
Here is my code:
namespace DuloGames.UI
{
[Serializable]
public class UISpellInfo
{
public int ID;
public string Name;
public Sprite Icon;
public string Description;
public float Range;
public float Cooldown;
public float CastTime;
public float PowerCost;
public float test;
[BitMask(typeof(UISpellInfo_Flags))]
public UISpellInfo_Flags Flags;
}
}
Here is how i try add new node to the serialized class above from another class:
using DuloGames.UI;
public class Character : MonoBehaviour
{
private void AddToNode()
{
UISpellInfo serializedNode = new UISpellInfo();
serializedNode.Add(new UISpellInfo()
{
ID = 1,
Name = "test",
Description = "description"
});
}
}
This is how i try to add new node to the serialized class but it seems i'm not doing it correctly. Can you please help me out ?
I'm not sure what your are trying to do but you UISpellInfo class is just a POCO that has no Add method right?
What are you trying to do.. I think you need to have a List of UISpellInfo object before you can add something to it?
List<UISpellInfo> nodes = new List<UISpellInfo>();
nodes.Add(new UISpellInfo{ID = 1, Name = "test", Description = "description"});
This should work i think.
But then again what will you do with serializedNode, i don't see you are doing anything with it?
Related
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 11 months ago.
Improve this question
I'm fairly new to c# and I'm trying to create a custom object, but they always seem to be empty.
public class Item
{
public string itemName {get;set;}
public int itemAmount {get;set;}
public Item (string name, int amount)
{
name = itemName;
amount = itemAmount;
}
}
public class Backpack : MonoBehaviour
{
void Start()
{
Item gold = new Item ("gold", 5)
}
}
When I try to get gold parameters I get null, 0. Should it work like that? I wanted to use it to quickly add items to a list, and right now I would have to change all of them manually with something like
gold.itemName = "gold"; gold.itemAmount = 5.
Can I do it in another way?
Your assignment in the constructor is the wrong way around, instead it should be:
public Item (string name, int amount)
{
itemName = name;
itemAmount = amount;
}
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 3 years ago.
Improve this question
I don´t know what I´m doing wrong, but this is the Json I´m trying to read:
[{
"object":
{
"Weigh": 4000
}
}]
I really don´t know why I need the "object": part, but if I remove it, the code doesn´t work.
Here is my API Rest:
[HttpPost]
public string GetMixerTime([FromBody]JsonObject<WeighMix>[] json)
{
IList<WeighMix> listawm = JsonConvert.DeserializeObject<List<WeighMix>>(json.ToString());
return listawm.ToString();
}
WeighMix class:
public class WeighMix
{
public double Weigh { get; set; }
public WeighMix()
{
}
public WeighMix(double weigh)
{
Weigh = weigh;
}
}
Thanks a lot.
The Square Brackets signifies Arrays in Json. Your Json is an array of type (say) A, which has a single property called object of type B (B matches the definition of WeighMix).
You need to change your class declaration as
public class RootObject
{
[JsonProperty("object")]
public WeighMix Object{get;set;}
}
// Define other methods and classes here
public class WeighMix
{
public double Weigh { get; set; }
public WeighMix()
{
}
public WeighMix(double weigh)
{
Weigh = weigh;
}
}
You can now deserialize using
var result = JsonConvert.DeserializeObject<List<RootObject>>(json);
Sample Input
var json = #"[ { 'object': { 'weigh': 4000.0 }, 'json': '{\'Weigh\':4000.0}' } ]";
Sample Output
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 6 years ago.
Improve this question
Today i am trying to add another info into a class but i am unable to add. I have tough of adding another class but i am sure that there is a way to modify the class to add more Info.
The info i want to add is
Mr Chan, 1200, 18 Pioneer Rd and
Mr Lee, 600, Blk 21 #21-21 Yishun Rd
class PersonalInfo
{
private string name;
private float salary;
private string address;
public PersonalInfo(string nameVar, float salaryVar, string addressVar)
{
name = nameVar;
salary = salaryVar;
address = addressVar;
}
public void PrintPersonalInfo(TextBox txtPersonalInfo)
{
txtPersonalInfo.Text += name + Environment.NewLine + salary + Environment.NewLine + address + Environment.NewLine + Environment.NewLine;
}
}
That is my code for the class Personal info.
private void btnRun_Click(object sender, EventArgs e)
{
PersonalInfo obj = new PersonalInfo("Mr Tan", 3000, "Blk 123, #12-003 Kepple Rd");
obj.PrintPersonalInfo(txtPersonalinfo);
That is my code for the form.cs So far i can only think of adding new class to add more Info. Now i would like to know how to modify PersonalInfo.cs to add more Info.
Thanks for all you help and have a nice day ahead :)
Perhaps you could consider encapsulating your common properties into a new object, say BasicInfo:
public class BasicInfo
{
public string Name {get; set;}
public float Salary {get; set;}
public string Address {get; set;}
// add more properties when necessary
// e.g. Gender
// public string Gender {get; set;}
}
With that, in the constructor of PersonalInfo class, you can just pass in a BasicInfo object so you don't have to worry about all those properties in the PersonalInfo constructor.
public class PersonalInfo
{
BasicInfo basicInfo;
public PersonalInfo(BasicInfo basicInfo)
{
this.basicInfo = basicInfo;
}
}
You can then keep adding new properties into your BasicInfo class without needing to change PersonalInfo constructor. Hopefully it helps and that's what you are looking for.
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 want to add a property to the Node which is optional for the user to set.
How do I add another class level attribute aside from the one which already exists ?
VirtualName - this would give users the ability to name the node whatever they want.
I already implemented the ClassName. I will eventually want to add other class object level attributes like color and icon.
namespace NodeDemo
{
[NodeAttribute("DemoNode")]
public class DemoNode
{
[InputPinAttribute("Radius")]
public int Radius { get; set; }
[InputPinAttribute("Width Segments")]
public int WidthSegs { get; set; }
[InputPinAttribute("Height Segments")]
public int HeightSegs { get; set; }
}
}
Just add as many as you want:
namespace NodeDemo
{
public class ColorAttribute : Attribute
{
public string Color {get;set;}
public ColorAttribute(string color)
{
Color = color;
}
}
[NodeAttribute("DemoNode")]
[ColorAttribute("Red")]
public class DemoNode
{
...
}
}
You can latter check attribute using Type.Attributes property
string color = null;
Type myType = typeof(DemoNode);
ColorAttribute cAttribute = (ColorAttribute)Attribute.GetCustomAttribute(myType, typeof(ColorAttribute));
if (cAttribute != null)
{
color = cAttribute.Color;
}
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 9 years ago.
Improve this question
I have a list like that.
bread.BreadAdd(new Bread("DF6", "8", 16, 500));
bread.BreadAdd(new Bread("ZER6D23", "5", 8, 1000));
As I understood, I need to create a code in the bread class, but furthermore I have no idea is has to be in the property? If so, where exactly?
edit: uups. I wrote it wrong. it has to be in a function.
Can be something like this:
using SCG = System.Collections.Generic;
public class Bread {
public class Bread(string name, string version, int foodValue, int weight) {
// ...
}
}
public class Breads {
private readonly SCG.List breads = new SCG.List();
public void AddBread(Bread bread) {
breads.Add(bread);
}
public SCG.IEnumerable Breads {
get { return breads; }
}
}
See below.. You can create a method to add it to a private List variable and then expose it via property or you can have a setter for BreadList property and call breadClass.BreadList.Add((new Bread("DF6", "8", 16, 500));. Either option should be fine.
public class BreadClass
{
private List<Bread> lstbread;
public List<Bread> BreadList
{
get
{
return bread;
}
}
public void BreadAdd(Bread bread)
{
lstbread.Add(bread)
}
}