将具有预定义结构的列表作为参数传递给不同的表格

问题描述:

我在this,this等几个链接中看到过此问题,但我相信我的情况有点不同。将具有预定义结构的列表作为参数传递给不同的表格

我想传递给第二个表格,将其上传到数据库。

列表的结构是:

public struct GraphData 
    { 
     public double Temp1; 
     public double Temp2; 
     public DateTime Date; 
     public string Type; 
    } 

该列表中填充这样的:

for (int i = startIndex; i <= endIndex; i++) 
    { 
     GraphData update = _listData[i]; 
     update.Type = type; 
     _listData[i] = update; 
     chart1.Series[0].Points[i].Color = color; 

     // add to a list so we can save on the DB if the user wants 
     PointsAndCycles.Add(new GraphData() 
     { 
      Temp1 = update.Temp1, 
      Temp2 = update.Temp2, 
      Date = update.Date, 
      Type = update.Type 
      // CAN I ALSO HAVE THE ID? 
     }); 

    } 

我想通过这样的名单:

public void button1_Click(object sender, EventArgs e) 
{ 
    Database.SavePointsAndCyclesListIntoDB(List<GraphData> PointsAndCycles) ; 
} 

但我得到错误: CS0305 C#使用泛型类型'List'需要1个类型参数和CS 0119 C#是一种类型,在给定的上下文中无效。

额外的信息......在第二个形式,我会执行列表如下:

public void SavePointsAndCyclesListIntoDB(List<GraphData> PointsAndCycles) 
    { 
     using (SqlConnection dbConnection = new SqlConnection(sqlConection)) 
     { 
      try 

任何人可以帮助我吗?我把这个想法传递作为参数,从here

如果我尝试:

public void button1_Click(object sender, EventArgs e) 
{ 
    Database.SavePointsAndCyclesListIntoDB(PointsAndCycles) ; 
} 

我得到的错误:

System.Collections.Generic.List<A_Dev_.Analysis.FormA_DataType.GraphData>' to 'System.Collections.Generic.List<A_Dev_.Database.ClassDatabase.GraphData>' 

最后,我想榜上无名公众对这一方式:

public List<GraphData> PointsAndCycles = new List<GraphData>(); 

附加: 在ClassDatabase中,我还定义了GraphData这样

public struct GraphData 
{ 
    public double Temp1; 
    public double Temp2; 
    public DateTime Date; 
    public string Type; 
} 

没有与此一对夫妇的问题,第一个是你的函数调用的形式...

如果是这样,你可能只是这样做

public void button1_Click(object sender, EventArgs e) 
{ 
    Database.SavePointsAndCyclesListIntoDB(PointsAndCycles) ; 
} 
唯一的问题

您在函数调用中声明了一个类型。

另一个问题是,PointsAndCycles似乎不是当前范围的一部分,除非它包含在包含函数的类中。您需要找出一种方法来访问事件处理程序中的List,可能通过esender,具体取决于您正在做什么。

+0

嘿,我刚加了这个试试。它不起作用。我通过“PointsAndCycles”作为公开。 –

+0

你有什么错误? – NonSecwitter

+0

我尝试了两种方法。第一个用“公开名单 PointsAndCycles =新列表();”错误 - 错误\t CS1503 \t参数1:无法从'System.Collections.Generic.List '转换为'System.Collections.Generic.List ';;如果我尝试公开名单 PointsAndCycles {get;组; } 我犯了同样的错误。 –