【发布时间】:2021-08-25 22:00:11
【问题描述】:
我创建了一个包含一个对象的列表,其中包含一个对象列表。我想从继承/子类对象中检索值,即在父类列表/对象中。在下面解释我的代码有点令人困惑,应该会有所帮助。也许 Linq 可能是解决方案?我不确定。非常感谢。
RelocationActivityForm 类 -- 这将使用继承的 Relocation 活动类创建一个新对象。
private Vehicle selectedVehicle;
selectedVehicle = (Vehicle)mainFormRego.SelectedItem; // Gets the selected item from a listbox
selectedVehicle.ActivityList.Add(new Relocation() // create an object for the selected vehicle, inside the activitylist (list) creating a new object using the Relocation class.
{
Date = date, // In activity class
Name = name, // In activity class
Type = "Relocation", // In activity class
Start = start,// In relocation class
End = end, // In relocation class
Distance = distance, // In relocation class
Cost = cost // In activity class
});
ViewVehicleForm.Form.updateViewVehicle(); // Method updates textbox display.
Hide(); // Form hidden
}
车辆类别
namespace VehicleSystem
{
[Serializable]
public class Vehicle
{
private int dailyCharge;
private int year;
private string model;
private string make;
private string rego;
private List<Activity> _activityList = new List<Activity>();
public string Rego { get => rego; set => rego = value; }
public string Make { get => make; set => make = value; }
public string Model { get => model; set => model = value; }
public int Year { get => year; set => year = value; }
public int DailyCharge { get => dailyCharge; set => dailyCharge = value; } // Creating an ativity list for each vehicle
internal List<Activity> ActivityList { get => _activityList; set => _activityList = value;
}
}
}
活动类
[Serializable]
public class Activity
{
// Relocation Activity
private DateTime _date;
private string _name;
private string _type;
private decimal _cost;
public string Name { get => _name; set => _name = value; }
public string Type { get => _type; set => _type = value; }
public decimal Cost { get => _cost; set => _cost = value; }
public DateTime Date { get => _date; set => _date = value; }
}
重定位类
class Relocation : Activity
{
private string start;
private string end;
private int distance;
public string Start { get => start; set => start = value; }
public string End { get => end; set => end = value; }
public int Distance { get => distance; set => distance = value; }
}
使用它,我想从车辆的 ActivityList 中获取 Relocation 类中的项目。我可以从 ActivityList 中获取项目(通过调用此语句如下所示),但我无法获取使用 Relocation 类存储的活动/项目。
private void viewVehicleActivityData_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int x = viewVehicleActivityData.CurrentCell.RowIndex; // Getting the datagridview index of a row
MessageBox.Show(selectedVehicle.ActivityList[x].Name); // Using it to get the name
}
【问题讨论】:
-
这个问题有点不清楚。您是说您有一个
Activity对象列表,并且您想要该列表的所有Relocation对象的子序列?这就是OfType<Relocation>序列运算符。如果这不是你想要的,那么你能澄清这个问题吗? -
不清楚你在问什么。 “我无法获取使用 Relocation 类存储的活动/项目”不清楚。
Relocation类型不存储或具有与活动/项目相关的成员。 -
我可能在这里冒昧,但我认为您正在寻找的是一种确定或关联
RowIndex到selectedVehicle.ActivityList.OfType<Relocation>()的方法,因为您需要解决索引反对selectedVechicle.ActivityList?这是你面临的问题吗? -
@BrettCaswell 是的,这就是我的意思。对不起,伙计们
-
我正在尝试从活动的重定位子类中获取项目。我正在创建一个使用 Relocation 类的对象,但我无法仅从活动类中获取该类中的项目。
标签: c# winforms linq object controls