Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 months ago.
Improve this question
Hi I have 3 base interfaces:
public interface IKlarfDefect
{
int TEST { get; set; }
int DEFECTID { get; set; }
}
public interface IKlarfDefect <TImageListInfo> : IKlarfDefect
{
List<TImageListInfo> KlarfImageList { get; set; }
}
public interface IHasKlarfImageList<TImageListInfo>
{
List<TImageListInfo> KlarfImageList { get; }
}
These classes implement it:
public class CIMKlarfDefect : IKlarfDefect, IHasKlarfImageList<CIMKlarfImageListInfo>
{
public int TEST { get; set; }
public int DEFECTID { get; set; }
public List<CIMKlarfImageListInfo> KlarfImageList { get; set; } = new List<CIMKlarfImageListInfo>();
}
public class RMTKlarfDefect : IKlarfDefect, IHasKlarfImageList<CIMKlarfImageListInfo>
{
public int TEST { get; set; }
public int DEFECTID { get; set; }
public List<RMTKlarfImageListInfo> KlarfImageList { get; set; } = new List<RMTKlarfImageListInfo>();
}
Then I have this function in another class that tries to read through these classes:
internal static string CreateCIMDefectListString(IEnumerable<IKlarfDefect<IKlarfImageListInfo>> klarfDefectList)
{
StringBuilder defectListString = new StringBuilder();
defectListString.AppendLine("");
foreach (var klarfDefect in klarfDefectList) {
defectListString.Append(klarfDefect.TEST).Append(" ");
defectListString.Append(klarfDefect.DEFECTID).Append(" ");
if (klarfDefect is IHasKlarfImageList<IKlarfImageListInfo> grcKlarfDefect)
{
if (grcKlarfDefect.KlarfImageList.Count == 0)
{
defectListString.Append("N;");
defectListString.AppendLine();
}
}
}
return defectListString.ToString();
}
It compiles but the if statement fails when I am passing in either RMTKlarfDefect or CIMKlarfDefect. Might anyone know why this is?
if klarfDefect is IHasKlarfImageList grcKlarfDefect
fails as in, it doesn't return true which would allow it to process the imagelist
Your if statement says
if (klarfDefect is IHasKlarfImageList<IKlarfImageListInfo> grcKlarfDefect)
But your classes implement
IHasKlarfImageList<CIMKlarfImageListInfo>
So you need to update your if statement accordingly:
if (klarfDefect is IHasKlarfImageList<CIMKlarfImageListInfo> grcKlarfDefect)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I have class A and class B inherits Class A.
Class A has extension method. My problem is Class A extension method don't have all properties, So, I need to create Extension method for class B.
In this case, How can I use Class A extension method and avoid duplicate code in class B extension method.
public class A
{
public int Id { get; set; }
public string FirstName {get; set; }
public string LastName { get; set; }
}
public class B: A
{
public int Age { get; set; }
}
internal static class ClassAExtension
{
public static ADto ExtensionA(this A a)
{
return a != null
? new ADto
{
Id = a.Id,
Name = a.FirstName + " " + a.LastName
}
: null;
}
}
public class ADto
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set;}
}
Use pattern matching.
public static ADto ExtensionA(this A a)
{
if (a is null) return null;
var result = new ADto {
Id = a.Id,
Name = $"{a.FirstName} {a.LastName}"
};
if (a is B b) {
result.Age = b.Age;
}
return result;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have two classes like below and I want to use InvoiceItem properties at Invoice class and add to AddInvoiceItem method but I receive error.
public class InvoiceItem
{
public int InvoiceItemId { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public double Cost { get; set; }
}
public class Invoice
{
public int InvoiceNumber { get; set; }
public DateTime InvoiceDate { get; set; }
public List<InvoiceItem> LineItems { get; set; }
public void AddInvoiceItem(InvoiceItem invoiceItem)
{
LineItems.Add(invoiceItem);
}
From the little you've shown, most likely you haven't actually created your list.
Try
public List<InvoiceItem> LineItems { get; set; } = new List<InvoiceItem>();
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
using System.Text;
using System.Threading.Tasks;
namespace Program
{
public class Book
{
public string title;
public string author;
public int pages;
Book.pages = 10;
}
}
I'm not sure why I'm getting the invalid token error. Please help.
You can use this code, if you want to set default value for pages.
public class Book
{
public string title { get; set; }
public string author { get; set; }
public int pages { get; set; } = 10;
}
But if you want to set value after create your class you can use this code:
first time you should create instance of your object and set value.
public class Book
{
public string title { get; set; }
public string author { get; set; }
public int pages { get; set; }
}
Book book = new Book { pages = 10 };
And you can also use constructor to set default value.
public class Book
{
public Book()
{
this.pages = 10;
}
public string title { get; set; }
public string author { get; set; }
public int pages { get; set; }
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following classes
class ClassA : SuperClass { }
class ClassB : SuperClass { }
class ClassC : SuperClass { }
class SuperClass
{
int Id { get; set; }
string Property1 { get; set; }
string Property2 { get; set; }
List<string> Property3 { get; set; }
}
When a condition is met, I have to override one, two or all of the properties in any of the classes (A, B, C) and I have to know that the properties were overridden and which ones where.
So I created this interface that should be implemented by SuperClass.
public interface IOverride
{
string OverrideProperty1 { get; set; }
string OverrideProperty2 { get; set; }
List<string> OrderedProperty3 { get; set; }
}
How can I know if a property was overridden without having to create a boolean for each property.
Or should I go for a different approach?
EDIT
ClassA, ClassB and ClassC data come from a specific source and for each of the objects I get information from another source. If the new source has values I need to stored them and override the original ones.
Something like
var dataList = GetClassesData() as List<SuperClass>;
var newData = GetNewData() as object[];
foreach (var data in dataList)
{
if (newData.Contains(o => o.Id == data.Id))
{
data.Property1 = newData[Id].Property1;
data.Property2 = newData[Id].Property2;
data.Property3 = newData[Id].Property3;
}
}
Overriding itself is not intent to be conditional, what can be conditional, is the value of the property you return from overridden class.
This should do it for you.
if(typeof(ClassA).GetMethod("Property1").DeclaringType == typeof(SuperClass)){
//This method was overridden
}
If I understand your edited question right, you could put it like that:
public interface IOverride
{
string StandardProperty1 { get; set; }
string StandardProperty2 { get; set; }
List<string> StandardProperty3 { get; set; }
string OverrideProperty1 { get; set; }
string OverrideProperty2 { get; set; }
List<string> OrderedProperty3 { get; set; }
}
class ClassA : SuperClass { }
class ClassB : SuperClass { }
class ClassC : SuperClass { }
class SuperClass : IOverrideProperty
{
string StandardProperty1 { get; set; }
string StandardProperty2 { get; set; }
List<string> StandardProperty3 { get; set; }
string OverrideProperty1 { get; set; }
string OverrideProperty2 { get; set; }
List<string> OrderedProperty3 { get; set; }
string Property1
{
get
{
if (condition)
return OverrideProperty1;
else
return StandardProperty1;
}
set
{
if (condition)
OverrideProperty1 = value;
else
StandardProperty1 = value;
}
//Same for Property2 and 3
}
You can now set both Properties and your condition will decide wich one is returned or set.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have an model in my MVC application that contains som properties and som calculated properties. When i try to POST a new object of the model to database i get an error that my calculated properties cannot be null.
Here is my model:
public class OrderItem
{
public int orderItemId { get; set; }
[DataType(DataType.MultilineText)]
public string orderItemDescr { get; set; }
public string orderItemText { get; set; }
public double orderItemFixeedPrice { get; set; }
public virtual Order orderItemOrder { get; set; }
public virtual OrderItemType orderItemType { get; set; }
public virtual ICollection<Time> orderItemTime { get; set; }
public virtual ICollection<Material> orderItemMaterial { get; set; }
public OrderItem ()
{ }
public OrderItem (Order order)
{
this.orderItemOrder = order;
}
public string orderItemTypeDescr
{
get
{
return (this.orderItemType.orderItemTypeNumber.ToString() + " - " + orderItemDescr);
}
}
public double orderItemMaterialSum
{
get
{
return orderItemMaterial.Sum(m => m.materialItmPrice * m.materialItem);
}
}
public double orderItemTimeCount
{
get
{
return orderItemTime.Sum(t => t.timeItem);
}
}
public double orderItemTimeSum
{
get
{
return orderItemTime.Sum(t => t.timePrice * t.timeItem);
}
}
public double orderItemSum
{
get
{
return orderItemTimeSum + orderItemMaterialSum;
}
}
}
Error shows on properties: orderItemMaterialSum, orderItemTimeCount, orderItemTimeSum
these properties is only calculated as you seen and should not have values.
The problem comes only when i create an objekt and post it to database.
The reason for the error is that your properties in question are doubles and thus cannot be null. Three ways come to mind to address the problem:
Convert the properties in question to methods and re-factor your code
as necesesary.
Use a view model which only includes the properties you need (recommended)
Use the bind attribute to specify how binding is to be done when the model is posted (link)
Best practice is to use view models. Your view model might look something like the following:
public class OrderItemViewModel
{
public int orderItemId { get; set; }
[DataType(DataType.MultilineText)]
public string orderItemDescr { get; set; }
public string orderItemText { get; set; }
public double orderItemFixeedPrice { get; set; }
public virtual Order orderItemOrder { get; set; }
public virtual OrderItemType orderItemType { get; set; }
public virtual ICollection<Time> orderItemTime { get; set; }
public virtual ICollection<Material> orderItemMaterial { get; set; }
public string orderItemTypeDescr
{
get
{
return this.orderItemType.orderItemTypeNumber.ToString() + " - " + this.orderItemDescr;
}
}
}
Does this help?