I have the following property Class:
public class Ctas
{
private string _CodAgrup;
public string CodAgrup
{
get { return _CodAgrup; }
set { _CodAgrup = value; }
}
private string _NumCta;
public string NumCta
{
get { return _NumCta; }
set { _NumCta = value; }
}
private string _Desc;
public string Desc
{
get { return _Desc; }
set { _Desc = value; }
}
private string _subctade;
public string SubCtaDe
{
get { return _subctade; }
set { _subctade = value; }
}
private string _Nivel;
public string Nivel
{
get { return _Nivel; }
set { _Nivel = value; }
}
private string _Natur;
public string Natur
{
get { return _Natur; }
set { _Natur = value; }
}
public override string ToString()
{
return "CodAgrup = " + CodAgrup + ", NumCta = " + NumCta + ", Desc = " + Desc + ", SubCtaDe = " + SubCtaDe + ", Nivel = " + Nivel + ", Natur = " + Natur;
}
#endregion
}
and I have Create an XML from these properties, so first I have to fill the properties, then i got the next method i want to use to fill the properties, first question is, is it correct the way Im using to fill the properties?
Then I should retreive the data and write it on an XML file so I convert properties data into a list and then just write them as atributes but when i Debug, I get that the list is empty, Why is that? what could be the best way to do it?
//Insert n data on properties
static void cuenta(string codagroup, string numcta, string desc, string subctade, string nivel, string natur)
{
Ctas cuentas = new Ctas();
int x = 0;
while (cuentas.CodAgrup != null)
{
cuentas.CodAgrup.Insert(x, "codagroup");
cuentas.NumCta.Insert(x, "numcta");
cuentas.Desc.Insert(x, "desc");
cuentas.SubCtaDe.Insert(x,"subctade");
cuentas.Nivel.Insert(x, "nivel");
cuentas.Natur.Insert(x, "natur");
x = x + 1;
}
}
//Converting propierties data into list
List<string> coda = cuentas.CodAgrup.GetType().GetProperties().Select(p => p.Name).ToList();
List<string> ncta = cuentas.NumCta.GetType().GetProperties().Select(p => p.Name).ToList();
List<string> desc = cuentas.Desc.GetType().GetProperties().Select(p => p.Name).ToList();
List<string> subdes = cuentas.SubCtaDe.GetType().GetProperties().Select(p => p.Name).ToList();
List<string> nivel = cuentas.Nivel.GetType().GetProperties().Select(p => p.Name).ToList();
List<string> natur = cuentas.Natur.GetType().GetProperties().Select(p => p.Name).ToList();
//Create XML from data in list´s
for (int i = 0; i < coda.Count; i++)
{
xmlWriter.WriteAttributeString("CodAgrup", coda[i]);
xmlWriter.WriteAttributeString("NumCta", ncta[i]);
xmlWriter.WriteAttributeString("Desc", desc[i]);
//write the atribute when property data exists.
if (cuentas.SubCtaDe != null)
{
xmlWriter.WriteAttributeString("SubCtaDe", subdes[i]);
}
xmlWriter.WriteAttributeString("Nivel", nivel[i]);
xmlWriter.WriteAttributeString("Natur", natur[i]);
xmlWriter.WriteEndElement();
}
Your code is confusing, but if I understand it right, here is the first error I see:
Ctas cuentas = new Ctas();
int x = 0;
while (cuentas.CodAgrup != null) // cuentas.CodAgrup is null from the beginning!
{
cuentas.CodAgrup.Insert(x, "codagroup");
cuentas.NumCta.Insert(x, "numcta");
cuentas.Desc.Insert(x, "desc");
cuentas.SubCtaDe.Insert(x,"subctade");
cuentas.Nivel.Insert(x, "nivel");
cuentas.Natur.Insert(x, "natur");
x = x + 1;
}
Since you are looking at a brand-new Ctas object, and there is no code to initialize the CodAgrup property, it will have the default value of null, so the code never enters the while loop.
Even if it DID, I suspect it would be an endless loop, because you're Inserting a literal value into a string property, and there is no condition I see where cuentas.CodAgrup will ever be null.
As for your XML generation, why not just use the built in XmlSerializer class? Even if you require a specific format, there are attributes that let you customize the XML that is generated.
Related
I have an issue with my application.
Using the debugger I've found that my application is stuck in an infinite loop. I have attached an image to demonstrate the error which occurs when I try to execute the program.
When executed
However, when debugging I've come to the conclusion this is an issue with either my CompareTomethod or my InsertItem method in my AVLTree class.
The program appears to loop around the following line of codes-
In my Country class-
public int CompareTo(object obj)
{
Country temp = (Country)obj;
return name.CompareTo(temp.name);
}
And in my AVLTree class (doesn't go past the code I have attached)-
private void insertItem(T item, ref Node<T> tree)
{
if (tree == null)
{
tree = new Node<T>(item);
}
else if (item.CompareTo(tree.Data) < 0)
{
insertItem(item, ref tree.Left);
}
I am reading in data from a CSV file, however, this occurs when the application reads in the fourth entry in the CSV File.
I can insert the following countries - USA, Canada, Brazil but Chile (the next record) appears to run into this issue. I was wondering what the problem could be?
I am inserting this data with this piece of code:
avlTree.InsertItem(new Country(columns[0], float.Parse(columns[1]), float.Parse(columns[2]), float.Parse(columns[3]), Int32.Parse(columns[4]), tradingPartners));
I have the loop to get the trading partners and the code to read in the file as well, just cannot seem to get out of this loop. Where could the issue lie?
I have the following classes-
AVLTree
BSTree
BinTree
Country
Node
Form
Program
Thank you for your time.
UPDATE - My whole Country class where my CompareTo method is-
private string name;
private float GDP;
private float inflation;
private float tradeBal;
private int HDI;
public LinkedList<String> tradingPartners;
public string x;
public Country()
{
}
public Country(string name, float GDP, float inflation, float tradeBal, int HDI, LinkedList<String> tradingPartners)
{
this.name = name;
this.GDP = GDP;
this.inflation = inflation;
this.tradeBal = tradeBal;
this.HDI = HDI;
this.tradingPartners = tradingPartners;
}
public string CountryName
{
get { return name; }
set { name = value; }
}
public float GDPGrowth
{
get { return GDP; }
set { GDP = value; }
}
public float Inflation
{
get { return inflation; }
set { inflation = value; }
}
public float TradeBalance
{
get { return tradeBal; }
set { tradeBal = value; }
}
public int HDIRank
{
get { return HDI; }
set { HDI = value; }
}
public LinkedList<String> TradingPartners
{
get { return tradingPartners; }
set { tradingPartners = value; }
}
public int CompareTo(object obj)
{
Country temp = (Country)obj;
return name.CompareTo(temp.name);
}
public override string ToString()
{
foreach (string y in TradingPartners)
{
x += y + ", ";
}
return name + ". " + GDP + ", " + inflation + ", " + tradeBal + ", " + HDI + "[ " + x + " ]";
}
I hope this can be of help, I don't think it's my AVLTree as I used to once before and no issues arised. I forgot to add, the code continuously loops back and forth between my CompareTo and my insertItem method (up to where I've pasted it in, will not go further than that).
Goal:
Retrieve a string value that is "1_2_3" om the code myListAnimals. In the future, the value can be random.
I need to add a "_" between numbers.
Problem:
I don't know how to do it by using LINQ?
public class Animal
{
private void int _number;
private void string _name;
private bool display;
public int Number
{
get { return _number;}
set { _number = value; }
}
public int Name
{
get { return _name;
set { _name = value; }
}
public bool Display
{
get { return display;
set { display = value; }
}
}
List<Animal> myListAnimal = new List<Animal>
Animal myAnimal = new List<Animal>
myAnimal.Number = 1;
myAnimal.Name = "Dog";
myAnimal.Display = True;
myAnimals.add(myAnimal )
Animal myAnimal2 = new List<Animal>
myAnimal2.Number = 2;
myAnimal2.Name = "Cat";
myAnimal2.Display = True;
myAnimals.add(myAnimal2)
Animal myAnimal3 = new List<Animal>
myAnimal3.Number = 3;
myAnimal3.Name = "Pig";
myAnimal3.Display = True;
myAnimals.add(myAnimal3)
Animal myAnimal4 = new List<Animal>
myAnimal4.Number = 4;
myAnimal4.Name = "Sheep";
myAnimal4.Display = false;
myAnimals.add(myAnimal4)
Note: Your code sample isn't valid C#. I assume that you can fix that (it's pretty simple basic changes that need to be made). That said:
Yes, you can use LINQ to concatenate strings, which is ultimately what you're doing.
var concat = myListAnimal
.Where(a => a.Display)
.Select(a => a.Number.ToString())
.Aggregate((current, next) => current + "_" + next);
Console.WriteLine(concat);
Would output with your data:
1_2_3
Where() filters the values where Display != true
Select() projects the number values to a sequence of strings
and Aggregate() does the concatenation.
your code is not valid. First fix it and try this.
var concat =string.Join("_", myListAnimal.Select(a => a.Number).ToArray());
Try using StringBuilder and ForEach extension method.
StringBuilder sb = new StringBuilder();
myAnimals.ForEach(x=> sb.AppendFormat("{0}_",x.Number));
I am relatively new to C# (WinForms), and had a question regarding combo boxes. I have a combo box of Reviewer objects (it is a custom class with an overridden ToString method) and am currently attempting to go through all the checked items and use them to generate a setup file.
Here is how the combo box is populated (populated on form load). Parameters is just a collection of linked lists and parsing code.
for (int i = 0; i < parameters.GetUsers().Count; i++)
{
UserList.Items.Add(parameters.GetUsersArray()[i], parameters.GetUsersArray()[i].isSelected());
}
Here is how I am trying to read it. setup is a StringBuilder. The problem is that GetID is not defined. Does the add function above cast the Reviewer object to a Object object? It looks a little funny since it creates a file fed into a Perl script. A sample desired output line looks like this: inspector0 => "chg0306",
for (int i = 0; i < UserList.CheckedItems.Count; i++)
{
setup.AppendLine("inspector" + i.ToString() + " => \t \"" +
UserList.CheckedItems[i].GetID() + "\",");
}
Here is the users class: (Sample User is ID = aaa0000 name: Bob Joe)
public class Reviewer
{
private string name;
private string id;
private bool selected;
public Reviewer(string newName, string newID, bool newSelected)
{
name = newName;
id = newID;
selected = newSelected;
}
public string GetName()
{
return name;
}
public override string ToString()
{
//string retVal = new string(' ', id.Length + name.Length + 1);
string retVal = id + '\t' + name;
return retVal;
}
public string GetID()
{
return id;
}
public bool isSelected()
{
return selected;
}
}
For posterity, here is the Parameters class:
public class ParameterLists
{
public ParameterLists()
{
projects = new LinkedList<string>();
reviewers = new LinkedList<Reviewer>();
}
public enum FileContents {
PROJECT_LIST,
USERS_LIST,
}
public LinkedList<Reviewer> GetUsers()
{
return reviewers;
}
public LinkedList<string> GetProjects()
{
return projects;
}
public Reviewer[] GetUsersArray()
{
Reviewer[] userArray = new Reviewer[reviewers.Count];
reviewers.CopyTo(userArray, 0);
return userArray;
}
public string[] GetProjectsArray()
{
String[] projectArray = new String[projects.Count];
projects.CopyTo(projectArray, 0);
return projectArray;
}
public void LoadParameters(string fileName)
{
//Reads the parameters from the input file.
}
private void CreateDefaultFile(string fileName)
{
// Create the file from the defaultfile , if it exists.
// Otherwise create a blank default file.
}
private LinkedList <string> projects;
private LinkedList <Reviewer> reviewers;
}
I am probably missing something simple, coming from embedded C++. Any help would be appreciated.
You have to cast that object:
((Reviewer)UserList.CheckedItems[i]).GetID()
Finding an Index of a Class:
The only way I know to find an index of List is
int index = listEmployee.FindIndex(
delegate(Employee findEmployee)
{
return findEmployee.Name.Equals(findName, StringComparison.Ordinal);
});
I was wondering how to add the option to use
int indexT = listEmployee.FindIndex(r >= r.Name == findName);
Or basically what I'm doing wrong that I can't use it.
class Employee
{
private string _name; private int _idNumber;
private string _deptarment; private string _position;
public Employee()
{
_name = ""; _idNumber = 0; _deptarment = ""; _position = "";
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public int IdNumber
{
get { return _idNumber; }
set { _idNumber = value; }
}
public string Department
{
get { return _deptarment; }
set { _deptarment = value; }
}
public string Position
{
get { return _position; }
set { _position = value; }
}
}
I was wondering how to add the option to use
int indexT = listEmployee.FindIndex(r >= r.Name == findName);
That's fine, apart from the syntax problem at r >= which should be r =>
So this works:
int indexT = listEmployee.FindIndex(r => r.Name == findName);
See: Lambda Expressions
int indexT = listEmployee.FindIndex(r => r.Name == findName);
should work. Perhaps you are missing the using System.Linq referece
Not entirely sure what you're trying to accomplish, but a simple List collection is not going to ensure order or sort, so the index (especially if the collection is going to be expected to change) is not a reliable means of accessing a specific object.
If index / order is important, maybe look at a different collection type, such as the Sorted list: http://msdn.microsoft.com/en-us/library/system.collections.sortedlist.aspx
If you're just trying to find a specific object, you can use Linq and just go something like:
listEmployee.Where( r => r.Name == findName );
public class EventDetail
{
private Int64 logID;
public Int64 LogID
{
get { return logID; }
set { logID = value; }
}
private Object logedObject;
public Object LogedObject
{
get { return logedObject; }
set { logedObject = value; }
}
}
First off, you dont need to use fields if you do nothing in the property other then set it.
public Object LogedObject { get; set; }
is enough.
Secondly, to show a list of your objects in a DataGrid, as example.
protected void Page_Load(object sender, EventArgs e)
{
DataGrid dg = new DataGrid();
dg.DataSource = getModels();
dg.DataBind();
}
public List<EventDetail> getModels()
{
var m = new List<EventDetail>();
for (int a = 0; a < 15; a++)
{
m.Add(new EventDetail() { prop1 = a, prop2 = string.Format("Prop2 {0}", a) });
}
return m;
}
public class EventDetail
{
public Int64 LogID { get; set; }
public Object LogedObject { get; set; }
}
Look at the DataBinder class, it should allow you to do what you want ;-)
To bind with GridView (or similar controls such as Repeater, ListView), you typically need to know property names of object because that's what you will use to bind column to. So is this example, you may bind to LogID and LogedObject properties. For later, ToString will be invoked on object to show the string representation.
Instead of returning collection of class EventDetail, if you only have to bind logedevent, then do the following. Extract the collection of LogedObject in some other collection or
directly bind it to grid
protected void BindGrid()
{
gv.DataSource = EventDetail_Object.LogedObect_Property_Of_Class;
gv.DataBind();
}
tanx for any body help me
i solve this problem by reflection
i use refelection to get my properties of my object an thie values an then add this value to my string list
//exteract properties of loged object
PropertyInfo[] _PropertyInfo = _ObjectType.GetProperties();
List<string> _ObjBeforTostring = new List<string>();
//_ObjBeforTostring.Add("");
_ObjBeforTostring.Add("*************Befor Object**********");
_ObjBeforTostring.Add("");
foreach (PropertyInfo pf in _PropertyInfo)
{
if (_objbefor != null)
{
string _str = pf.GetValue(_objbefor, null).ToString();
_ObjBeforTostring.Add(pf.Name.ToString() + " :: ( " + _str + " )");
_ObjBeforTostring.Add("==============================");
}
}
_ObjBeforTostring.Add("");
_ObjBeforTostring.Add("*************After Object**********");
_ObjBeforTostring.Add("");
foreach (PropertyInfo pf in _PropertyInfo)
{
if (_objAfter != null)
{
string _str = pf.GetValue(_objAfter, null).ToString();
_ObjBeforTostring.Add(pf.Name.ToString() + " :: ( " + _str+" )");
_ObjBeforTostring.Add("==============================");
}
}