\$\begingroup\$
\$\endgroup\$
2
I would like to know if this is being done correctly.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
public class InnerJoinTables
{
public List<InnerJoinTable> Items = new List<InnerJoinTable>();
}
public class InnerJoinTable
{
public int ID { get; set; }
public string RightTable { get; set; }
public string LeftTable { get; set; }
public InnerJoinTable(int ID, string RightTable, string LeftTable)
{
this.ID = ID;
this.RightTable = RightTable;
this.LeftTable = LeftTable;
}
}
Calling code:
InnerJoinTables m = new InnerJoinTables();
m.Items.Add(new InnerJoinTable(1, "A", "B"));
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
-
1\$\begingroup\$ Does it seem to work? What exactly are you worried about? Why do you think it might not be the correct way? \$\endgroup\$svick– svick2013年03月16日 14:15:47 +00:00Commented Mar 16, 2013 at 14:15
-
\$\begingroup\$ You can achieve the same result using a stored procedure, an ORM or LINQ. What do you find the need to implement this join yourself? \$\endgroup\$Leonid– Leonid2013年03月16日 16:30:41 +00:00Commented Mar 16, 2013 at 16:30
2 Answers 2
\$\begingroup\$
\$\endgroup\$
The code will work, so from that point of view, it is correct.
But there are also some issues with it:
- I don't see any reason for the
InnerJoinTables
class to exist. You should just useList<InnerJoinTable>
directly in the calling code. That's unless there's something else going on that you're not showing. - Public fields should not be used. Use a property instead.
answered Mar 16, 2013 at 14:18
\$\begingroup\$
\$\endgroup\$
3
You can directly derive your InnerJoinTables class with List. Then you can use it as normal list object. No need of having items as member there.
-
2\$\begingroup\$ This is not necessarily a good idea. I prefer composition over inheritance, given a choice. \$\endgroup\$Brian– Brian2013年03月19日 13:14:28 +00:00Commented Mar 19, 2013 at 13:14
-
\$\begingroup\$ Could you explain how is that better than using
List
directly? \$\endgroup\$svick– svick2013年03月20日 22:39:53 +00:00Commented Mar 20, 2013 at 22:39 -
\$\begingroup\$ It just change the way you would use that object in your code. If you have an InnerJoinTables and Items as collection of InnerJoinTable object. Each time you want to access the object you would write innerJoinTables.Items.Add(). If you just have it derived with list then it would be like innerJoinTables.Add(new InnerJoinTable()), that's why I thought its better than using list directly. \$\endgroup\$Vivek Dixit– Vivek Dixit2013年04月09日 08:09:51 +00:00Commented Apr 9, 2013 at 8:09
lang-cs