Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Suppose I have a DB as shown. For now I want to focus on the relationships among the tables.

DB structure
************
table1: a (int), b (string), c (string)
table2: a (int), d (int), e (string)
{table2.a references table1.a}
table3: d (int), f (string), g (string)
{table3.d references table2.d}
table4: a (int), h (string), i (string)
{table4.a references table1.a}

I found that these relationships could be replicated and enforced at an object level also. I think I found the below idea from MSDN, though I am not sure.... Anyways, according to that concept, I have the below code for the objects.

C#
class table1
{
	public Int32 a { get; set; }
	public String b { get; set; }
	public String c { get; set; }

	public ICollection<table2> t2 { get; set; }
	public ICollection<table4> t4 { get; set; }

	public table1()
	{
		t2 = new List<table2>;
		t4 = new List<table4>;		
	}
}

class table2
{
	public Int32 a { get; set; }
	public Int32 d { get; set; }
	public String e { get; set; }

	public ICollection<table3> { get; set; }
	public virtual table1 { get; set; }

	public table2()
	{
		t3 = new List<table3>;
	}
}

class table3
{
	public Int32 d { get; set; }
	public String f { get; set; }
	public String g { get; set; }

	public virtual table2 { get; set; }
}

class table4
{
	public Int32 a { get; set; }
	public String h { get; set; }
	public String i { get; set; }

	public virtual table1 { get; set; }
}


Now, I can see the advantage in this. I mean, I can get the parent table table1's object and along with it I also get a list of related data from table2 and table4 in the same object and so on...
I can assign the related data to a new table2 object, and add that object to table1 object's ICollection<table2> field thing.

What I am not sure of is, the 'virtual' member of objects table2, table3 and table4... I can understand that they refer to the respective 'parent' Object... But how do I assign the parent table's related values to this member? I mean, being virtual, VS throws an error if I try to assign values to them in the DAL layer... Same thing happens when i hover the cursor over the object and when the tooltip/hint thing that shows the object's content pops up, i can see the values of all the members of the object, except for 'virtual' members if any. The virtual members show an error of not being able to be evaluated or so and the program seems to exit or crash...

So, how do i exactly use the virtual members?
Posted
Comments
Sergey Alexandrovich Kryukov 19-Jul-13 9:55am    
You should not write a single line without understanding what you are doing. Virtual members are used to be overridden in derived classes, in case of properties, overridden are their getters or setters. And you provided no information on "error" and the code casing them. It it error or exception? Where? Crash? Come on, software developers don't have "crash", only exceptions and other stuff...
—SA
princektd 22-Jul-13 8:30am    
something like 'not being able to be evaluated' or 'cannot be evaluated' is what is shown before the debug run stops on its own... No exception or other messages...

I worked out that the getter and setter has to be overridden. But where do I do it? In the DAL class?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900