gpt4 book ai didi

c# - 在将列表连接到列表框时获取 StackOverflowException

转载 作者:太空狗 更新时间:2023-10-30 01:33:15 28 4
gpt4 key购买 nike

我创建了 Auction.cs 类,后面有这段代码:

namespace WebApplication5
{
public class Auction
{
public string Productname { get; set; }
public string Lastbidder { get; set; }
public int Bidvalue { get; set; }

private List<Auction> listaAukcija = new List<Auction>();

public List<Auction> ListaAukcija
{
get { return listaAukcija; }
set { listaAukcija = value; }
}

public void getAll()
{
using (SqlConnection conn = new SqlConnection(@"data source=JOVAN-PC;database=aukcija_jovan_gajic;integrated security=true;"))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = @"SELECT a.id AS aid, p.name AS pn, u.name AS un, a.lastbid AS alb FROM (Auction a INNER JOIN Product p ON a.productid = p.id) INNER JOIN User u ON a.lastbider = u.id";
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
listaAukcija.Clear();

while (reader.Read())
{
Auction auction = new Auction();

if (reader["aid"] as int? != null)
{
auction.Productname = reader["pn"] as string;
auction.Lastbidder = reader["un"] as string;
auction.Bidvalue = (int)reader["alb"];
}

listaAukcija.Add(auction);
}
}
}
}

public override string ToString()
{
return base.ToString();
}
}
}

我在另一个名为 DbBroker.cs 的类中调用了它的方法:

 public class DbBroker : Home
{
Auction aukcija = new Auction();

public void executeQuery()
{
aukcija.getAll();
}

public void getArr()
{
List<string[]> lista = aukcija.ListaAukcija.Cast<string[]>().ToList();
var x = ListBox1.Text;
x = lista.ToString();
}
}

并在 Home 页面上调用了 getArr :

public partial class Home : System.Web.UI.Page
{
DbBroker dbb = new DbBroker();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label3.Text = Session["Username"].ToString();
dbb.getArr();
}
}
}

问题是,我在 DbBroker.cs 类的 Auction aukcija = new Auction(); 上遇到了 StackOverflowException 错误。我不知道为什么或如何解决它。

最佳答案

您在自身 = Stackoverflow 中创建了一个 Auctions 对象列表。

这是你的问题:

public class Auction {
private List<Auction> listaAukcija = new List<Auction>();
}

您需要将拍卖 模型与获取数据的服务或存储库分开。

例如:

//the model
public class Auction {
public string Productname { get; set; }
public string Lastbidder { get; set; }
public int Bidvalue { get; set; }

public override string ToString()
{
return base.ToString();
}
}

//the service (or can replace this with a repository)
public class AuctionService {
private List<Auction> listaAukcija = new List<Auction>();

public List<Auction> ListaAukcija
{
get { return listaAukcija; }
set { listaAukcija = value; }
}

public void getAll()
{
//get the data and populate the list
}
}

更新

您需要在 DbBroker 中实例化 AuctionServiceDbBroker 不再继承 Home(已注释掉)。

public class DbBroker //: Home <-- circular reference
{
AuctionService auctionService = new AuctionService();

public void executeQuery()
{
auctionService.getAll();
}

public void getArr()
{
string[] lista = auctionService.ListaAukcija.ConvertAll(obj => obj.ToString()).ToArray();

ListBox1.Text = string.Join("\n", lista);
}
}

并且在 Page_Load() 上 - 您没有调用 executeQuery() 函数来填充列表。

public partial class Home : System.Web.UI.Page
{
DbBroker dbb = new DbBroker();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label3.Text = Session["Username"].ToString();
dbb.executeQuery(); //populate list.
dbb.getArr(); //convert to string and update textbox
}
}
}

附言。有了新的更新,AuctionService 实际上应该是存储库,而 DbBroker 可以充当服务层。但是,这仍然适用于教育目的。

希望对您有所帮助。

关于c# - 在将列表连接到列表框时获取 StackOverflowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34464321/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com