list class property in a class c# assign value - c#

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()
}
);

Related

For loop takes the the max count

private List<SurveyDetail> GetSurveyDetails()
{
List<SurveyDetail> surveyDetails = new List<SurveyDetail>();
SurveyDetail detail = new SurveyDetail();
int cid = 0;
for (int i = 1; i < 3; i++)
{
detail.choiceId = "1";
detail.choiceDesc = "tesT";
detail.questionId = i.ToString();
surveyDetails.Add(detail);
}
return surveyDetails;
}
public class SurveyDetail
{
public string questionId { get; set; }
public string choiceId { get; set; }
public string choiceDesc { get; set; }
}
when I run the code it question Id always gives me the last number of i that was run for example, in this case, it gives me 2. It gives me 2 on both counts.
Where I want the questionid to be 1 in the first count and 2 in the second.
You should move SurveyDetail initialization into the for body
private List<SurveyDetail> GetSurveyDetails()
{
List<SurveyDetail> surveyDetails = new List<SurveyDetail>();
int cid = 0;
for (int i = 1; i < 3; i++)
{
SurveyDetail detail = new SurveyDetail();//<==NOTE THIS
detail.choiceId = "1";
detail.choiceDesc = "tesT";
detail.questionId = i.ToString();
surveyDetails.Add(detail);
}
return surveyDetails;
}
Incase you want to use : LINQ
var result = Enumerable.Range(1, 3).Select(detail => new SurveyDetail
{
choiceId = "1",
questionId = detail.ToString(),
choiceDesc = "testT",
});

NPE when trying to initialize an internal class array

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();
}

Bind a linkedlist to a datagridview

I'm trying to bind a linkedlist to a datagridview. The method below works for the properties in the class except the array.
If I declare the array as a new instance the linkedlist is created correctly, but the array is not bound into the datagridview.
If the array is created as a property (I think the code is correct) causes An unhandled exception of type 'System.StackOverflowException' occurred when the linkedlist is created.
Thanks for any help.
public class PayoffNode
{
public int DealNo { get; set; }
public string Category { get; set; }
public string Strategy { get; set; }
public string GreekType { get; set; }
// declare array as instance or as a property?
//public double[] Data = new double[22];
public double[] Data
{
get { return Data; }
set { Data = value; }
}
}
LinkedList<Globals.PayoffNode> PayLL = new LinkedList<Globals.PayoffNode>();
Random Rnd = new Random();
for (int K = 1; K <= 10; K++)
{
var T = new Globals.PayoffNode();
T.Category = "Account==" + K;
T.GreekType = "Greek==" + K;
T.DealNo = K;
T.Strategy = "Strategy==" + K;
for (int DP = 1; DP <= 21; DP++)
{
T.Data[DP] = Rnd.Next(10, 99);
}
PayLL.AddLast(T);
}
List<Globals.PayoffNode> qP = (from P in PayLL
where P.Category == "Account==4" && P.DealNo == 4 && P.GreekType == "Greek==4" && P.Strategy == "Strategy==4"
select P).ToList();
PayoffTable.DataSource = qP;
Update:
Thanks for the comments, this seems to be working.
public class PayoffNode
{
public int DealNo { get; set; }
public string Category { get; set; }
public string Strategy { get; set; }
public string GreekType { get; set; }
public double Data1 { get; set; }
public double Data2 { get; set; }
public double Data3 { get; set; }
public double[] Data = new double[22];
}
LinkedList<Globals.PayoffNode> PayLL = new LinkedList<Globals.PayoffNode>();
Random Rnd = new Random();
for (int K = 1; K <= 10; K++)
{
var T = new Globals.PayoffNode();
T.Category = "Account==" + K;
T.GreekType = "Greek==" + K;
T.DealNo = K;
T.Strategy = "Strategy==" + K;
for (int DP = 1; DP <= 21; DP++)
{
T.Data[DP] = Rnd.Next(10, 99);
}
PayLL.AddLast(T);
}
List<Globals.PayoffNode> qP = (from P in PayLL
where P.Category == "Account==4" && P.DealNo == 4 && P.GreekType == "Greek==4" && P.Strategy == "Strategy==4"
select new Globals.PayoffNode()
{
Category=P.Category,
DealNo=P.DealNo,
GreekType=P.GreekType,
Strategy=P.Strategy,
Data1=P.Data[1],
Data2 = P.Data[2],
Data3 = P.Data[3],
}).ToList();
PayoffTable.DataSource = qP;
One way to avoid making 21 Data properties is to convert the List to DataTable:
class PayoffNode
{
public int DealNo;
public string Category;
public double[] Data; // = new double[21];
}
and then
Random Rnd = new Random();
List<PayoffNode> PayLL = Enumerable.Range(1, 10).Select(i => new PayoffNode {
DealNo = i,
Category = "Account==" + i,
Data = Enumerable.Range(1, 21).Select(d => (double)Rnd.Next(10, 99)).ToArray()
}).ToList();
// List<PayoffNode> to DataTable
var dt = new DataTable();
dt.Columns.Add("DealNo", typeof(int));
dt.Columns.Add("Category"); // typeof(string) by default
for (int i = 1; i <= 21; i++)
dt.Columns.Add("Data" + i, typeof(double));
foreach (var P in PayLL)
{
var dr = dt.Rows.Add(P.DealNo, P.Category);
for (int i = 0; i < 21; i++)
dr[i+2] = P.Data[i]; // +2 is the number of fields before the Data fields
}
PayoffTable.DataSource = dt;
dt.DefaultView.RowFilter = " Category = 'Account==4' ";
The advantage is that you set the DataSource only once and just change the RowFilter to filter it. Also, any changes made in the DataGridView change the DataTable and vice-versa.
Note that arrays in C# and most other languages start from 0 and not from 1 ( .Data[0] to access the first item in the array ), so the for loop to access the Data array in my example is from 0 to 20.
public double[] Data
{
get { return Data; }
set { Data = value; }
}
This is the problem, the object in the set and get method needs to refer to a different object otherwise whenever the object value is set or trying to be retrieved it will just keep calling these methods infinitely. should be...
private double[] _data;
public double[] Data
{
get { return _data; }
set { _data = value; }
}
or...
public double[] Data { get; set; }

List inside Class

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;
}

Adding data to a list inside a class

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);

Categories