I have this code:
public class TABLE01
{
public string FIELD1 { get; set; }
public string FIELD2 { get; set; }
}
...
// fieldlist:
List<string> fieldsTABLE01 = new List<string>();
fieldsTABLE01.Add("FIELD1");
fieldsTABLE01.Add("FIELD2");
//response type List:
lResponseLegacy.Clear();
lResponseLegacy = DataExtract(fieldsTABLE01);
List<ClassDefinitions.TABLE01> listDataTableTABLE01 = new List<ClassDefinitions.TABLE01>();
foreach (string linea in lResponseLegacy)
{
ClassDefinitions.TABLE01 tableTABLE01 = new ClassDefinitions.TABLE01();
tableTABLE01.table01FIELD1 = ClassPackSupport.GetStringBetween(linea, "<FIELD1>", "</FIELD1>");
tableTABLE01.table01FIELD2 = ClassPackSupport.GetStringBetween(linea, "<FIELD2>", "</FIELD2>");
listDataTableTABLE01.Add(tableTABLE01);
}
classRespuestaDataclassModelo.tableTABLE01 = listDataTableTABLE01;
Now, I need to use the same previous code for others tables like fieldsTABLE01 with their own fields. As well, the previous code must create a list from a dynamic definition of a class (ClassDefinitions.TABLE01) and loop through of their own fields on these class.
In other words, I need to use the same code (maybe on a method) for other classes:
public class TABLE02
{
public string FIELD3 { get; set; }
public string FIELD4 { get; set; }
public string FIELD9 { get; set; }
}
public class TABLE03
{
public string FIELD5 { get; set; }
public string FIELD6 { get; set; }
}
public class TABLE04
{
public string FIELD7 { get; set; }
public string FIELD8 { get; set; }
public string FIELD14 { get; set; }
public string FIELD15 { get; set; }
}
Please, do you give me some tip?
Thanks.
You can use reflection like so
//Get a classes properties, you can do this with many classes.
var yourClassProperties = typeof(YourClass).GetProperties();
//And down the line:
fieldsTABLE01 = fieldsTABLE01.Concat(yourClassProperties).ToList();
Related
I am trying to create object with values and a multidimensional array from a second classes. What I created:
class ProductData
{
private ProductPrices[] prices;
public string ParentName { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string TechnicalDescription { get; set; }
public bool Obsolete { get; set; }
public bool Deleted { get; set; }
public string Innercarton { get; set; }
public string Outercarton { get; set; }
public string Package { get; set; }
public string Barcode { get; set; }
public decimal Weight { get; set; }
public decimal EndUserPrice { get; set; }
public int MOQ { get; set; }
public bool ComingSoon { get; set; }
public bool NewProduct { get; set; }
public string Equivalent { get; set; }
public string ReplacedBy { get; set; }
public ProductPrices[] Prices
{
get
{
return prices;
}
set
{
prices = value;
}
}
public List<ProductFeatures> Feature { get; set; }
public List<string> Specification { get; set; }
public List<string> Image { get; set; }
public string File { get; set; }
public string Icon { get; set; }
}
class ProductFeatures
{
public string Feature;
}
public class ProductPrices
{
public decimal Price;
public int MinQuantity;
public DateTime validuntil;
public string currency;
}
Now I would like to have for the ProductPrices an multidimensional array.
So, at the end I would have something like this:
Dictionary<string, ProductData> productInfo = new Dictionary<string, ProductData>();
//Adding data to productInfo
string productName = productInfo.Name;
string description = productInfo.Description;
decimal wholesalePrice = productInfo.ProductPrices[0].Price;
int minQty = productInfo.ProductPrices[0].MinQuantity;
Somewhere I'm missing something. I've been searching around for hours trying to find it but unfortunally...
Thanks in advance!
I already found it.
Remove the private ProductPrices[] prices; and change the
public ProductPrices[] Prices
{
get
{
return prices;
}
set
{
prices = value;
}
}
to
public ProductPrices[] Prices { get; set; }
After that, you can do:
ProductData prData = new ProductData();
prData.ParentName = "Parent name";
prData.Name = "Some name";
prData.Prices = new ProductPrices[enterArrayCount];
ProductPrices prdPrices = new ProductPrices();
prdPrices.Price = Convert.ToDecimal(yourDecimalValue);
prdPrices.MinQuantity = 8;
prdPrices.Currency = "Eur";
prData.Prices[firstIndex] = prdPrices; //So firstIndex must increase after a value has been set.
I have 2 or more class:
public class AllParam
{
public int Id { get; set; }
public string Data { get; set; }
public string Allarme { get; set; }
}
public class UtentiParam
{
public int Id { get; set; }
public string Utente { get; set; }
public string Psw { get; set; }
public string Livello { get; set; }
}
and I want to read data from my data base with this code:
var list = new List<AllParam>();
using (var db_ = new LiteDatabase(#"C:\HmiImpBroda\DataBase\" + TableToProcess + ".db"))
{
var col = db_.GetCollection<AllParam>(TableToProcess);
foreach (AllParam _id in col.FindAll())
{
list.Add(_id);
}
}
Exist a way to set set AllParam or CurveParam in my function from a string? in this way I can use the same code for each type of class without write other code.
Thanks.
I have a class defined as:
public class ReportObjectInformation
{
public string tableName { get; set; }
public int progressBarValue { get; set; }
public int rowCount { get; set; }
public bool canConnect { get; set; }
public int index { get; set; }
public string summaryFile { get; set; }
public string reportFile { get; set; }
public string errorFile { get; set; }
}
I currently have seven different lists of objects in my code. Is there a way I can do something like:
public class ReportObjectInformation
{
public string tableName { get; set; }
public int progressBarValue { get; set; }
public int rowCount { get; set; }
public bool canConnect { get; set; }
public int index { get; set; }
public string summaryFile { get; set; }
public string reportFile { get; set; }
public string errorFile { get; set; }
public List<> listOne { get; set; } // add this
public List<> listTwo { get; set; } // add this
}
And then in my code set the list to be one of my seven predefined types of lists?
One of my other lists is made up of this class:
class parameters
{
public string firstName{ get; set; }
public string lastName{ get; set; }
public string address{ get; set; }
public string city{ get; set; }
public string state { get; set; }
public string country{ get; set; }
public string active_flag { get; set; }
}
which I create in my code as:
List<parameters> parm_list = new List<parameters>();
The parm_list is populated with data. Now I want to add that list to this other object I'm creating. At other times in my code I'll want to set the list to one of the my other types but for now how would I do this? Is this even possible?
ReportObjectInformation reportObject = new ReportObjectInformation();
reportObject.tableName = "UserInformation";
reportObject.listOne = parm_list;
reportObject.listTwo = someother_list;
If you can guarantee that a particular instance of ReportObjectInformation will work with a given type of List you can do this:
public class ReportObjectInformation<TOne, TTwo>
{
public string tableName { get; set; }
public int progressBarValue { get; set; }
public int rowCount { get; set; }
public bool canConnect { get; set; }
public int index { get; set; }
public string summaryFile { get; set; }
public string reportFile { get; set; }
public string errorFile { get; set; }
public List<TOne> listOne { get; set; }
public List<TTwo> listTwo { get; set; }
}
Which lets you specify which types you want the ReportObjectInformation object lists to use.
You could make ReportObjectInformation generic
public class ReportObjectInformation<TOne, TTwo>
{
public List<TOne> listOne { get; set; } // add this
public List<TTwo> listTwo { get; set; } // add this
}
Then create an instance like this
var reportInfo = new ReportObjectInformation<parameters, otherClass>();
reportInfo.listOne = new List<parameters>();
reportInfo.listTwo = new List<otherClass>();
Of course this means that each instance can not switch to hold a list of one of the other types.
Well, since you want to assign of lists of different types to the object during runtime, you'll be left with no type checking. Can be implemented like:
public class ContainingClass
{
public IList SomeList { get; set; }
}
then you can do
var c = new ContainingClass();
c.SomeList = new List<int> { 1, 2, 3 };
c.SomeList = new List<string> { "abc", "def" };
foreach (var member in c.SomeList)
Console.WriteLine(member);
But you should only do this as a last resort, generally prefer using generics or clean design, because this is:
Slow - you'll have to cast a lot since IList works with object
Unsafe - who knows what list is there at a given time? Not to mention you'll be left without compile-time type checks (those are very powerful to have, try not to lose them).
Generally consider this a no-go unless you really really really have no choice (e.g. legacy code compatibility).
I have a class with 8 properties declared:
public class classProduct
{
public int iProductID { get; set; }
public string sPName { get; set; }
public string sPDescription { get; set; }
public int iPTypeID { get; set; }
public string sPPrice { get; set; }
public string sPType { get; set; }
public string sImagePath { get; set; }
public string sDestinationPath { get; set; }
}
And I have a method in which I add 4 properties in my list like and ProductDetailsTbls is my Tbl1 and ProductTypeTbls is Tbl2.
public List<classProduct> GetAllProduct()
{
List<classProduct> lst = new List<classProduct>();
lst = (from c in dc.ProductDetailsTbls
join d in dc.ProductTypeTbls
on c.PTypeID equals d.PTypeID
select new classProduct { sPName=c.PName, sPDescription=c.PDescription, sPPrice=c.PPrice, sPType=d.PType }).ToList();
return lst;
}
But it returns all properties that I don't want.
If you only want some properties I think you will have to introduce a new class.
My approach for this would be to define a base class with your four properties and then you can classProduct inherit from it.
your base-class then looks like:
public class baseClassProduct
{
public string sPName { get; set; }
public string sPDescription { get; set; }
public string sPPrice { get; set; }
public string sPType { get; set; }
}
and your classProduct:
public class classProduct : baseClassProduct
{
public int iProductID { get; set; }
public int iPTypeID { get; set; }
public string sImagePath { get; set; }
public string sDestinationPath { get; set; }
}
In your GetAllProduct-Method you can work with the baseClass and you'll get only the information you want to.
public List<baseClassProduct> GetAllProduct()
{
List<baseClassProduct> lst = new List<baseClassProduct>();
lst = (from c in dc.ProductDetailsTbls
join d in dc.ProductTypeTbls
on c.PTypeID equals d.PTypeID
select new baseClassProduct { sPName=c.PName, sPDescription=c.PDescription, sPPrice=c.PPrice, sPType=d.PType }).ToList();
return lst;
}
So I'm creating a new tool in C# and I've created a class called 'Customer' and each 'Customer' has sub-group of employees which is an array of names. How do I set this property in my class for 'Customer'? What I have below for 'Employees' is not correct. I just left it there as a placeholder.
Thank you
public class Customer
{
public String Number { get; set; }
public String Name { get; set; }
public String Street { get; set; }
public String City { get; set; }
public String State { get; set; }
public String Zipcode { get; set; }
public string[] Employees = { get; set; }
}
You could use a List instead of an array as it is easier to manipulate:
public class Customer
{
public String Number { get; set; }
public String Name { get; set; }
public String Street { get; set; }
public String City { get; set; }
public String State { get; set; }
public String Zipcode { get; set; }
public List<string> Employees { get; set; }
}
And then when you instantiate it, you could add new employers as such:
Customer customer = new Cusomter();
customer.Number = "num1";
customer.Name = "ABC";
//...
List<string> lstEmp = new List<string>();
lstEmp.Add("NewEmployee1");
lstEmp.Add("NewEmployee2");
customer.Employees = lstEmp;
And read it like this:
foreach (string name in customer.Employees)
{
Console.WriteLine(name);
}
Simply use this declaration:
public string[] Employees { get; set; }