-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: Balasubramanian1
Message 1 in Discussion
Hi All
We have three classes Purchaseorder , orderedItem and childItems.
In purchase class we have array of ordereditem object. In orderItem class we
have array of ChildItems.
when we try to assign value to child item we are getting error,object
reference not set.
how to assign childitem object to orderitem . See the below code.
if you have any other way plz suggess me. I am getting error in Bold
Code.
public class
PurchaseOrder
{
public Address
ShipTo;
public string OrderDate;
/* The XmlArrayAttribute changes the XML
element name
from the default of "OrderedItems" to "Items". */
[XmlArrayAttribute("Items")]
public OrderedItem[]
OrderedItems;
public decimal SubTotal;
public decimal ShipCost;
public decimal TotalCost;
}
public class Address
{
/* The XmlAttribute instructs the
XmlSerializer to serialize the Name
field as an XML attribute instead of an XML element (the default
behavior). */
[XmlAttribute]
public string Name;
public string Line1;
/* Setting the IsNullable property to false
instructs the
XmlSerializer that the XML attribute will not appear if
the City field is set to a null reference. */
[XmlElementAttribute(IsNullable = false)]
public string City;
public string State;
public string Zip;
}
public class ChildItems
{
public string name;
public string qty;
}
public class OrderedItem
{
public string ItemName;
public string Description;
public decimal UnitPrice;
public int Quantity;
public decimal LineTotal;
[XmlArrayAttribute("ChildItem")]
public ChildItems[]
ChildItem;
/* Calculate is a custom method that
calculates the price per item,
and stores the value in a field. */
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
public class Test
{
public static void Main()
{
// Read and write purchase
orders.
Test t = new
Test();
t.CreatePO("po.xml");
t.ReadPO("po.xml");
}
private void CreatePO(string filename)
{
// Create an instance of the XmlSerializer
class;
// specify the type of object to
serialize.
XmlSerializer serializer =
new
XmlSerializer(typeof(PurchaseOrder));
TextWriter writer = new
StreamWriter(filename);
PurchaseOrder po = new
PurchaseOrder();
// Create an address to ship and bill
to.
Address billAddress = new Address();
billAddress.Name = "Teresa Atkinson";
billAddress.Line1 = "1 Main St.";
billAddress.City = "AnyTown";
billAddress.State = "WA";
billAddress.Zip = "00000";
po.ShipCost = (decimal)23.6;
po.ShipTo = billAddress;
po.SubTotal = (decimal)67.5;
po.TotalCost = (decimal)45.6;
po.OrderDate = "OrderDate";
OrderedItem ord = new
OrderedItem();
ord.ItemName = "test";
ord.LineTotal = 56;
ord.Quantity = 3;
ord.UnitPrice = 56;
ord.Description = "desx";
ChildItems chk = new
ChildItems();
chk.name = "mouse";
chk.qty = "10";
ord.ChildItem[0] = chk;
po.OrderedItems[0] = ord;
Regards
Bala S
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/bdotnet/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you
received this message by mistake, please click the "Remove" link below. On the
pre-addressed e-mail message that opens, simply click "Send". Your e-mail
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]