c# - Set property to generic parameter -


I have two classes cars and trucks , and they both Expand my vehicle class both cars and trucks are the assets that look like this:

  public Int wheels {get; Set; }   

Now, I have two modes that set the wheels property in some variables. I think:

  Public Zero SetCarWheels (int value) {car. Wow = value; } Public Zero SetTruckWheels (int value) {Truck.Wheels = value; }   

Because the method works the same way, I wanted to create a method that had an input parameter of type vehicle . The method should basically do the following:

  public static zero setways & lt; T & gt; (Vehicle items, int wheels) where T: vehicle {if (item! = Null) {item.Wheels = wheels; }}   

The problem is, it does not recognize the wheels property.

How can I solve this by using generic?

Please do not suggest alternative solutions without genetic (I know that there may be a value of car and truck wheels, but for this Problems should be left in this form)

Your code will only work if property is an example property in vehicles Base class:

  public class vehicles {public int wheels {receive; Set; }}   

In addition, in your method, you do not use normal type parameters and variables:

  public static zero SetWheels < T & gt; (T items, int wheels) where t: vehicle {if (item! = Null) {items. Wheels = wheels; }}    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -