Making a list in the property [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 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)
}
}

Related

How can I deserialize a simple Json with square brackets 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 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

C# - Add node to serialized 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 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?

Adding multiple class level attributes 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 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;
}

Can We Have Class Name Instead Of Return Type 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 7 years ago.
Improve this question
Can We Have Class Name Instead Of Return Type ? If Yes Than How And If No Than How ? , Please Explain Me With This Basic Singleton Pattern
class Program
{
static void Main(string[] args)
{
Sample s1;
s1 = Sample.cr();
Sample s2 = Sample.cr();
Console.ReadLine();
}
}
class Sample
{
static Sample p;
private Sample()
{
}
// Why Here Retrun Type Is Missing I Mean Void / Int ?
public static Sample cr()
{
if(p==null)
{
p = new Sample();
}
return p;
}
}
Return type is not missing in your sample. Your cr method has return type, and it is Sample.
Any of your custom class is type too, not only primitive types such as int, string and so on.

The best way to implement the class which answer if coninas and adds elemns [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 am interested in data structure to implement indicator for my data
my data could be preseted as triple of string,string and int
public class MyData
{
public string Key1,
public string Key2,
public int Key3
}
I want to implement
public class MyIndicator
whith Methods :
IsContained (MyData data) and
Add (MyData data)
What is the best and efficient way to implement MyIndicator class
Are you looking for something like this?
public class Indicator
{
List<MyData> _dataList = new List<MyData>();
public bool IsContained(MyData data)
{
return _dataList.Exists(x => x.Key == data.Key
&& x.Key1 == data.Key1
&& x.Key2 == data.Key2);
}
public void Add(MyData data)
{
_dataList.Add(data);
}
}

Categories