encapsulation of an array of objects c# - c#

I would like to create an array of objects. Each object has it's own int array.
For each object I assign values to it's array ONLY with keys given by myself (example: li[i].V[10] = 1; li[i].V[50] = 10; )
Can someone tell me how to do that? Can I do that without using Lists?
The second case is analogical to first. I would like to know how to assign values of object's List
using setter.
I tried to do that by myself. Unfortunately My code crashed cuz I don't know how to set the dimension of V and Word:
class CFiles
{
//private int[] v=new int[5];//dont want to specify the dimention of array here
private int[] v;//vector of file
private List<string> words;
public CFiles()
{
words = Words;
v = new int[50];
v = V;
}
public int[] V { get; set; }
public List<string> Words { get; set; }
}
class Program
{
static void Main(string[] args)
{
CFiles[] li = new CFiles[2];
for(int i=0;i<li.Length;i++)
{
li[i]=new CFiles();
li[i].V[10] = 1;
li[i].V[50] = 10;
li[i].V[50] = 15;
li[i].Words.Add("a");
li[i].Words.Add("ab");
li[i].Words.Add("abc");
}
for (int i = 0; i < li.Length; i++)
{
for(int j=0;j<li[i].V.Length;j++)
{
Console.WriteLine(li[i].V[j]);
}
}
Console.WriteLine();
}
}

Your constructor isn't right and your properties aren't quite right. You might want something more like this:
class CFiles
{
//private int[] v=new int[5];//dont want to specify the dimention of array here
private int[] v;
public int[] V { get { return v; } set { v = value; } }
private List<string> words;
public List<string> Words { get { return words; } set { words = value; } }
public CFiles()
{
words = new List<string>();
v = new int[51]; //needs to be 51 if you are going to assign to index 50 below
}
}
Other than those issues, your code seems to do what you want. You have an array of objects where each object has its own int array (in addition to a string of strings).
Is that not what you want?

Related

All items in my list update but i only want to add a single item

In this script i sort my list with my own script and i want to add all the state of my list in a list of position
This is where i add my item in my list :
private List<int> values;
public List<int> Values { get => values; private set => values = value; }
static Position p;
public List<Position> _lp;
public Insertiontri()
{
_lp = new List<Position>();
}
public override void Sort()
{
int en_cours;
int i, j;
for (i = 0; i < 20; i++)
{
en_cours = Values[i];
for (j = i; j > 0 && Values[j - 1] > en_cours; j--)
{
Values[j] = Values[j - 1];
}
Values[j] = en_cours;
addValuesToPos();
}
}
private void addValuesToPos()
{
List<int> ints = Values;
p = new Position(ints);
_lp.Add(p);
}
And this is my class
public class Position
{
private List<int> _position;
public List<int> position { get => _position; set => _position = value; }
public Position(List<int> p)
{
position = p;
}
}
and the Values is generated with this function
public override void Random()
{
Values = Enumerable.Range(1, 20) // la plage de nombres dans ta collection,
.OrderBy(x => Guid.NewGuid()) // ordonné par rapport à un guid,
.ToList();
_lp.Add(new Position(Values));
}
You use the same List<int> for all the Positions.
In order to simplify and illustrate the issue we need to do some clean up. We will remove Position that is just a wrapper around List<int>.And remove unnecessary properties.
We have a class that store a List<int> and a list of snapshoot of that list at each step or the sorting process.
public class SortingAlgo_StepByStepDemo {
public List<int> Source {get;set;}
public List<List<int>> Steps {get;set;}
...
}
The Sorting algorytm and implementation stay the same.
We just need to change the way we stored the picture/snapshoot of the "step".
public void SaveStep()
{
// It's the same.
// Steps.Add(Source.ToList());
Steps.Add(new List<int>(Source));
}
If we had Steps.Add(Source) like you had. It will save not a picture but the reference for the actual List source. And every elements of Steps will just point to the same thing.
Live demo : https://dotnetfiddle.net/9sbezB
I added a Ctor that take a List<int> Source. So you can test on fixed Input.

Having trouble creating random values

So I created Pairs class that contains int and double and I want to create an array of them with my
array class by creating random values, but I'm getting System.NullReferenceException at Line 19 of
my array class.
Here's my pair class
class Pair
{
public int integer = 0;
public double doubl = 0.0;
public Pair(int integer, double doubl)
{
this.integer = integer;
this.doubl = doubl;
}
public Pair()
{
}
public int Integer() { return integer; }
public double Doubl() { return doubl; }
}
And this is my array class and the abstract class
class MyDataArray : DataArray
{
Pair[] data;
int operations = 0;
public MyDataArray(int n, int seed)
{
data = new Pair[n];
Random rand = new Random(seed);
for (int i = 0; i < n; i++)
{
data[i].integer = rand.Next(); //I get error here
data[i].doubl = rand.NextDouble();
}
}
public int integer(int index)
{
return data[index].integer;
}
public double doubl(int index)
{
return data[index].doubl;
}
}
abstract class DataArray
{
public int operations { get; set; }
public abstract void Swap(int i, int j);
public abstract void Swap2(int i, int high);
}
Also is it even worth it using this abstract class I used this from a reference that my university
provided. I have to create an quicksort algorithm that sorts pairs in arrays and linked lists and analyze it.
data = new Pair[n];
This creates a new array of null references.
The loop should be
for (int i = 0; i < n; i++)
{
data[i] = new Pair(rand.Next(), rand.NextDouble())
}
While we are looking at your code: you are making a good attempt here to make an immutable pair, but it could be better. What you want is:
class Pair
{
public Pair(int integer, double doubl)
{
this.Integer = integer;
this.Double = doubl;
}
public int Integer { get; private set; }
public double Double { get; private set; }
}
Shorter, safer, clearer.
The issue with your code is that you are only initializing the array of data in MyDataArray. When creating an array of instances, it only initializes references for the array, not the actual instances to be in the array. Those references all point to null. So when you do try to set the integer member of the i'th Pair instance in the data array:
...
data[i].integer = rand.Next();
...
You are actually trying to set the integer member of null, which does not exist.
...
null.integer = rand.Next();
...
To fix this, simply create a new instance of Pair for each index of data in your loop.
...
for (int i = 0; i < n; i++)
{
data[i] = new Pair();
data[i].integer = rand.Next();
data[i].doubl = rand.NextDouble();
}
...
Even better, you can use the constructor you've made where it takes parameters to set integer and doubl upon construction to simplify the code in your loop.
...
for (int i = 0; i < n; i++)
{
data[i] = new Pair(rand.Next(), rand.NextDouble());
}
...

How to maintain instance number in list of objects

I have a class baseClass, and a list of objects of the baseClass. What i want to achieve is that i have to dynamically assign the instance number to each object in the list. for that what am doing is that use a constructor to do this.
Following is the class definition:
public class baseClass
{
private int _InstanceNumber;
private int _MyIntVal;
private string _MyString;
public string MyString
{
get { return _MyString; }
set { _MyString = value; }
}
public int MyIntVal
{
get { return _MyIntVal; }
set { _MyIntVal = value; }
}
public int MyProperty
{
get { return _InstanceNumber; }
}
public baseClass(int instance)
{
_InstanceNumber = instance;
}
}
The creation of the List of objects is as follows:
int instanceNumber = 0;
List<baseClass> classList = new List<baseClass>();
classList.Add(new baseClass(instanceNumber++) { MyString = "sample1", MyIntVal = 10 });
classList.Add(new baseClass(instanceNumber++) { MyString = "sample2", MyIntVal = 11 });
I know it is not the actual way for creating this. it does not give the index number actually. how can i calculate the instance number?
Consider the following scenario, that am creating another list of objects then it hard to maintain the instance number. or if i create another object(this also be an instance) external to the list.
int instanceNumber = 0;
List<baseClass> anotherClassList = new List<baseClass>();
classList.Add(new baseClass(instanceNumber++) { MyString = "sample1", MyIntVal = 10 });
classList.Add(new baseClass(instanceNumber++) { MyString = "sample2", MyIntVal = 11 });
Updates:
This is my temporary solution for this. i need proper way/ method to maintain instance number
If you want to find the index of item in the list, you should ask it from the list, not the item like:
var index = list.IndexOf(item);
But it seems that you expect the item to be aware of its position in the list. In order to do this, you should pass the list to the item so it can use it to find its own place in it:
public class Item
{
private List<Item> _containerList;
public Item(List<Item> containerList)
{
_containerList = containerList;
}
public int InstanceNumber
{
get { return _containerList.IndexOf(this); }
}
}
and change your code to:
List<Item> classList = new List<Item>();
classList.Add(new Item(classList ) { ... });
classList.Add(new Item(classList ) { ... });

Store array of classes in a list

I have a class as below,
class EUInput
{
public EUInput()
{
RtID = 0;
}
public int RtID { get; set; }
}
I want to store this class with different RtID values in a list. I tried as below,
static void Main(string[] args)
{
EUInput clsEUInput = new EUInput();
List list = new List();
for (int i = 0; i < 5; i++)
{
clsEUInput.RtID = i;
list.Add(clsEUInput);
}
foreach (EUInput obj in list)
{
Console.WriteLine(obj.RtID.ToString());
}
Console.ReadLine();
}
I am getting an output as
4
4
4
4
4
But I need an outupt as
0
1
2
3
4
You need to move the declaration of clsEUInput inside the for loop. Right now, there is only one EUInput object and you're adding the same object to the list multiple times.
List list = new List();
for (int i = 0; i < 5; i++)
{
EUInput clsEUInput = new EUInput();
clsEUInput.RtID = i;
list.Add(clsEUInput);
}
Change EUInput to be a struct (and keep your Main method as it is):
public struct EUInput
{
public int RtID;
}
A struct is a value type (a class is a reference type), so when you add it to a list, you basically add a "copy" of the whole structure (and not just a reference). So when you keep changing the RtID in the loop, you still change that one object you created, but the objects in the list won't be affected.
Either your boss is playing a trick on you, i.e. want's to test your knowledge about value types and reference types, or he doesn't know about the difference between them himself...
you Need new instances to the class
or the complete list will hold references to the one instance
private class EUInput
{
public EUInput()
{
RtID = 0;
}
public int RtID { get; set; }
}
//I want to store this class with different RtID values in a list. I tried as below,
private static void Main(string[] args)
{
List<EUInput> list = new List<EUInput>();
for (int i = 0; i < 5; i++)
{
EUInput clsEUInput = new EUInput();
clsEUInput.RtID = i;
list.Add(clsEUInput);
}
foreach (EUInput obj in list)
{
Console.WriteLine(obj.RtID.ToString());
}
Console.ReadLine();
}

Assignment to struct array inside method does not work in C#?

Here is the code snippet from my LinqPad:
public class Elephant{
public int Size;
public Elephant()
{
Size = 1;
}
}
public struct Ant{
public int Size;
}
private T[] Transform2AnotherType<T>(Elephant[] elephantList)
where T:new()
{
dynamic tArray = new T[elephantList.Length];
for (int i = 0; i < elephantList.Length; i++)
{
tArray[i] = new T();
tArray[i].Size = 100;
//tArray[i].Dump();
}
return tArray;
}
void Main()
{
var elephantList = new Elephant[2];
var elephant1 = new Elephant();
var elephant2 = new Elephant();
elephantList[0] = elephant1;
elephantList[1] = elephant2;
elephantList.Dump();
var r = Transform2AnotherType<Ant>(elephantList);
r.Dump();
}
I want to change one object array of known type,Elephant,to another object array of type T. T is not a class,but limited to struct which
provided by the already existed API.And every instance of type T shares some common property,says Size,but also has their own particular property which
I have omitted in my example code.So I put dynamic keyword inside the Transform2AnotherType<T>.
And I could not even to use Dump to make sure if the assignment has made effect,thus will throw RuntimeBinderException.
My question is: how to correctly make the assignment in such a struct array and return it back properly?
I suggest change your code like this:
public class Elephant
{
public Elephant()
{
Size = 1;
}
public int Size { get; set; }
}
public struct Ant
{
public int Size { get; set; }
}
private static T[] Transform2AnotherType<T>(Elephant[] elephantList)
where T : new()
{
T[] tArray = new T[elephantList.Length];
for (int i = 0; i < elephantList.Length; i++)
{
dynamic arrayElement = new T();
arrayElement.Size = 100;
tArray[i] = arrayElement;
//tArray[i].Dump();
}
return tArray;
}
static void Main()
{
var elephantList = new Elephant[2];
var elephant1 = new Elephant();
var elephant2 = new Elephant();
elephantList[0] = elephant1;
elephantList[1] = elephant2;
//elephantList.Dump();
var r = Transform2AnotherType<Ant>(elephantList);
//r.Dump();
}

Categories

Resources