I hava a class like;
public class MainClass
{
public class Class1
{
public string Property1 { get; set; }
public string Property2 { get; set; }
public int Property3 { get; set; }
}
public class Class2
{
public Class2()
{
List1 = new List<Class1>();
}
public string Property4 { get; set; }
public List<Class1> List1 { get; set; }
}
public List<Class2> List2 { get; set; }
public string Property5 { get; set; }
public bool Property6 { get; set; }
}
I want to add parameters in MainClass at runtime
public class MainClass
{
...
public string Parameter1 { get; set; }
public string Parameter2 { get; set; }
...
}
or create new class with inheritance existing class.
public class NewClass : MainClass
{
public string Parameter1 { get; set; }
public string Parameter2 { get; set; }
}
or something else. These's my ideas. Unfortunately, using a dictionary will not solve my problem because I am using xtrareport and I must show a fieldlist.
You could bind your ExtraReport to a DataSet instead of binding to collection of a strong typed objects (which can't be changed at execution-time). So if you add new fields to the data-source it’s a matter of changing the report for showing that new fields. If you create a Stored Procedure to retrieve data, you can even change the query without having to compile the Application. You can iterate through the DataTable object of the DataSet to read all the fields & place them dynamically using XRBinding DevExpress Objects.
See: https://www.devexpress.com/Support/Center/Question/Details/T408680/binding-datatable-object-to-xtrareport-xrtable
Related
I am simply trying to generate a unique cachekey that takes in the object type and property values
GetHashCode returns different results each time so that wont work, so I have to implement a solution but it has to be fast (I can go through the properties and and concat their values to a string but this might be slow and not the best way to go about it)
Nice To Have:
so if 2 different object types have the exact same properties and same values but they are different classes, they should be different cachekeys (chances of this happening are very slim but just in case)
Here is my code
public interface ICachableRequest
{
string GetCacheKey();
}
public class Object1 : ICachableRequest
{
public int IntValue1 { get; set; }
public double DoubleVal1 { get; set; }
public string StringVal1 { get; set; }
public string GetCacheKey()
{
throw new NotImplementedException();
}
}
public class Object2 : ICachableRequest
{
public int SomeIntValue1 { get; set; }
public double SomeOtherDoubleVal1 { get; set; }
public string MoreStringVal1 { get; set; }
public string MoreStringVal2 { get; set; }
public string MoreStringVal3 { get; set; }
public string MoreStringVal4 { get; set; }
public string GetCacheKey()
{
throw new NotImplementedException();
}
}
I want to create a method that will create a List of generic objects. Is it possible? Something like this:
public class Mtrl
{
public string Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public string Aa { get; set; }
}
public class Trdr
{
public string Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public string Address { get; set; }
public string AFM { get; set; }
public string Phone01 { get; set; }
public string Aa { get; set; }
}
And then with a generic class to create my list:
public class GenericClass<T>
{
public List<T> GetData<T>()
{
List<T> myList = new List<T>();
if (typeof(T) == typeof(Trdr))
{
myList.Add(new Trdr());//Error 1: cannot convert from Trdr to 'T'
}
if (typeof(T) == typeof(Mtrl))//Error 2: cannot convert from Mtrl to 'T'
{
myList.Add(new Mtrl());
}
return myList;
}
}
My mistake. I will try to clarify more. The Classes Trdr,Mtrl etc will have many different properties. The Getdata method will take data from a web service via json and i want to create a List of Objects and return it
Something like this:
public List<T> GetData<T>()
{
List<T> myList = new List<T>();
if (typeof(T) == typeof(Trdr))
{
for (int i = 0; i < 100; i++)//fetch data from web api in json format
{
Trdr NewObj = new Trdr();
NewObj.Aa = "...";
NewObj.AFM = "...";
myList.Add(NewObj);
}
}
if (typeof(T) == typeof(Mtrl))
{
for (int i = 0; i < 100; i++)
{
Mtrl NewObj = new Mtrl();
NewObj.Aa = "...";
NewObj.Name = "name ...";
myList.Add(NewObj);
}
}
return myList;
}
}
I suggest extracting an interface (or even a base class):
//TODO: Since you have more than 2 classes, please check the common interface
public interface Idr {
string Id { get; set; }
string Name { get; set; }
string Code { get; set; }
string Aa { get; set; }
}
With all classes of interest implementing it:
public class Mtrl : Idr
{
public string Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public string Aa { get; set; }
}
public class Trdr : Idr
{
public string Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public string Aa { get; set; }
public string Address { get; set; }
public string AFM { get; set; }
public string Phone01 { get; set; }
}
Now you can use List<Idr> collection; we want the class (T) that implements Idr to have a parameterless constructor as well (new()):
public class GenericClass<T> where T : Idr, new()
{
public List<T> GetData()
{
List<T> myList = new List<T>() {
new T();
};
return myList;
}
}
Sounds like you are trying to create a new list that contains some initial data, and you need to do this for many different types T.
If Mtrl and Trdr has nothing in common and are handled completely differently, consider simply using different methods to get each type of list:
GetDataMtrl(){
var result = new List<Mtrl>();
result.Add(new Mtrl());
return result;
} // etc
Otherwise what you need is Type Constraints of Generic Parameters, that can tell the compiler more about what T is. Or rather what the different values of T must have in common. For example you can do
public List<T> GetData<T>() where T : new()
{
List<T> myList = new List<T>();
myList.Add(new T());
return myList;
}
To say that for T it has to be possible to do new T(). Then you can say GetData<Trdr>() to get a list that contains a single empty Trdr, as you code might seem to try to do.
Maybe you need to set some default values in the data. I notice the classes has a lot of variables in common. Then you might consider using inheritance to specify this commonality:
public class Mtrl
{
public string Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public string Aa { get; set; }
}
public class Trdr : Mtrl
{
public string Address { get; set; }
public string AFM { get; set; }
public string Phone01 { get; set; }
}
And then write generic code where T is either of the type Mtrl or its descendant class:
public List<T> GetData<T>() where T : Mtrl
{
List<T> myList = new List<T>();
T MtrlOrTrdr = new T();
MtrlOrTrdr.Id = "my-new-id-";
myList.Add(MtrlOrTrdr);
return myList;
}
First of all, please stick to how I accomplish this with the current setup, and not in trying to redesign how it is structured. I have to build an app/web service to receive a JSON query. The JSON is generated by a widget that we can not customize. I used Visual Studio to create the class structure from a JSON example.
I want to test my service, and I'm getting errors in SOAPUI, so I need something where I can debug, so I'm hard-coding the JSON input object for my Service Reference.
Here are the classes, as they reside on the web service -
[DataContract]
public class QuoterIn
{
[DataMember]
public Quoterinput quoterinput { get; set; }
}
public class Quoterinput
{
public string agent_id { get; set; }
public string key { get; set; }
public string auto_assign { get; set; }
public Consumer consumer { get; set; }
public string viaEmail { get; set; }
}
public class Consumer
{
public string brand_id { get; set; }
public string full_name { get; set; }
public Addresses_Attributes[] addresses_attributes { get; set; }
public string birth_or_trust_date { get; set; }
public string gender { get; set; }
public Emails_Attributes[] emails_attributes { get; set; }
public Phones_Attributes[] phones_attributes { get; set; }
public Cases_Attributes[] cases_attributes { get; set; }
}
public class Addresses_Attributes
{
public string zip { get; set; }
}
public class Emails_Attributes
{
public string value { get; set; }
}
public class Phones_Attributes
{
public string value { get; set; }
}
public class Cases_Attributes
{
public Quoting_Details_Attributes[] quoting_details_attributes { get; set; }
}
public class Quoting_Details_Attributes
{
public string carrier_id { get; set; }
public string duration_id { get; set; }
public string policy_type_id { get; set; }
public string plan_name { get; set; }
public string product_type_name { get; set; }
public string face_amount { get; set; }
public string carrier_health_class { get; set; }
public string planned_modal_premium { get; set; }
public string premium_mode_id { get; set; }
public string health_class_id { get; set; }
}
Looking at the sub-class of Consumer, there are several sub-classes that are arrays - address_attributes, phone_attributes, and email_attributes.
I'm able to initialize the main JSON input/inquiry container, and then I can initialize the main class and the sub-class of "Consumer." I can enter data for the main fields, and then fields in the sub-class of Consumer, but I can't initialize the arrays.
Here's the code up until this point for populating that input container -
protected void btnRun_Click(object sender, EventArgs e)
{
QuoterIn req = new QuoterIn();
req.quoterinput = new Quoterinput();
req.quoterinput.consumer = new Consumer();
req.quoterinput.agent_id = "1938";
req.quoterinput.key = "afasdfasdfasdfasdfasd";
req.quoterinput.auto_assign="true";
req.quoterinput.consumer.brand_id = "21264";
req.quoterinput.consumer.full_name = "Fred Smith";
req.quoterinput.consumer.addresses_attributes[0].zip = "53704";
.................(more code like this, but this last line is where the program fails)
Since we aren't going to take in multiple email addresses or phone numbers, I'd be fine with not having it be an array, but I'm worried that the incoming JSON with all the grouping and bracketing will fail if it's not set up like an array. I've tried to initialize instance zero like this -
req.quoterinput.consumer.addresses_attributes[0] = new Addresses_Attributes();
.... and Visual Studio likes the code syntax-wise, but it doesn't initialize an instance when it runs, and then I get the "Object reference not set to an instance of an object." error when it runs. It hasn't allowed me to initialize the array using more generic methods since it has to map to this specific property we've already declared.
I have no doubt it's probably straightforward, but I'm not all that experienced in manipulating arrays and lists like this, and I haven't been able to find examples of arrays of sub-classes within subclasses of properties.
Just instantiate / assign the address at the same time. Something like this should work:
req.quoterinput.consumer.addresses_attributes = new []
{
new Addresses_Attributes
{
zip = "53704"
}
};
Another way (since the property toy are trying to set is an Array), is to create a dynamic sized list, add to it, then convert it back during declaration.
var addresses = new List<Addresses_Attributes>
{
new Addresses_Attributes {zip = "53704"}
};
req.quoterinput.consumer.addresses_attributes = addresses.ToArray();
Or, you can assign it an array of size 1, and then set it up from there:
req.quoterinput.consumer.addresses_attributes = new Addresses_Attributes[1];
req.quoterinput.consumer.addresses_attributes[0] = new Addresses_Attributes { zip = "666" };
I have a xml format in following format mentioned below:-
<JobRunnerPluginStaus PluginName="JobRun">
<JobstepStatus>
<JobStatus StepNumber="1" StepStatus="Done"/>
<JobStatus StepNumber="2" StepStatus="Started" />
</JobstepStatus>
</JobRunnerPluginStaus>
I want to get it converted to following class object using Generics and Reflection.
I want to convert the attributes to simple type(PluginName) and the nested property to a list object(JobstepStatus).
public class JobRunnerPluginStaus
{
public List<JobStatus> JobstepStatus { get; set; }
public string PluginName { get; set; }
}
public class JobStatus
{
public int StepNumber { get; set; }
public string StepStatus { get; set; }
}
I mostly use sites like: https://xmltocsharp.azurewebsites.net/
to do the dirty work for me.
Below is how your class hierarchy would look like:
[XmlRoot(ElementName="JobStatus")]
public class JobStatus {
[XmlAttribute(AttributeName="StepNumber")]
public string StepNumber { get; set; }
[XmlAttribute(AttributeName="StepStatus")]
public string StepStatus { get; set; }
}
[XmlRoot(ElementName="JobstepStatus")]
public class JobstepStatus {
[XmlElement(ElementName="JobStatus")]
public List<JobStatus> JobStatus { get; set; }
}
[XmlRoot(ElementName="JobRunnerPluginStaus")]
public class JobRunnerPluginStaus {
[XmlElement(ElementName="JobstepStatus")]
public JobstepStatus JobstepStatus { get; set; }
[XmlAttribute(AttributeName="PluginName")]
public string PluginName { get; set; }
}
I have a class
class AttendanceReport : BaseBean
{
public int Id { get; set; }
public int serialNo { get; set; }
public string aadharCard { get; set; }
public string employeeName { get; set; }
}
and i have i list of that class
List<AttendanceReport> list=New List<AttendanceReport>();
i want to pass this is in a constructor of another class
public popUpGrid(List<AttendanceReport> arr)
but it show me an error in the class of the constructor that inconsistent accessibility
please help me with that error or give me another option to how i can send tabular data to another class without using DataTable or DataSet
Your class AttendanceReport is not marked as public. This is the reason for the error you encounter.
Change Your Class as Below
Public class AttendanceReport : BaseBean
{
public int Id { get; set; }
public int serialNo { get; set; }
public string aadharCard { get; set; }
public string employeeName { get; set; }
}
So it is accessible to Outside Context i.e. in other Classes