为了账号安全,请及时绑定邮箱和手机立即绑定

请原谅我是个菜鸟,求帮助

请原谅我是个菜鸟,求帮助

C#
眼眸繁星 2023-01-11 15:15:50

namespace shiyan3_2
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}

private void Form4_Load(object sender, EventArgs e)
{
Book[] book = new Book[10];
book[0] = new Book("chinese", "me", 21, 2,30 );
book[1] = new Book("english", "you", 28, 3, 40);
}

private void button2_Click(object sender, EventArgs e)
{
Form4 f = new Form4();  
}
public class Book
{
string title;
string author;
int price;
int store;
int sellprice;

public Book()
{
}

public Book(string ti,string au,int pr,int st,int se)
{
title = ti;
author = au;
price = pr;
store = st;
sellprice = se;
}
}
}

如何在button_click中调用book【0】呢?看到一些人说不能调用的,如果不能调用那数组不就是废了

查看完整描述

2 回答

?
慕运维8079593

TA贡献1638条经验 获得超5个赞

namespace shiyan3_2
{
public partial class Form4 : Form
{
//如果你想在Form4以外的地方调用那就声明为public 变量或public的属性。
Book[] book = new Book[10];
public Form4()
{
InitializeComponent();
}

private void Form4_Load(object sender, EventArgs e)
{
book[0] = new Book("chinese", "me", 21, 2,30 );
book[1] = new Book("english", "you", 28, 3, 40);
}


查看完整回答
反对 回复 2023-01-15
?
慕标琳琳

TA贡献1546条经验 获得超9个赞

namespace shiyan3_2
{
public partial class Form4 : Form
{
Book[] book;
public Form4()
{
InitializeComponent();
}

private void Form4_Load(object sender, EventArgs e)
{
book = new Book[10];
book[0] = new Book("chinese", "me", 21, 2,30 );
book[1] = new Book("english", "you", 28, 3, 40);
}

private void button2_Click(object sender, EventArgs e)
{
Form4 f = new Form4();  
var val = book[0];
}
public class Book
{
string title;
string author;
int price;
int store;
int sellprice;

public Book()
{
}

public Book(string ti,string au,int pr,int st,int se)
{
title = ti;
author = au;
price = pr;
store = st;
sellprice = se;
}
}
}

 


查看完整回答
反对 回复 2023-01-15
  • 2 回答
  • 0 关注
  • 11 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信