0
\$\begingroup\$

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
asked Mar 16, 2013 at 14:01
\$\endgroup\$
2
  • 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\$ Commented 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\$ Commented Mar 16, 2013 at 16:30

2 Answers 2

6
\$\begingroup\$

The code will work, so from that point of view, it is correct.

But there are also some issues with it:

  1. I don't see any reason for the InnerJoinTables class to exist. You should just use List<InnerJoinTable> directly in the calling code. That's unless there's something else going on that you're not showing.
  2. Public fields should not be used. Use a property instead.
answered Mar 16, 2013 at 14:18
\$\endgroup\$
0
\$\begingroup\$

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.

answered Mar 19, 2013 at 12:11
\$\endgroup\$
3
  • 2
    \$\begingroup\$ This is not necessarily a good idea. I prefer composition over inheritance, given a choice. \$\endgroup\$ Commented Mar 19, 2013 at 13:14
  • \$\begingroup\$ Could you explain how is that better than using List directly? \$\endgroup\$ Commented 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\$ Commented Apr 9, 2013 at 8:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.