Main Class:
public class ServiceResponse
{
public string RequestId { get; set; }
public string ConnectionId { get; set; }
public List<FTTask> Tasks { get; set; }
}
List Class:
public class FTTask
{
public int TransType { get; set; }
public int Status { get; set; }
public string TaskStatus { get; set; }
}
ServiceResponse sr; // class object
int count = _SerResponse.Tasks.Count; // count list items
How to find each param value of this list class inside for loop ...
for (int j = 0; j < count; j++){
// Unable to find TransType,status values of list inside this loop
}
You need to obtain a reference to the list item.
for (int j = 0; j < count; j++){
FTTask task = _SerResponse.Tasks[j]
// Unable to find TransType,status values of list inside this loop
}
It could be the simple one
ServiceResponse rep = // instance of this class
for (int j = 0; j < rep.Tasks.Count ; j++){
FTTask ftTask = rep.Tasks[j];
int transTypeValue = ftTask.TransType;
}
I think you're on the right track..see my addition for your script below. Hope this helps.
for (int j = 0; j < count; j++){
// Unable to find TransType,status values of list inside this loop
FTTask fTTaskEntry = _SerResponse.Tasks[j];
int transType = fTTaskEntry.TransType; //<- this is it right?
}
using System.Reflection;
PropertyInfo[] props = typeof(Item).GetProperties();
for(int i = 0; i < props.Length; i++)
{
string ParamName = props[i].Name;
}
Related
I have Classes:
Class A
{
public string Name { get; set; }
public List<B> ExpenseList { get; set; }
}
Class B
{
public string Expense{ get; set; }
public decimal Money{ get; set; }
}
Suppose my data is as follows:
data:
{
0 Name: Carlos;
1 Name: Tom;
}
expense:
{
0 Expense: book; Money: 100;
1 Expense: fund; Money: 80;
}
I want each data to have 1 list expense. And this is i use:
for(int i=0; i < data.Count; i++)
{
for(int k=0; k < expense.Count; k++)
{
data[i].ExpenseList.Expense = expense[k].Expense;
data[i].ExpenseList.Money= expense[k].Money;
}
}
I run the program and got the error. What did I do wrong? I do not know where the error is
Can somebody help me, thanks
You're working with a list, not with an object. You need to create an object and then add to the list.
for(int i=0; i < data.Count; i++)
{
for(int k=0; k < expense.Count; k++)
{
B toAdd = new B();
toAdd.Expense = expense[k].Expense;
toAdd.Money = expense[k].Money;
data[i].ExpenseList.Add(toAdd);
}
}
Initialize the list in constructor,it will solve to ur problem
i am trying to create an internal class inside a class and then initialize it as first value =0
i have this.
class Vehicle {
internal class AGVSteps {
public double X { get; set; }
public double Y { get; set; }
}
private AGVSteps[] steps ;
public AGVSteps [] Steps {
get { return this.steps; }
}
public Vehicle() { //constructor
this.steps = new AGVSteps[2000];
this.steps[0].X = 0; //CRASHES HERE
MessageBox.Show(this.steps[0].X + "");
for (int i = 0; i < 2000; i++) {
// this.steps[i].X = -1;
//this.steps[i].Y = -1;
}
}
}
any idea ?i get a NPE error.Thanks!
steps is a container to AGVSteps objects. You need to initialize the AGVSteps itself
this.steps = new AGVSteps[2000];
for (int i = 0; i < steps.Length; i++) {
steps[i] = new AGVSteps();
}
Multiple List Object only can add single item to preceding lists. Only adds last item for second list and of course I need all of them not just the last one.
Here is my Code:
public class MyListData
{
public List<HeaderItem> HeaderItems { get; set; }
public List<MatrixItem> MatrixItems { get; set; }
}
public MyListData GetSchedule()
{
MyListData objTab = new MyListData();
objTab.HeaderItems = new List<HeaderItem>();
//Header loop works perfectly
for(int x=0; x < 7;x++)
{
HeaderItem objItem = new HeaderItem();
objItem.strHeadName = x;
objTab.HeaderItems.Add(objItem);
}
objTab.MatrixItems = new List<MatrixItem>();
for(int x=0; x < 7;x++)
{
MatrixItem objItem = new MatrixItem();
objItem.nHRJobID = x;
objTab.MatrixItems.Add(objItem);
}
//Only adds the last one Need ALL
return objTab;
}
If I need to create a new object then how would I combine say objTab and objMatrix?
Seems to add all seven values for both HeaderItem and MatrixItem, based on the code you shared.
If you want to combine the header and Matrix item into a single list you might use a Tuple<> instead of a nested class. I've created a sample of what that would look like and included the code I used to test your sample code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace listadd
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Original GetSchedule:");
MyListData mld = GetSchedule();
for (int i = 0; i < mld.HeaderItems.Count; i++)
{
Console.WriteLine(string.Format("HeaderItem: {0}, MatrixItem: {1}", mld.HeaderItems[i].strHeadName, mld.MatrixItems[i].nHRJobID));
}
Console.WriteLine();
Console.WriteLine("Tuple GetSchedule:");
var list = GetScheduleCombined();
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(string.Format("HeaderItem: {0}, MatrixItem: {1}", list[i].Item1.strHeadName, list[i].Item2.nHRJobID));
}
Console.WriteLine();
Console.WriteLine("combined GetSchedule:");
var clist = GetScheduleCombined2();
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(string.Format("HeaderItem: {0}, MatrixItem: {1}", clist[i].hdrItm.strHeadName, clist[i].mtxItm.nHRJobID));
}
Console.ReadKey();
}
public static List<Tuple<HeaderItem, MatrixItem>> GetScheduleCombined()
{
List<Tuple<HeaderItem, MatrixItem>> list = new List<Tuple<HeaderItem, MatrixItem>>();
for (int x = 0; x < 7; x++)
{
var h = new HeaderItem();
h.strHeadName = x;
var m = new MatrixItem();
m.nHRJobID = x;
list.Add(new Tuple<HeaderItem, MatrixItem>(h, m));
}
return list;
}
public class MyCombined
{
public MyCombined()
{
hdrItm = new HeaderItem();
mtxItm = new MatrixItem();
}
public HeaderItem hdrItm { get; set; }
public MatrixItem mtxItm { get; set; }
}
public static List<MyCombined> GetScheduleCombined2()
{
List<MyCombined> list = new List<MyCombined>();
for (int x = 0; x < 7; x++)
{
var item = new MyCombined();
item.hdrItm.strHeadName = x;
item.mtxItm.nHRJobID = x;
list.Add(item);
}
return list;
}
//----- begin original sample code from question -----
public class MyListData
{
public List<HeaderItem> HeaderItems { get; set; }
public List<MatrixItem> MatrixItems { get; set; }
}
public static MyListData GetSchedule()
{
MyListData objTab = new MyListData();
objTab.HeaderItems = new List<HeaderItem>();
//Header loop works perfectly
for (int x = 0; x < 7; x++)
{
HeaderItem objItem = new HeaderItem();
objItem.strHeadName = x;
objTab.HeaderItems.Add(objItem);
}
objTab.MatrixItems = new List<MatrixItem>();
for (int x = 0; x < 7; x++)
{
MatrixItem objItem = new MatrixItem();
objItem.nHRJobID = x;
objTab.MatrixItems.Add(objItem);
}
//Only adds the last one Need ALL
return objTab;
}
// ---- End original sample code ----
}
}
class Program
{
static void Main(string[] args)
{
Posting onjPosting = null;
List<Posting> objList = null;
for (int i = 0; i < 100; i++)
{
onjPosting = new Posting();
onjPosting.Key1 = i;
for (int j = 0; j < 5; i++)
{
Choice objChoice = new Choice();
objChoice.ID = i;
objChoice.VAL = j;
onjPosting.GetPostingChoice.Add(objChoice); // GETTING ERROR [ Object reference not set to an instance of an object. ]
}
objList.Add(onjPosting);
}
}
}
public class Choice
{
public int ID { get; set; }
public int VAL { get; set; }
}
public class Posting
{
public int Key1 { get; set; }
public List<Choice> GetPostingChoice { get; set; }
}
While looping through and assigning the value I am getting error . How to solve this ? Please help me out .
My requirement is one parent class (Posting) , can contain number of data List .
Thanks in advance .
You never allocate the GetPostingChoice list so of course it is null.
You could do it in the constructor:
public class Posting
{
public Posting()
{
GetPostingChoice = new List<Choice>();
}
public int Key1 { get; set; }
public List<Choice> GetPostingChoice { get; set; }
}
Add a public constructor on your Posting class:
public class Posting
{
public int Key1 { get; set; }
public List<Choice> GetPostingChoice { get; set; }
public Posting()
{
GetPostingChoice = new List<Choice>();
}
}
You also have other errors:
You do not initialize objList, so you cannot add in there.
List<Posting> objList = null;
So you will get another Null Reference when you get to:
List<Posting> objList = null;
In your second loop you increase i instead of j so it will never end.
for (int j = 0; j < 5; i++)
This is how it should look:
Posting onjPosting = null;
List<Posting> objList = new List<Posting>();
for (int i = 0; i < 1; i++)
{
onjPosting = new Posting();
onjPosting.Key1 = i;
for (int j = 0; j < 5; j++)
{
Choice objChoice = new Choice();
objChoice.ID = i;
objChoice.VAL = j;
onjPosting.GetPostingChoice.Add(objChoice); // GETTING ERROR [ Object reference not set to an instance of an object. ]
}
objList.Add(onjPosting);
}
Since you ask for another approach, and this is just one of the many ways you could do it, have a look at this:
List<Posting> objList = new List<Posting>();
Enumerable.Range(0,100)
.Select
(
(x,i)=>new Posting
{
Key1 = i,
GetPostingChoice = Enumerable.Range(0,5).Select((p,j)=>new Choice{ID = i,VAL = j}).ToList()
}
);
I want to fill a list<int> inside a class I can't get it to work.
(Some / Most of the code is from here
The class:
class Fiu
{
public int feleseg { get; set; }
public List<int> preferencia { get; set; }
public Fiu (int _feleseg) : this()
{
feleseg = _feleseg;
}
public Fiu()
{
this.preferencia = new List<int>();
}
}
The code:
for (int i = 1; i < 4; i++)
{
Fiu ujfiu = new Fiu(0);
for (int j = 1; j < 4; j++)
{
ujfiu.preferencia[j-1] = 1;
}
}
The main goal would be filling it from excel, but right now it doesn't even put 1-s in. I don't know what's wrong.
I get a "Argument out of range exception unhandled" error.
Replace This:
ujfiu.preferencia[j-1] = 1;
With This:
ujfiu.preferencia.Add(1);