Need explanation on these bits of codes - c#

i have recently stumbled upon a project(8-puzzle solver using A* alg) in which some codes are weird to me , because i have never seen the likes of it before .
what does this line mean ? what is this ?!
this[StateIndex]
whats this notation ? i cant undersand it at all !
i posted a sample of the class so that you can see it almost all together .
and one more question , is it not wrong to have a class implemented like StateNode? it used only a constructor to initialize its fields , and yet worst, declared them all public ! should he/she not have implemented Propertise for this task?
public enum Direction
{
Up = 1, Down = 2, Left = 3, Right = 4, UpUp = 5, DownDown = 6, LeftLeft = 7, RightRight = 8, Stop = 9
}
class StateNode
{
public int Parent;
public List<int> Childs;
public Direction Move;
public Direction ParentMove;
public byte[,] State;
public byte Depth;
public byte NullRow;
public byte NullCol;
public StateNode()
{ }
public StateNode(int NewParent, Direction NewMove, Direction ParentMove, byte NewDepth, byte NewNullRow, byte NewNullCol)
{
this.Parent = NewParent;
this.State = new byte[5, 5];
this.Move = NewMove;
this.ParentMove = ParentMove;
this.Depth = NewDepth;
this.NullRow = NewNullRow;
this.NullCol = NewNullCol;
this.Childs = new List<int>();
}
}
class StateTree : List<StateNode>
{
public static long MakedNodes;
public static long CheckedNodes;
public static byte MaxDepth;
public List<int> Successor1(int StateIndex)
{
List<int> RetNodes = new List<int>();
StateNode NewState = new StateNode();
//Up
if (this[StateIndex].NullRow + 1 <= 3 && this[StateIndex].ParentMove != Direction.Up)
{
NewState = ChangeItemState(this[StateIndex], StateIndex, Direction.Up, Direction.Down, Convert.ToByte(this[StateIndex].Depth + 1), this[StateIndex].NullRow, this[StateIndex].NullCol, Convert.ToByte(this[StateIndex].NullRow + 1), this[StateIndex].NullCol);
this.Add(NewState);
RetNodes.Add(this.Count - 1);
StateTree.MakedNodes++;
this[StateIndex].Childs.Add(this.Count - 1);
if (NewState.Depth > StateTree.MaxDepth)
StateTree.MaxDepth = NewState.Depth;
}
//Down
//Left
//Right
return RetNodes;
}
}

In your concrete case it's just access to the element, as it used inside the class that is derived from the List<T>
But it can be also indexer which enables index acces to your class object.
For example declare class like this:
public class ListWrapper
{
private List<int> list = ...
public int this[int index]
{
return list[index];
}
}
and after use it like
var lw = new ListWrapper();
//fill it with data
int a = lw[2]; //ACCESS WITH INDEX EVEN IF THE TYPE IS NOT COLLECTION BY ITSELF

this[StateIndex] is using the current class' indexer property. The indexer property is what allows you to access an element in a collection or list object as if it was an array. For instance:
List<string> strings = new List<string>();
strings.Add("Item 1");
strings.Add("Item 2");
strings.Add("Item 3");
string x = strings[0]; // Returns the first item in the list ("Item 1")
When you want to access the indexer property of your own class, however, you have to preface it with the this keyword. You'll notice that in your example, the StateTree class doesn't implement an indexer property, so that may be adding to your confusion. The reason it works is because StateTree inherits from List<StateNode> which does implement an indexer property.
But don't get confused between classes with indexer properties and arrays. Arrays are a completely different thing, though the syntax is similar. An array is a list of objects which can be accessed by an index. An indexer property is an unnamed property of a single object that acts as an array. So for instance, List<string> has an indexer property, so you can access the items it contains using the same syntax as an array index (as shown in the above example). However, you can still make an array of List<string> objects. So for instance:
List<string> strings1 = new List<string>();
strings1.Add("Item 1.1");
strings1.Add("Item 1.2");
List<string> strings2 = new List<string>();
strings2.Add("Item 2.1");
strings2.Add("Item 2.2");
List<string>[] stringsArray = new List<string>[] { strings1, strings2 };
object result;
result = stringsArray[0]; // Returns strings1
result = stringsArray[0][1]; // Returns "Item 1.2"
result = stringsArray[1][0]; // Returns "Item 2.1"
As far as StateNode goes, there's nothing technically wrong with it, and it's not unusual to have a constructor that initializes all the field values, but it's always better to use properties instead of public fields.

its Indexed Properties in C# .net .
you can check Tutorial : http://msdn.microsoft.com/en-us/library/aa288464(v=vs.71).aspx check here

this[StateIndex] is pointing to an element within the class. Because StateTree inherits from a List<T>, you have a collection that's accessible by index (in this case this[N] where N is the element's index.

this[StateIndex] is how you give a class and indexed property e.g
public class IndexedClass
{
private List<String> _content;
public IndexedClass()
{
_content = new List<String>();
}
public Add(String argValue)
{
_content.Add(argValue);
}
public string this[int index]
{
get
{
return _content[index];
}
set
{
_content[Index] = value;
}
}
}
so now you can do
IndexedClass myIndex = new IndexedClass();
myIndex.Add("Fred");
Console.Writeline(myIndex[0]);
myIndex[0] = "Bill";
Console.Writeline(myIndex[0]);
As for statenode if it's local to the class (a helper) then you could argue it as okay, I don't like it though, another ten minutes work it could be done properly. If it's public in the assembly, then it's not accpetable in my opinion. But that is an opinion.

Related

How to sort array of classes in C# by integer value?

I have a class Player that has fields name and points. i.e.
class Player { string name, int points }.
I have an array of these objects:
Program.playerList = new ArrayList();
I am displaying these objects in a Windows Forms program. I want to display them in order based on the number points a Player class has (the highest amount of points first). How do I do this?
You have an ArrayList.
First, think if you really need it.
You are going to store Player objects there, so, it would be better if it was strongly-typed array Player[] or List<Player>.
However, if you for some reasons need an ArrayList, you will need to implement IComparer.
public class PlayersByPointsComparer : IComparer
{
private readonly IComparer _baseComparer;
public int Compare(object x, object y)
{
return ((Player)x).Points - ((Player)y).Points;
}
}
// ...
Program.playerList.Sort(new PlayersByPointsComparer());
However, it there will be objects of non-Player type it will throw an exception. If it will not - then do you need an ArrayList?
If you replace in with an Array, you will be able to use Array.Sort or LINQ OrderBy method.
Array.Sort(Program.playerList, (a, b) => (a.points - b.points));
// or
Program.playerList = Program.playerList.OrderBy(x => x.Points).ToArray();
That's how you would sort it if you had a List.
Program.playerList.Sort(new PlayersByPointsComparer());
// or
Program.playerList.Sort((a, b) => (a.Points - b.Points));
// or
Program.OrderBy(a => a.Points).ToList();
Please use generic collection Type<Player>, it has some advantages in compare with ArrayList. One is type checks of array items, second is possibility of using Linq.
You can use then
Program.playerList = Program.playerList.OrderBy(p => p.points).ToList()
or (without Linq)
Program.playerList.Sort((a, b) => a.points.CompareTo(b.points));
This can be your Player class :
class Player : IComparable<Player>
{
public string Name { get; set; }
public int Points { get; set; }
public int CompareTo(Player other)
{
// Alphabetic sort if points are equal. [A to Z]
if (this.Points == other.Points)
{
return this.Name.CompareTo(other.Name);
}
// Default to points sort. [High to low]
return other.Points.CompareTo(this.Points);
}
}
And to test it:
List<Player> list = new List<Player>();
list.Add(new Player() { Name = "Player1", Points = 50 });
list.Add(new Player() { Name = "Player2", Points = 60 });
list.Add(new Player() { Name = "Player3", Points = 30 });
list.Add(new Player() { Name = "Player4", Points = 80 });
list.Add(new Player() { Name = "Player5", Points = 70 });
list.Sort();
foreach (var element in list)
{
Console.WriteLine(element.Name);
}
Here is the result:

I cannot create object that contains arrays of property inside object

I am a new developer to c# MVC3.
I have a problem that I cannot create object that contains arrays of property inside object.
For example, I want to create instance ABC.Property[] for 10 arrays.
ABC.Property[0]
ABC.Property[1]
ABC.Property[2]
.....
....
ABC.Property[10]
I used debug mode to check and found out that ABC.Property[] is null. So i cannot add the value back to that object's array.
How to crate object with propeties's array?
thank you.
namespace finance3.Models
{
public class Expected_and_Risk
{
public decimal[] Prop { get; set; }
public decimal[] Forecast { get; set; }
public string[] Name { get; set; }
public decimal[] AxB { get; set; }
public decimal[] PowAxB { get; set; }
public decimal ExpectValue(Expected_and_Risk abc)
{
decimal count = abc.Forecast.Count();
Expected_and_Risk Result = new Expected_and_Risk();
for (int i = 0 ; i < count ; i++)
{
// here is the problem
// i cannot add new data to array because it has no dimemsion and i tried this
//
// Expected_and_Risk[] Result = new Expected_and_Risk[10];
//
// but it didn't work
Result.Name[i] = abc.Name[i];
Result.Prop[i] = abc.Prop[i];
Result.Forecast[i] = abc.Forecast[i];
Result.AxB[i] = abc.Prop[i] * abc.Forecast[i];
decimal a = Result.AxB[i];
decimal sumAxB =+ a;
double temp = (double)(a * a) ;
Result.PowAxB[i] = (decimal)(temp);
}
return Convert.ToDecimal(Result);
}
}
}
You need to add a Constructor in your class and in that constructor you can define the size for your property
public class Expected_and_Risk
{
//......//your code here
public Expected_and_Risk()
{
this.Prop = new decimal[10]; // this will define an array of 10 decimal elements for Prop
}
}
Also read about object oriented programming, Also check out this article An Intro to Constructors in C#
At the moment, you've got arrays within Expected_and_Risk, but you're never initializing the variables. You could do so within a constructor:
public Expected_and_Risk(int count)
{
Name = new string[count];
...
}
... but I'd suggest that's actually a nasty design. Any time you have lots of collections, all with the same count, where x[0] corresponds to y[0] and z[0] etc, you should consider having one collection of a custom type representing the encapsulation of those properties. For example:
// Must be an int, not a decimal - you can't create an array with a decimal count
int count = abc.Forecast.Count();
// Note: rename type to follow .NET naming conventions
Expected_and_Risk[] results = new Expected_and_Risk[count];
for (int i = 0; i < count; i++)
{
results[i].Name = abc[i].Name;
...
}
... except that of course now abc would be an array too. (It's not clear whether you're really just trying to copy all the values here, or whether you've got some business logic involved. There are better ways of copying values.)
Alternatively, if you really do want collections within Expected_and_Risk, you might want to consider using List<T> instead of arrays. That way you don't need to specify the count up-front.
Better way could be to add following method into Expected_and_Risk class and call it from within constructor.
EDIT - edit is done to make Initialize private, and call it within constructor.
void Initialize(int size)
{
Prop = new decimal[size];
AxB = new decimal[size];
Forecast = new decimal[size];
PowAxB = new decimal[size];
Name = new string[size];
}
public Expected_and_Risk(int size)
{
....
Initialize(size);
}
After that use it in ExpectValue like
Expected_and_Risk Result = new Expected_and_Risk(size)// size is 10 in example;

C# Return an object from a list based on index

I need to make some changes so that I only retrieve one object at a time from my list instead of the entire list as I do now.
Right now ive got a private list in class B with a property returning the entire list basically making it public anyway and I want to change this.
The way class a looks(the ui class operating with the list) is, I enter some data validate it and send it to class B which in turn packages it into a list of objects based on the input.
Then class A needs to loop this list and add it to a listview for displaying it which looks like this at the moment:
ListViewItem lvi = new ListViewItem();
foreach ([Object] o in CLassB.getList())
{
lvi = new ListViewItem(o.property0);
lvi.SubItems.Add(o.property1);
lvi.SubItems.Add(o.property2);
lvi.SubItems.Add(o.property3);
}
}
Object is my abstract class which controls how the different types of items are added and getList() is my method in class B returning the entire list.
The thing is these propertys are the common ones all classes share the there are some that arent, like a textbox that you enter specific text about the object and so on which is displayed like this:
private void lvRegistered_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (Listview.SelectedItems.Count > 0)
{
foreach ([Object] ob in ClassB.getList())
{
if (Listview.SelectedItems[0].SubItems[0].Text == ob.id.ToString())
{
TextBox.Clear();
TextBox.Text = ob.property4;
}
}
}
}
Now this all works great at the moment but now I have a returned list to operate on but I dont want to return the list making it public I want to return one object of the list based on an index number (yes the functionality will be exactly the same, I made a method returning the count of the private list so I can loop over it and return all). This is for practicing OOP for when I dont want to return everything.
How would this be done? All I can think of is making a new list, take an int as input and search my private list and find the index then add that to the other list and return that list, but I dont know if this is good practice or the best way to do it? Well I havent looked into how to "copy" one element over to next list either but might aswell check and see if theres a better way to do things?
Just get the feeling im "taking the long way around" this way
Not sure to understand, but how about a simple GetById method?
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Test
{
private List<Foo> list = new List<Foo>();
public void Add(Foo foo)
{
this.list.Add(foo);
}
public Foo GetById(int id)
{
return this.list.FirstOrDefault(z => z.Id == id);
}
}
....
Test test = new Test();
test.Add(new Foo { Id = 1, Name = "1" });
test.Add(new Foo { Id = 2, Name = "2" });
test.Add(new Foo { Id = 3, Name = "3" });
Foo foo2 = test.GetById(2);
I'm not sure I understand completely, but it sounds like you could resolve this be creating an indexer on class B to return the item you want:
public object this[int index] {
get {
return list[index];
}
}
change 'object' to whatever your class type actually is.
You can then access the items as if class B was an array:
object item = classB[5];
1) You can use List.AsReadOnly() Method.
public ReadOnlyCollection<Double> MyList {
get {
return myList.AsReadOnly();
}
}
private List<Double> myList;
2) Use index method in the class.
public Double this[int index] {
get {
return myList[index];
}
set {
myList[index] = value;
}
}
private List<Double> myList;
Did about the same as suggested by Ken2k but instead I went with this:
public [Class] getListItem(int index)
{
return myList.ElementAt(id);
}
which in turn is used in my other class that displays the info like:
for (int i = 0; i < am.getCount(); i++)
{
ListViewItem = new ListViewItem([class reference].getList(i).[property].ToString());
ListViewItems.SubItems.Add([class reference].getList(i).[property]);
}
Thanks everyone for all your help on this.

Dynamically add values to List<double> using get & set

Is it possible to do somethink like
public class TestClass
{
public List<double> preTvoltage
{
get
{
return preTvoltage;
}
set
{
preTvoltage.Add(this); //how to add to the List??
}
}
}
The reason I want to do this (I do not know if this is a best method, just as far as my knowledge allows) because I have to get data from xml files that do not have always same number of data in them.
Later I want to fill a ListView rows and using list I can count how many items are and how many columns will be needed.
Here is a schematic of xml file:
and there are also Trigger and PostTrigger nodes in xml file with same data sorting.
and here is the listview I want to achive:
Link to full size image
So, there are some pin groups and each pingroup has lots of data, the above code I gave, was just to hold 1 of the voltage nodes in xml file.
I am pretty much listening for your ideas!
Thanks.
No, and it defies usage of properties - you should implement it as an Add (or similarly aptly named) method.
You can't add this, because this is a TestClass, not a double; and you can't add value, as otherwise suggested, because that is a List<double>, and Add requires a double.
It's not clear how you would use this, but it looks like a very bad idea to me. Setting a collection as a property is slightly unusual already, but it's even odder for that set operation to mutate the list. It's additionally weird that you're not using the value variable within the setter... why not?
You should consider what the calling code would look like, and whether that's really the clearest way of expressing the semantics you want.
set { preTvoltage.AddRange(value); }
As Jon Skeet is saying, this is not what you should do. Instead, do
TestClass t = new TestClass();
t.PreTvoltage.Add(...);
declaring the property as
public List<double> PreTvoltage
{
get { return preTvoltage; }
}
The type of a getter and setter must match.
You could have:
public List<double> preTvoltage
{
get
{
return preTvoltage;
}
set
{
preTvoltage.AddRange(value); //add all items in list assigned.
}
}
However, this seems like a bad idea as it would be confusing to users why the value got did not match the value just set. I would have the two operations as separate members, and the setter either not exist or else overwrite the existing preTvoltage entirely.
You can not implement it like this, the preferable way is to make collection controls like:
private IList<double> _preTvoltage = new List<double>();
public IEnumerable<double> preTvoltage
{
get
{
return preTvoltage.AsEnumerable();
}
}
public void AddTvoltage(double item)
{
_preTvoltage.Add(item);
}
Well I managed to solve my problem this way:
public class ITestData
{
public string pinName { get; set; } //Name of the pin
public double stressLevel { get; set; } //Stress level for latchup
public int psuCount { get; set;} //Number of PSU's
public List<double[]> preTrigger = new List<double[]>();
public List<double[]> inTrigger = new List<double[]>();
public List<double[]> postTrigger = new List<double[]>();
public void AddPreTrigger(double volt, double curr)
{
double[] data = new double[2];
data[0] = volt;
data[1] = curr;
preTrigger.Add(data);
}
public void AddInTrigger(double volt, double curr)
{
double[] data = new double[2];
data[0] = volt;
data[1] = curr;
inTrigger.Add(data);
}
public void AddPostTrigger(double volt, double curr)
{
double[] data = new double[2];
data[0] = volt;
data[1] = curr;
postTrigger.Add(data);
}
}

Array property syntax in C#

I have a a class that has an integer array property and I am trying to figure out the right syntax for it. The integer array gets instantiated in the class constructor.
class DemoClass
{
private int[] myNumbers;
public int[] MyNumbers
{
get { /* Some logic */ }
set { /* Some logic */ }
}
public DemoClass(int elements)
{
// Here, the array should get instantiated using the elements.
}
}
How does the get/set block syntax work if I want my client code to retrieve a number from the array through the property MyNumbers?
How can I send it the right index?
What do I have to initialize?
Are you looking for:
class DemoClass
{
public int[] MyNumbers { get; private set; }
public DemoClass(int elements)
{
MyNumbers = new int[elements];
}
}
As for normal properties that do nothing except publicize a private field (as you seem to want):
private int[] myNumbers;
public int[] MyNumbers
{
get { return myNumbers; }
set { myNumbers = value; }
}
CA1819: Properties should not return arrays
http://msdn.microsoft.com/en-us/library/0fss9skc.aspx
Arrays returned by properties are not write-protected, even if the property is read-only. To keep the array tamper-proof, the property must return a copy of the array. Typically, users will not understand the adverse performance implications of calling such a property. Specifically, they might use the property as an indexed property.
To fix a violation of this rule, either make the property a method or change the property to return a collection instead of an array
If the number of element in the array is fixed, I would only provide a getter for the array and leave off the setter. You will still be able to assign values to individual elements in the array, but this will prevent someone from swapping the whole array out from under you (or setting it to null. The code would look like this:
class DemoClass
{
public int[] MyNumbers
{ get; private set; }
public DemoClass(int elements)
{
MyNumbers = new int[elements];
}
}
If the number of elements are not fixed, then you should use a List<int> rather than an array, and then you definitely want a property with no setter.
class DemoClass
{
private int[] myNumbers;
public int[] MyNumbers
{
get { return myNumbers; }
set { myNumbers = value; }
}
public DemoClass(int[] elements)
{
myNumbers = elements;
// Here, the array should get instantiated using the elements.
}
}
It is called Auto-Implemented Properties . So if you have syntax like
public int[] MyNumbers { get; set; }
C# compiler will automatically create for you backing field. This feature was introduced in C# 3.0, and before that you always had to implement property with backing field.
You can read more at: New C# "Orcas" Language Features: Automatic Properties, Object Initializers, and Collection Initializers
class DemoClass
{
private int[] myNumbers;
public int[] MyNumbers
{
get { return myNumbers; }
set { myNumbers = value;}
}
public DemoClass(int elements)
{
// Here, the array should get instantiated using the elements.
MyNumbers = new int[5] { 1, 2, 3, 4, 5};
}
}

Categories