I have this code:
public class TopTen
{
public int Id { get; set; }
public string ShortDesc { get; set; }
public string LongDesc { get; set; }
public Image Photo { get; set; }
}
public class Image
{
public string ImgUrl { get; set; }
public string AlterText { get; set; }
}
I can do like this to assign values:
new topten()
{
/*
here I can access and give values to my properties. BUT i cant
access and give values to the properties in my `Image` class.
This is a problem because I would like to upload the new object as a json-file.
So how do I access the properties in `Image`?
*/
}
var x = new TopTen
{
Id = 1,
Photo = new Image
{
ImgUrl = "pic.jpg",
AlterText = "This is a picture"
}
};
Related
I am able to set the values for each item in ROOT, such as Address_to and Line_items, but when I try to pass the populated class to Post, it's empty.
public class OrdersClass
{
public class Line_items
{
public string sku { get; set; }
public int quantity { get; set; }
}
public class Address_to
{
public string first_name { get; set; }
public string last_name { get; set; }
public string address1 { get; set; }
public string address2 { get; set; }
public string city { get; set; }
public string zip { get; set; }
}
public class Root
{
public string external_id { get; set; }
public IList<Line_items> line_items { get; set; }
public Address_to address_to { get; set; }
}
}
My c# code:
OrdersClass.Root thisOrder = new OrdersClass.Root();
thisOrder.address_to = new OrdersClass.Address_to();
IList<OrdersClass.Line_items> lineItems = new List<OrdersClass.Line_items>();
I can populate address_to as
thisOrder.address_to.first_name "my first name";
and line_items using:
lineItems.Add(new OrdersClass.Line_items());
lineItems[0].sku = ProductSKU;
lineItems[0].quantity = cartQuantity;
but..I know I'm doing this wrong.
Thanks.
You need to add Line_items:
IList<OrdersClass.Line_items> lineItems = new List<OrdersClass.Line_items>();
var lineItem1 = new OrdersClass.Line_items()
{
quantity = 1,
sku = "sku1"
};
lineItems.Add(lineItem1);
var lineItem2 = new OrdersClass.Line_items()
{
quantity = 2,
sku = "sku2"
};
lineItems.Add(lineItem2);
try to use
lineitems.Add(new OrderClass.Line_items(){
sku = ProductSKU,
quantity = cartQuantity
});
Thanks to those who helped me get my thinking cap back on. Resolved.
line_items.Add did not add the iList to the main "thisOrder" class.
I had instantianted it syntactically correct, but it programmatically correct.
This worked:
thisOrder.line_items = new List<OrdersClass.Line_items>();
Then adding a new ilist:
var lineItem = new OrdersClass.Line_items()
{
quantity = cartQuantity,
sku = printifyProductSKU
};
thisOrder.line_items.Add(lineItem);
Yea. Now on the to the programming challenge:ASYNC Post vs Put.
Thanks.
I have a Model that is filled with 20 Properties, for instance such as
public class SensorModel
{
public string Trigger1 { get; set; }
public string PathDoor1 { get; set; }
public string PathDoor2 { get; set; }
public string PathTrigger1 { get; set; }
public string PathTrigger2 { get; set; }
public string PathTrigger3 { get; set; }
public string PathTrigger4 { get; set; }
public string PathTrigger5 { get; set; }
public string PathTrigger6 { get; set; }
public string PathTrigger7 { get; set; }
public string PathTrigger8 { get; set; }
}
After declaring and setting their properties by doing such,
SensorModel sensorsData = new SensorModel();
How can I access sensorsData's properties using a loop?
Because I would like to logs all the data into a txt along with DateTime, I find manually accessing is a waste of time.
Is there any way to automate, for instance, using a loop and accessing it one by one?
You can use reflection to achieve your goal:
var model = new SensorModel() {
PathDoor1 = "Foo",
PathDoor2 = "Foo2",
PathTrigger1 = "Value of PT1",
PathTrigger2 = "Value of PT2",
};
foreach(var value in model.GetTriggerValues()) {
Console.WriteLine(value);
}
public class SensorModel
{
public string Trigger1 { get; set; }
public string PathDoor1 { get; set; }
public string PathDoor2 { get; set; }
public string PathTrigger1 { get; set; }
public string PathTrigger2 { get; set; }
/* ... */
public IEnumerable<string> GetTriggerValues() {
foreach(var prop in this.GetType().GetProperties().Where(x => x.Name.StartsWith("PathTrigger"))) {
yield return (string)prop.GetValue(this, null);
}
}
}
This example filters your properties by name, if you want or need a different result set, amend or remove the where clause.
You can use reflection to achieve this:
var obj = new SensorModel();
// ...
// Get all the properties of your class
var props = typeof(SensorModel).GetProperties();
foreach (var prop in props)
{
// Get the "Get" method and invoke it
var propValue = prop.GetGetMethod()?.Invoke(obj, null);
// Do something with the value
Console.Out.WriteLine("propValue = {0}", propValue);
}
there I have done this before but forgotten the syntax. I am making a JSON file which looks list the classes below but on the submit button I want to add them but I can't remember the syntax for when there is a list inside a list if that makes sense! my classes are:
public class LineItemCheck
{
public string Id { get; set; }
public string Check { get; set; }
public bool Yes { get; set; }
public bool No { get; set; }
}
public class LineItem
{
public string Category { get; set; }
public List<LineItemCheck> LineItemChecks { get; set; }
}
public class RootObject
{
public object SerialNumber { get; set; }
public object UnitNumber { get; set; }
public object Refrigerant { get; set; }
public object ModelNumber { get; set; }
public object BeltSize { get; set; }
public object FreezingUnitComments { get; set; }
public object VisualInspectionComments { get; set; }
public List<LineItem> LineItems { get; set; }
}
private void SubmitButton_Clicked(object sender, EventArgs e)
{
var checklist = new RootObject();
var checklistLineItem = new List<LineItem>();
var checklistLineItemChecks = new List<LineItemCheck>();
checklist.SerialNumber = SerialNumber.Text.ToString();
checklist.UnitNumber = UnitNumber.Text.ToString();
checklist.Refrigerant = Refrigerant.Text.ToString();
checklist.ModelNumber = ModelNumber.Text.ToString();
checklist.BeltSize = BeltSize.Text.ToString();
checklistLineItem.Add(new LineItem() {
Category = "Ziegra Machines Only",
LineItemChecks = new LineItemCheck(LineItemCheck.
),
});
as you can see the bottom section is wrong which is what i am trying to solve thanks
UPDATE: I remember the syntax myself sorry for posting but the answer i was looking for was:
var checklist = new RootObject();
var checklistLineItem = new List<LineItem>();
var checklistLineItemChecks = new List<LineItemCheck>();
checklist.SerialNumber = SerialNumber.Text.ToString();
checklist.UnitNumber = UnitNumber.Text.ToString();
checklist.Refrigerant = Refrigerant.Text.ToString();
checklist.ModelNumber = ModelNumber.Text.ToString();
checklist.BeltSize = BeltSize.Text.ToString();
checklistLineItem.Add(new LineItem() {
Category = "Ziegra Machines Only",
LineItemChecks = new List<LineItemCheck>()
{
new LineItemCheck()
{
Check = "",
},
new LineItemCheck()
{
Check = ""
}
}
});
If I understand your question correctly the following should be the right syntax:
checklistLineItem.Add(new LineItem()
{
Category = "Ziegra Machines Only",
LineItemChecks = new List<LineItemCheck>()
{
new LineItemCheck(),
new LineItemCheck(), ...
},
});
If I may: I would amend your models like so.
public class LineItem
{
public string Category { get; set; }
public List<LineItemCheck> LineItemChecks { get; private set; }
public LineItem()
{
LineItemChecks = new List<LineItemCheck>();
}
}
public class RootObject
{
public object SerialNumber { get; set; }
public object UnitNumber { get; set; }
public object Refrigerant { get; set; }
public object ModelNumber { get; set; }
public object BeltSize { get; set; }
public object FreezingUnitComments { get; set; }
public object VisualInspectionComments { get; set; }
public List<LineItem> LineItems { get; private set; }
public RootObject()
{
LineItems = new List<LineItem>();
}
}
That is, initializing all the collections in a constructor. Properties that expose collections and that can be null are ugly and, more importantly, unexpected. I never expect a collection to be null if it's exposed through a property, I expect it to just be empty, and that I can add stuff to it right away. I'm not in charge of a collection exposed by your object, it's your object's business to initialize itself properly.
This is the consensus in .NET land, you will be hard-pressed to find an example of a collection exposed by a library that is null upon instantiation of the object.
Also notice that I made the setters private, because no other object should be able to flat-out replace a collection exposed by a different object.
Then adding items to collections is simply a matter of:
var root = new RootObject();
var lineItem = new LineItem();
lineItem.LineItemChecks.Add(new LineItemCheck() { /* init properties here */ });
root.LineItems.Add(lineItem);
How do you populate a list as a class object? For example, this does not work:
[DataContract]
public class JsonReviewFormFields
{
[DataMember]
public PersonalDevelopmentPlan personalDevelopmentPlan { get; set; }
}
public class PersonalDevelopmentPlan
{
public List<ShortTerm> shortTerm { get; set; }
public List<LongTerm> longTerm { get; set; }
}
public class ShortTerm
{
public string workRelated { get; set; }
public string structured { get; set; }
public string informal { get; set; }
public string reviewDate { get; set; }
}
public class LongTerm
{
public string workRelated { get; set; }
public string structured { get; set; }
public string informal { get; set; }
public string reviewDate { get; set; }
}
This is controller action:
public JsonReviewFormFields GetReviewForm()
{
PersonalDevelopmentPlan personalDevelopmentPlan = new PersonalDevelopmentPlan();
List<ShortTerm> _itemsShort = new List<ShortTerm>();
_itemsShort.Add(new ShortTerm { workRelated = "workRelated text", structured = "structured text", informal = "informal text", reviewDate = "reviewDate" });
jsonReviewFormFields.personalDevelopmentPlan.shortTerm = _itemsShort;
List<LongTerm> _itemsLong = new List<LongTerm>();
_itemsLong.Add(new LongTerm { workRelated = "workRelated text", structured = "structured text", informal = "informal text", reviewDate = "reviewDate" });
jsonReviewFormFields.personalDevelopmentPlan.longTerm = _itemsLong;
return jsonReviewFormFields;
}
The code crashes at
jsonReviewFormFields.personalDevelopmentPlan.shortTerm = _itemsShort;
It's probably a basic object orientated error. How do you populate the list?
You are not instantiating it, you have to instantiated the type first:
jsonReviewFormFields.personalDevelopmentPlan = new PersonalDevelopmentPlan();
and then set property of it:
jsonReviewFormFields.personalDevelopmentPlan.shortTerm = _itemsShort
before that you also have to instantiate main class which i don't see in your controller action anywhere :
JsonReviewFormFields jsonReviewFormFields = new JsonReviewFormFields();
public class Row
{
//row
public string name { get; set; }
public string height { get; set; }
public bool sortable { get; set; }
public string classes { get; set; }
public string color { get; set; }
public string data { get; set; }
}
private static List<object> dataList = new List<object>()
{
new Row()
{
name= "Milestones",
height= "3em",
sortable= false,
classes= "gantt-row-milestone",
color= "#45607D",
}
new Row()
{
name= "Milestones",
height= "3em",
color= "#45607D",
}
}
I am trying to create two objects with different number of variables
and my problem is that I don't now how to delete or escape variables instantiated by default (with 0 or null)
There is no way to delete Fields from an object. But you can design your classes as you need.
public class GenericRow
{
public string name { get; set; }
public string height { get; set; }
public string color { get; set; }
public string data { get; set; }
}
public class DetailedRow : GenericRow
{
public bool sortable { get; set; }
public string classes { get; set; }
}
And instantiate your objects as following.
private static List<GenericRow> dataList = new List<GenericRow>()
{
new DetailedRow ()
{
name= "Milestones",
height= "3em",
sortable= false,
classes= "gantt-row-milestone",
color= "#45607D",
},
new GenericRow()
{
name= "Milestones",
height= "3em",
color= "#45607D",
}
};
Just use null to indicate a 'deleted' property:
var row = (Row)dataList[0];
if (row.name == null)
// name is deleted
else
DoSomething(row.name);
For value types use Nullable<>:
public bool? sortable { get; set; }
if (row.sortable == null)
// sortable is deleted
else
DoSomething(row.sortable.Value);