operator to access other classes's objects - c#

I need help with this code. If you run the code you'll get in the last 7 lines (which is for...loop for Order object) Exercise.OrderItem.The problem is that I would like to access the OrderItem objects with for...loop but all I get are the last 7 lines representing OrderItem objects.How can I access them in for...loop so that I get the same as in foreach...loop? I think it has something to do with the indexer.Thank You.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Exercise
{
interface IPricing
{
//read/write property
double Price
{
get;
set;
}
double Discount
{
get;
set;
}
}
public class Order:IPricing
{
private string name;
private double price;
private static int OrderNo;
//private ArrayList m_items = new ArrayList();
private ArrayList m_items;
//static constructor
static Order()
{
OrderNo = 0;
}
//default constructor
public Order()
{
name = null;
price = 0;
OrderNo++;
m_items = new ArrayList();
}
//constructor with parameters
public Order(string name, double price)
{
this.name = name;
this.price = price;
OrderNo++;
this.m_items = new ArrayList();
}
//copy constructor
public Order(Order order)
{
this.name = order.name;
this.price = order.price;
this.m_items = order.m_items;
}
public string Name
{
get { return name; }
set { name = value; }
}
public IEnumerable Items
{
get { return m_items; }
private set { }
}
public void AddItem(OrderItem orderItem)
{
orderItem.Order = name;
m_items.Add(orderItem);
}
public static Order operator +(Order o1, Order o2)
{
Order o3 = new Order(o1.name+", "+o2.name,o1.price+o2.price);
o3.m_items.AddRange(o1.m_items);
o3.m_items.AddRange(o2.m_items);
return o3;
}
//indexer
public object this[int index]
{
get
{
m_items[index] = this.m_items[index];
return m_items[index];
}
set { m_items[index] = value; }
}
public double ItemCount
{
get { return m_items.Count; }
private set{}
}
public virtual void Print()
{
Console.WriteLine("*********************Order No. {0}***********************", OrderNo);
Console.WriteLine("Details");
Console.WriteLine("Name: {0}", name);
Console.WriteLine("Price: {0}", price);
}
public double Price
{
get { return price - Discount; }
set { price = value; }
}
public virtual double Discount
{
get { return 0; }
set { ;}
}
public void PrintItems()
{
Console.WriteLine("Items in this order: ");
Console.WriteLine();
foreach(OrderItem itm in this.m_items)
{
Console.WriteLine("Item name: {0,4};\tPart of order: {1}", itm.Name, itm.Order);
}
}
}
public class OrderItem
{
private string m_name; //name of the item in order
private string m_order; //name of the order whose parts are items with names m_name
//default constructor
public OrderItem()
{
m_order = null;
}
//parameter constructor
public OrderItem(string name)
{
this.m_name = name;
this.m_order = null;
}
//copy constructor
public OrderItem(OrderItem orderItem)
{
this.m_name = orderItem.m_name;
this.m_order = orderItem.m_order;
}
//Name read/write property
public string Name
{
get { return m_name; }
set { m_name = value; }
}
//Order read/write property
public string Order
{
get { return m_order; }
set { m_order = value; }
}
}
public class MainProgram
{
static void Main(string[] args)
{
string order1 = "Desktop PC";
Order desktopPC = new Order(order1,25000);
desktopPC.AddItem(new OrderItem("pc mouse"));
desktopPC.AddItem(new OrderItem("keyboard"));
desktopPC.AddItem(new OrderItem("monitor"));
desktopPC.AddItem(new OrderItem("pc"));
desktopPC.Print();
desktopPC.PrintItems();
Console.WriteLine();
string order2 = "Notebook";
Order notebook = new Order(order2, 54000);
notebook.AddItem(new OrderItem("mouse"));
notebook.AddItem(new OrderItem("bag"));
notebook.AddItem(new OrderItem("notebook"));
notebook.Print();
notebook.PrintItems();
Console.WriteLine();
Order total = desktopPC + notebook;
total.Print();
total.PrintItems();
Console.WriteLine();
Console.WriteLine("Getting the items via for loop");
for (int k = 0; k < total.ItemCount; k++)
{
Console.WriteLine(total[k]);
}
Console.ReadKey();
}
}
}

for (int k = 0; k < total.ItemCount; k++)
{
var x = total[k] as OrderItem;
if (x == null) continue;
Console.WriteLine(x.Name);
Console.WriteLine(x.Order);
}

Related

Stack overflow exception with no recursion

I'm having trouble with a stack overflow exception but I can't tell what's causing the exception to be thrown. I'm using a class library that contains all the methods and objects I need and running it from a console application.
Any help would be greatly appreciated as this is part of an assignment that is due in a couple of hours.
Here is my code:
TrafficIncidentNotificationRadiusCalculator class
namespace TrafficIncident
{
public class TrafficIncidentNotificationRadiusCalculator
{
public double meters;
public double CONFIGURED_NOTIFICATION_RADIUS
{
get { return CONFIGURED_NOTIFICATION_RADIUS; }
set { CONFIGURED_NOTIFICATION_RADIUS = meters; }
}
public List<string> GetNotificationRecipientsList(List<User> users, List<UserLocationUpdate> userLocation, TrafficIncidentReport report)
{
int i = 0;
List<string> userNotificationIds = new List<string>();
while (i < userLocation.Count)
{
UserLocationUpdate userLoc = userLocation.ElementAt(i);
userNotificationIds.Add(userLoc.userNotificationId);
Console.WriteLine(userNotificationIds.ElementAt(i));
i++;
}
return userNotificationIds;
}
}
}
TrafficIncidentReport class
namespace TrafficIncident
{
public class TrafficIncidentReport
{
public double[] incidentLocation;
public double latitude
{
get { return latitude; }
set { latitude = value; }
}
public double longitude
{
get { return longitude; }
set { longitude = value; }
}
public void SetIncidentLocation()
{
incidentLocation = new double[] { latitude, longitude };
}
public double[] GetIncidentLocation()
{
return incidentLocation;
}
}
}
User class
namespace TrafficIncident
{
public class User
{
public string userFName
{
get { return userFName; }
set { userFName = value; }
}
public string userLName
{
get { return userLName; }
set { userLName = value; }
}
}
}
UserLocationUpdate class
namespace TrafficIncident
{
public class UserLocationUpdate
{
public string userNotificationId
{
get { return userNotificationId; }
set { userNotificationId = value; }
}
public double lastKnownLatitude
{
get { return lastKnownLatitude; }
set { lastKnownLatitude = value; }
}
public double lastKnownLongitude
{
get { return lastKnownLongitude; }
set { lastKnownLongitude = value; }
}
}
}
And then this is the console application that the class library is running from:
namespace ClassLibraryTestApp
{
class Program
{
static void Main(string[] args)
{
List<User> users = new List<User>();
List<UserLocationUpdate> userLocation = new List<UserLocationUpdate>();
User user1 = new User();
user1.userFName = "Scott";
user1.userFName = "Gersbank";
users.Add(user1);
User user2 = new User();
user2.userFName = "John";
user2.userFName = "Smith";
users.Add(user2);
User user3 = new User();
user3.userFName = "James";
user3.userFName = "Moore";
users.Add(user3);
UserLocationUpdate user1Location = new UserLocationUpdate();
user1Location.lastKnownLatitude = 0;
user1Location.lastKnownLongitude = 0;
user1Location.userNotificationId = "user1";
userLocation.Add(user1Location);
UserLocationUpdate user2Location = new UserLocationUpdate();
user1Location.lastKnownLatitude = 1;
user1Location.lastKnownLongitude = 1;
user1Location.userNotificationId = "user2";
userLocation.Add(user2Location);
UserLocationUpdate user3Location = new UserLocationUpdate();
user1Location.lastKnownLatitude = 2;
user1Location.lastKnownLongitude = 2;
user1Location.userNotificationId = "user3";
userLocation.Add(user3Location);
TrafficIncidentReport trafficReport = new TrafficIncidentReport();
trafficReport.latitude = 1;
trafficReport.longitude = 1;
trafficReport.SetIncidentLocation();
TrafficIncidentNotificationRadiusCalculator TINRC = new TrafficIncidentNotificationRadiusCalculator();
TINRC.meters = 20000;
TINRC.GetNotificationRecipientsList(users, userLocation, trafficReport);
}
}
}
This is not a right way to create properties, define a private field, then the property itself: In your case it will call recursively the set_latitude() method and cause a stack overflow exception.
Wrong:
public double latitude
{
get { return latitude; }
set { latitude = value; }
}
Right:
private double latitude
public double Latitude
{
get { return latitude; }
set { latitude = value; }
}
Or use Auto-Implemented Properties:
public double Latitude { get; set; }
Your code starts with a recursive assignment, The first recursion is here :
public double meters;
public double CONFIGURED_NOTIFICATION_RADIUS
{
get { return CONFIGURED_NOTIFICATION_RADIUS; }
set { CONFIGURED_NOTIFICATION_RADIUS = meters; }
}
What's wrong:
Whenever you set some value to a property it's setter will trigger,
and whenever you access the value of a property the setter will
trigger. in the above mentioned case, you are assigning the property
value in it's setter which will repeatedly trigger the setter and
hance you get the exception
See all of your getter and setter are wrong, You should use a backup variable or else use them as {get;set}. In the case of userNotificationId you should define the property as like the following:
private _UserNotificationId
public string UserNotificationId
{
get { return _UserNotificationId; }
set { _UserNotificationId= value; }
}
Or simply
public string UserNotificationId { get; set; }

collection list and gridview

I want to add Mark1,Mark2,Mark3,Mark4,Mark5 and assign it to the Sum to show in the gridview.
i want to add those marks through any function and assign it to the Sum property. how to do it? thanks
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void procees()
{
List<student> ml= new List<student>();
student s1 = new student() {
Name = "ram",
Id = "gn01",
Mark1 = 90,
Mark2 = 89,
Mark3 = 75,
Mark4 = 45,
Mark5 = 65,
Sum = ""
};
ml.Add(s1);
dataGridView1.DataSource = ml;
}
private void button1_Click(object sender, EventArgs e)
{
procees();
}
}
public class student
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string id;
public string Id
{
get { return id; }
set { id = value; }
}
private int mark1;
public int Mark1
{
get { return mark1; }
set { mark1 = value; }
}
private int mark2;
public int Mark2
{
get { return mark2; }
set { mark2 = value; }
}
private int mark3;
public int Mark3
{
get { return mark3; }
set { mark3 = value; }
}
private int mark4;
public int Mark4
{
get { return mark4; }
set { mark4 = value; }
}
private int mark5;
public int Mark5
{
get { return mark5; }
set { mark5 = value; }
}
private int sum;
public int Sum
{
get { return sum; }
set { sum = value; }
}
}
Remove private field 'sum':
private int sum;
Let the pubic property 'Sum' to calculate the sum:
public int Sum
{
get { return Mark1 + Mark2 + Mark3 + Mark4 + Mark5; }
}

Polymorphism and Interfaces in C#

Create three small classes unrelated by inheritance—classes Building, Car and Bicycle. Write an interface ICarbonFootprint with a GetCarbonFootprint method. Have each of your classes implement that interface, so that its GetCarbonFootprint method calculates an appropriate carbon footprint for that class (check out a few websites that explain how to calculate carbon footprints). Write an app that creates objects of each of the three classes, places references to those objects in List, then iterates through the List, polymorphically invoking each object’s GetCarbonFootprint method. Constructor of Car initialize “gallon of gas”, and the Building constructor will initialize buiding-square-footage.
how to calculate carbon-footprint
One gallon of gas yields 20 pounds of CO2 for a car
Multiply the square footage by 50 for a building
None for a bicycle
My instructor's code:
public static void Main(string[] args)
{
ICarbonFootprint[] list = new ICarbonFootprint[3];
// add elements to list
list[0] = new Bicycle();
list[1] = new Building(2500);
list[2] = new Car(10);
// display carbon footprint of each object
for (int i = 0; i < list.Length; i++)
list[i].GetCarbonFootprint();
} // end Main
}
My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Miller
{
class Program
{
static void Main(string[] args)
{
Bicycle bike = new Bicycle();
Building b = new Building();
Car car = new Car();
List<ICarbonFootprint> list = new List<ICarbonFootprint>();
list.Add(bike);
list.Add(b);
list.Add(car);
int totalCarbon = 0;
foreach (var item in list)
{
totalCarbon += item.GetCarbonFootprint();
Console.WriteLine("{0} has a footprint of: {1}", item, item.GetCarbonFootprint());
}
Console.WriteLine("Total footprint is: {0}", totalCarbon);
Console.ReadKey();
}
}
public class Bicycle : ICarbonFootprint
{
private string _make;
private string _model;
public string Make
{
get { return _make; }
set { _make = value; }
}
public string Model
{
get { return _model; }
set { _model = value; }
}
public int GetCarbonFootprint()
{
return 10;
}
public override string ToString()
{
return string.Format("Bike");
}
}
public class Building : ICarbonFootprint
{
private string _address;
public string Address
{
get { return _address; }
set { _address = value; }
}
public int GetCarbonFootprint()
{
return 2000;
}
public override string ToString()
{
return string.Format("Building");
}
}
public class Car : ICarbonFootprint
{
private string _make;
private string _model;
public string Make
{
get { return _make; }
set { _make = value; }
}
public string Model
{
get { return _model; }
set { _model = value; }
}
public int GetCarbonFootprint()
{
return 1500;
}
public override string ToString()
{
return string.Format("Car");
}
}
public interface ICarbonFootprint
{
int GetCarbonFootprint();
}
}
Me integrating my instructor's code (lines 12-23 changed AKA class Program was the only thing changed):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Miller
{
class Program
{
public static void Main(string[] args)
{
ICarbonFootprint[] list = new ICarbonFootprint[3];
// add elements to list
list[0] = new Bicycle();
list[1] = new Building(2500);
list[2] = new Car(10);
// display carbon footprint of each object
for (int i = 0; i < list.Length; i++)
list[i].GetCarbonFootprint();
} // end Main
}
public class Bicycle : ICarbonFootprint
{
private string _make;
private string _model;
public string Make
{
get { return _make; }
set { _make = value; }
}
public string Model
{
get { return _model; }
set { _model = value; }
}
public int GetCarbonFootprint()
{
return 10;
}
public override string ToString()
{
return string.Format("Bike");
}
}
public class Building : ICarbonFootprint
{
private string _address;
public string Address
{
get { return _address; }
set { _address = value; }
}
public int GetCarbonFootprint()
{
return 2000;
}
public override string ToString()
{
return string.Format("Building");
}
}
public class Car : ICarbonFootprint
{
private string _make;
private string _model;
public string Make
{
get { return _make; }
set { _make = value; }
}
public string Model
{
get { return _model; }
set { _model = value; }
}
public int GetCarbonFootprint()
{
return 1500;
}
public override string ToString()
{
return string.Format("Car");
}
}
public interface ICarbonFootprint
{
int GetCarbonFootprint();
}
}
So, replacing my code for class Program with my instructor's code, I received the following errors:
Program.cs(51,23,51,41): error CS1729: 'Miller.Building' does not contain a constructor that takes 1 arguments
Program.cs(52,23,52,34): error CS1729: 'Miller.Car' does not contain a constructor that takes 1 arguments
Now, because the last two days before Spring break were cancelled due to the weather (snow), we weren't able to discuss. My code seems to do what the directions ask, but I would like to get my instructor's code for class Program working with my code. Could someone help me with these errors possibly?
There are a few issues with your code.
First up you need to include the constructors to make the code compile.
For Building this would look like:
private int squareFootage;
public Building(int squareFootage)
{
this.squareFootage = squareFootage;
}
And for Car this would look like:
private int gasGallons;
public Car(int gasGallons)
{
this.gasGallons = gasGallons;
}
Next, you're not following the rules for calculating the carbon footprint.
They should be:
//Bicycle
public int GetCarbonFootprint()
{
return 0;
}
//Building
public int GetCarbonFootprint()
{
return 50 * squareFootage;
}
//Car
public int GetCarbonFootprint()
{
return 20 * gasGallons;
}
Finally, your instructor's code doesn't actually display any results. The code in the for loop should be changed to be Console.WriteLine(list[i].GetCarbonFootprint()); if this is a console app.
So, all up the code should look like this:
public static void Main(string[] args)
{
ICarbonFootprint[] list = new ICarbonFootprint[3];
// add elements to list
list[0] = new Bicycle();
list[1] = new Building(2500);
list[2] = new Car(10);
// display carbon footprint of each object
for (int i = 0; i < list.Length; i++)
Console.WriteLine(list[i].GetCarbonFootprint());
}
public class Bicycle : ICarbonFootprint
{
public string Make { get; set; }
public string Model { get; set; }
public int GetCarbonFootprint()
{
return 0;
}
}
public class Building : ICarbonFootprint
{
private int squareFootage;
public Building(int squareFootage)
{
this.squareFootage = squareFootage;
}
public string Address { get; set; }
public int GetCarbonFootprint()
{
return 50 * squareFootage;
}
}
public class Car : ICarbonFootprint
{
private int gasGallons;
public Car(int gasGallons)
{
this.gasGallons = gasGallons;
}
public string Make { get; set; }
public string Model { get; set; }
public int GetCarbonFootprint()
{
return 20 * gasGallons;
}
}
public interface ICarbonFootprint
{
int GetCarbonFootprint();
}
I've opted to short-cut the property definitions rather than implement them with fields.
The output is:
0
125000
200
You should write constructors for Building and Car like next:
public Building(int MyValue)
{
...
}
and your code will work fine.
Suggestion: Car and Bicycle shares properties, and the ICarbonFootprint implementation, so you can create a base class with an abstract method. Also the GetCarbonFootprint from ICarbonFootprint interface must be type of System.Double.
public interface ICarbonFootprint
{
int GetCarbonFootprint();
}
public class Building : ICarbonFootprint
{
public int BuildingSquareFootage { get; set; }
public string Address { get; set; }
public Building(int buildingSquareFootage, string address)
{
BuildingSquareFootage = buildingSquareFootage;
Address = address;
}
public int GetCarbonFootprint()
{
return BuildingSquareFootage * 50;
}
public override string ToString()
{
return string.Format("Building");
}
}
public abstract class CarBicycleBase : ICarbonFootprint
{
public string Make { get; set; }
public string Model { get; set; }
protected CarBicycleBase(string make, string model)
{
Make = make;
Model = model;
}
public abstract int GetCarbonFootprint();
}
public class Bicycle : CarBicycleBase
{
public Bicycle(string make, string model)
: base(make, model) { }
public override int GetCarbonFootprint()
{
return 0;
}
public override string ToString()
{
return string.Format("Bike");
}
}
public class Car : CarBicycleBase
{
public int GallonOfGas { get; set; }
public Car(int gallonOfGas, string make, string model)
: base(make, model)
{
GallonOfGas = gallonOfGas;
}
public override int GetCarbonFootprint()
{
return GallonOfGas * 20;
}
public override string ToString()
{
return string.Format("Car");
}
}
Example:
...
var list = new List<ICarbonFootprint>(3)
{
new Car(10, "...", "..."),
new Bicycle("...", "..."),
new Building(20, "...")
};
foreach (ICarbonFootprint item in list)
item.GetCarbonFootprint();
...
I hope it helps.

How can the attributes of outer class can be accesed within the inner class in Composition Classes design?

I am totally unable to access the outer class attributes inside the inner class ...
even if i make object of outer class,, in inner class*which makes no sense in composition design* .. even then i cant access them ..
is there a way by which i can access these outer class attributes ?
Scenario is that there is some sports car which is constructed only if the customers who want to buy it exists! ..
namespace composition{
public class CustomCar
{
#region Attributes
private string name;
private string plateno;
private double cost;
private CarCustomer _customer = new CarCustomer();
#endregion
#region properties
public string Name
{
get { return name; }
set { name = value; }
}
public double Cost
{
get { return cost; }
set { cost = value; }
}
public string PlateNo
{
get { return plateno; }
set { plateno = value; }
}
public CarCustomer Customer
{
get { return _customer; }
set { _customer = value; }
}
#endregion
#region methods
public CustomCar()
{
Console.WriteLine("I am in custom car");
}
public CustomCar(string s1, string pno, double c, string s2, double n, double bc)
{
this.Name = s1;
this.PlateNo = pno;
this.Cost = c;
this.Customer.Name1 = s2;
this.Customer.Nic1 = n;
this.Customer.BargainCost = bc;
}
public double finalCost()
{
if (this.Customer.BargainCost < 10000)
{
double FinalCost = (this.Cost - this.Customer.BargainCost);
return FinalCost;
}
else
{
return this.Cost;
}
}
public void show()
{
Console.WriteLine(this.name + this.PlateNo + this.Customer.Name1 + this.Customer.Nic1);
}
#endregion
public class CarCustomer
{
private string name1;
private double Nic;
private double bargainCost;
public double BargainCost
{
get { return bargainCost; }
set { bargainCost = value; }
}
public double Nic1
{
get { return Nic; }
set { Nic = value; }
}
public string Name1
{
get { return name1; }
set { name1 = value; }
}
public CarCustomer()
{
Console.WriteLine("I have a customer");
}
public CarCustomer(string n1, double i1, double bc)
{
this.Name1 = n1;
this.Nic = i1;
this.BargainCost = bc;
}
public void showCustomer()
{
Console.WriteLine("Customer name: " + Name1);
Console.WriteLine("Customer NIC: " + Nic1);
}
}
}
}
There is nothing stopping you having a reference in the CarCustomer to the CustomCar object as well. This would then give you a one to one reference between the object. Were you instaiate this object is up to you in the Constructor of the CustomCar
public CustomCar(arguments)
{
this.Customer.CustomCar = this;
}
Or you could set it in the sets on the property accessors up to you. Try this
public class CustomCar
{
private string name;
private string plateno;
private double cost;
private CarCustomer _customer = new CarCustomer();
public string Name
{
get { return name; }
set { name = value; }
}
public double Cost
{
get { return cost; }
set { cost = value; }
}
public string PlateNo
{
get { return plateno; }
set { plateno = value; }
}
public CarCustomer Customer
{
get { return _customer; }
set { _customer = value; }
}
public CustomCar()
{
Console.WriteLine("I am in custom car");
}
public CustomCar(string name, string pno, double c, string customerName, double n, double bc)
{
this.Name = name;
this.PlateNo = pno;
this.Cost = c;
this.Customer.Name1 = customerName;
this.Customer.Nic1 = n;
this.Customer.BargainCost = bc;
this.Customer.Car = this;
}
public double finalCost()
{
if (this.Customer.BargainCost < 10000)
{
double FinalCost = (this.Cost - this.Customer.BargainCost);
return FinalCost;
}
else
{
return this.Cost;
}
}
public void show()
{
Console.WriteLine(this.name + this.PlateNo + this.Customer.Name1 + this.Customer.Nic1);
}
}
public class CarCustomer
{
private string name1;
private double Nic;
private double bargainCost;
private CustomCar customer;
public double BargainCost
{
get { return bargainCost; }
set { bargainCost = value; }
}
public double Nic1
{
get { return Nic; }
set { Nic = value; }
}
public string Name1
{
get { return name1; }
set { name1 = value; }
}
public CustomCar Car
{
get{return customer;}
set{customer = value;}
}
public CarCustomer()
{
Console.WriteLine("I have a customer");
}
public CarCustomer(string n1, double i1, double bc)
{
this.Name1 = n1;
this.Nic = i1;
this.BargainCost = bc;
}
public void showCustomer()
{
Console.WriteLine("Customer name: " + Name1);
Console.WriteLine("Customer NIC: " + Nic1);
}
}
Of course you can't access them. You've set their protection level to private. In order to get at them from an external resource their protection level has to be in line with the access level needed. In this case you should be able to change the modifier to protected and be able to access them.
However, looking at your class design, I think you would be better served using the automatic getter/setter syntax. You aren't doing anything particularly special in your property definitions, so it would make sense to get rid of the private variables and change your properties to this:
public string Name { get; set; }
public double Cost { get; set; }
public string PlateNo { get; set; }
public CarCustomer Customer { get; set; }
You'll still have public access to the variables through the properties and you won't have all the messiness of the extra variables.

Errors: How to save a many-to-many relationship in Castle Active Record?

I've been trying for hours to get many-to-many relationship to save with Castle ActiveRecord. What am I doing wrong? I can't find anything in the documentation or on google. There is data in the database.
Courses have a many to many relationship with Books.
Test code.
Database.Course c = new Database.Course();
c.Number = "CS 433";
c.Name = "Databases";
c.Size = 34;
c.Books = Database.Book.FindAll();
c.Save();
Also doesn't work
foreach(Database.Book b in Database.Book.FindAll()){
c.Books.Add(b);
}
Database Classes
[ActiveRecord]
public class Course : ActiveRecordValidationBase<Course>
{
private int? id;
private string number;
private string name;
private string description;
private int size; //number of students in class
//references
private IList books = new ArrayList();
public override string ToString()
{
return FormattedName;
}
public string FormattedName
{
get
{
return string.Format("{0} - {1}", Number, Name);
}
}
[PrimaryKey]
public int? Id
{
get { return id; }
set { id = value; }
}
[Property, ValidateNonEmpty]
public string Number
{
get { return number; }
set { number = value; }
}
[Property, ValidateNonEmpty]
public string Name
{
get { return name; }
set { name = value; }
}
[Property(ColumnType="StringClob")]
public string Description
{
get { return description; }
set { description = value; }
}
[Property]
public int Size
{
get { return size; }
set { size = value; }
}
[HasAndBelongsToMany(typeof(Book),
Table = "BookCourse", ColumnKey = "course_id", ColumnRef = "book_id", Inverse = true)]
public IList Books
{
get { return books; }
set { books = value; }
}
}
[ActiveRecord]
public class Book : ActiveRecordValidationBase<Book>
{
private int? id;
private string title;
private string edition;
private string isbn;
private bool is_available_for_order;
//relations
private IList authors = new ArrayList();
private IList bookordercount = new ArrayList();
private IList courses = new ArrayList();
private Inventory inventory;
public override string ToString()
{
return FormattedName;
}
public string FormattedName
{
//*
get {
string str;
if (Edition == null || Edition == "")
str = Title;
else
str = string.Format("{0} ({1})", Title, Edition);
if (Authors.Count != 0)
{
return string.Format("{0} by {1}", str, FormattedAuthors);
}
else
{
return str;
}
}
/*/
get
{
return Title;
}
//*/
}
public string FormattedAuthors
{
get
{
if (Authors.Count == 0) return "";
StringBuilder sb = new StringBuilder();
int i = 0, end = Authors.Count;
foreach (Author a in Authors)
{
i++;
sb.Append(a.FormattedName);
if (i != end) sb.Append("; ");
}
return sb.ToString();
}
}
[PrimaryKey]
public int? Id
{
get { return id; }
set { id = value; }
}
[Property, ValidateNonEmpty]
public string Title
{
get { return title; }
set { title = value; }
}
[Property]
public string Edition
{
get { return edition; }
set { edition = value; }
}
[Property, ValidateNonEmpty]
public string Isbn
{
get { return isbn; }
set { isbn = value; }
}
[Property]
public bool IsAvailableForOrder
{
get { return is_available_for_order; }
set { is_available_for_order = value; }
}
//relations
[HasAndBelongsToMany(typeof(Author),
Table = "BookAuthor", ColumnKey = "book_id", ColumnRef = "author_id")]
public IList Authors
{
get { return authors; }
set { authors = value; }
}
[HasMany(typeof(BookOrderCount), Table = "BookOrderCounts", ColumnKey = "BookId")]
public IList BookOrderCount
{
get { return bookordercount; }
set { bookordercount = value; }
}
[HasAndBelongsToMany(typeof(Course),
Table = "BookCourse", ColumnKey = "book_id", ColumnRef = "course_id")]
public IList Courses
{
get { return courses; }
set { courses = value; }
}
[OneToOne]
public Inventory Inventory
{
get { return inventory; }
set { inventory = value; }
}
}
Make sure you put the Inverse = true where you want it. From the Castle AR docs,
It is wise to choose one side of the
relation as the owner. The other side,
the non-writable, need to use
Inverse=true.
Put the Inverse = true on the other side of the relationship, like this:
[HasAndBelongsToMany(typeof(Book),
Table = "BookCourse", ColumnKey = "course_id", ColumnRef = "book_id")]
public IList<Book> Books
[HasAndBelongsToMany(typeof(Course),
Table = "BookCourse", ColumnKey = "book_id", ColumnRef = "course_id", Inverse = true)]
public IList<Course> Courses
You also have to add attributes to the top of both classes - at the moment they don't know what tables they're mapped to. Currently you have this:
public class Course : ActiveRecordBase<Course>
Add this (where "course" is the name of your Course table):
[ActiveRecord("course")]
public class Course : ActiveRecordBase<Course>

Categories