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);
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();
}
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;
}
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 am trying to make a game where an image appears, and if it is not clicked the image should disappear. I need help giving my array a value of three, then subtract it in another method.
Code:
NameCount = -1;
NameCount++;
Grid.SetColumn(mole, ranCol);
Grid.SetRow(mole, ranRow);
grid_Main.Children.Add(mole);
for (int i = 0; i < NumofImages; i++)
{
//Where I must give a value to the array of the array to 3 for every image that appears.
}
//Where I am trying to make the image disappear after 3 seconds.
private void deleteMole()
{
NumofImages = TUtils.GetIniInt(Moleini, "NumPictures", "pictures", 8);
NumberofImages = Convert.ToInt32(NumofImages);
for (int j = 0; j < NumofImages; j++)
{
CounterArray[j]--;
if (CounterArray[j] == 0)
{
//Not Sure How to delete image
Thanks for the help!
You could keep track of the images in another array.
After you add the image to the view you should also add it to the array:
images[j] = mole;
Then later:
if (CounterArray[j] == 0)
{
grid_Main.Children.Remove(images[j]);
}
But using static arrays and separating data is not a good idea.
If you can you should better aggregate all the metadata and the image together in the same structure:
class Mole
{
public int Counter { get; set; }
public Control Image { get; set; }
}
and manage them in a single List<Mole>; adding and removing them will be simpler.
Here is some code that illustrates the idea (won't compile):
class Mole
{
public int X { get; set; }
public int Y { get; set; }
public int Counter { get; set; }
public Control Image { get; set; }
public bool IsNew { get; set; }
}
class Test
{
IList<Mole> moles = new List<Mole>();
private static void AddSomeMoles()
{
moles.Add(new Mole{ X = rand.Next(100), Y = rand.Next(100), Counter = 3, Image = new PictureBox(), IsNew = true });
}
private static void DisplayMoles()
{
foreach (Mole mole in moles)
{
if (mole.IsNew)
{
grid_Main.Children.Add(mole.Image);
mole.IsNew = false;
}
}
}
private static void CleanupMoles()
{
foreach (Mole mole in moles)
{
mole.Counter -= 1;
if (mole.Counter <= 0)
{
grid_Main.Children.Remove(mole.Image);
moles.Remove(mole);
}
}
}
static void Main()
{
while (true)
{
AddSomeMoles();
DisplayMoles();
Thread.Sleep(1000);
CleanupMoles();
}
}
}
If you want to give every element in a List a certain value, use a foreach loop. In this case, it would look like:
foreach(int currentElement in CounterArray)
{
currentElement = 3;
}
This will loop through each element of the List and set it to 3.
EDIT: If you're using an array, which you are, you would do the following:
for (int i = 0; i < CounterArray.Length; i++)
{
CounterArray[i] = 3;
}