using System;
using System.Collections.Generic;
using System.Text;namespace WindowsApplication1
{
class BMessage
{
private static Double _Dparm;
private static string _Sparm;
private static object _Oparm;
public static Double DoubleParm
{
//传双精度类型
get{return _Dparm;}
set{_Dparm=value;}
}
public static string StringParm
{
//传字符串类型
get { return _Sparm; }
set { _Sparm = value; }
}
public static object PowerObjectParm
{
//传结构
get { return _Oparm; }
set { _Oparm = value; }
}
}
}
public struct Book
{
public decimal price;
public string title;
public string author;
}
在form1中的Button的Click事件中
private void simpleButton1_Click(object sender, EventArgs e)
{
Book b=new Book();
b.price = 100;
BMessage.PowerObjectParm=b;
Form2 myxtf = new Form2();
myxtf.Show();
}
在form2中的load事件中
private void Form2_Load(object sender, EventArgs e)
{
Book b;
b = (Book)BMessage.PowerObjectParm;
MessageBox.Show(b.price.ToString());
}
我的问题是怎么取到PowerObjectParm传过来的结构中的值。现在我这样写
这一行b = (Book)BMessage.PowerObjectParm;
报指定的转换无效异常?
using System.Collections.Generic;
using System.Text;namespace WindowsApplication1
{
class BMessage
{
private static Double _Dparm;
private static string _Sparm;
private static object _Oparm;
public static Double DoubleParm
{
//传双精度类型
get{return _Dparm;}
set{_Dparm=value;}
}
public static string StringParm
{
//传字符串类型
get { return _Sparm; }
set { _Sparm = value; }
}
public static object PowerObjectParm
{
//传结构
get { return _Oparm; }
set { _Oparm = value; }
}
}
}
public struct Book
{
public decimal price;
public string title;
public string author;
}
在form1中的Button的Click事件中
private void simpleButton1_Click(object sender, EventArgs e)
{
Book b=new Book();
b.price = 100;
BMessage.PowerObjectParm=b;
Form2 myxtf = new Form2();
myxtf.Show();
}
在form2中的load事件中
private void Form2_Load(object sender, EventArgs e)
{
Book b;
b = (Book)BMessage.PowerObjectParm;
MessageBox.Show(b.price.ToString());
}
我的问题是怎么取到PowerObjectParm传过来的结构中的值。现在我这样写
这一行b = (Book)BMessage.PowerObjectParm;
报指定的转换无效异常?
{
Book b = new Book();
b.price = 100;
BMessage.PowerObjectParm = b;
Form2 myxtf = new Form2(b);
myxtf.Show();
}//Form2
public partial class Form2 : Form
{
private Book myB;
public Form2(Book b)
{
InitializeComponent();
myB = b;
} private void Form2_Load(object sender, EventArgs e)
{
MessageBox.Show(myB.price.ToString());
}
}
你这样做不要不BMessage.PowerObjectParm = b;
都可以。
我现在不想每次传值是都先写一个构造函数
public class Book
{
public decimal price;
public string title;
public string author;
}