0

I'm trying to create a foreign key in a class, B, to the id of another class, A. The problem is that I can't verify the ID of A's objects until near the end of the program when all my instances of B have already been created. I would like to be able to instantiate objects of B with a reference to the id of an A object so that when the id of the A object changes, so does the value of the foreign key in B. I know I can't do that directly in C# without using 'unsafe' mode so I've been looking for alternative methods. As a last resort I could do a loop at the end through all my B objects, but B is actually many different classes and objects and there are enough of them that I would much prefer a more efficient solution.

Here's a rough example of the logic that I need.

using System;
namespace sandbox
{
    public class A
    {
        public int ID;

        public A(int id)
        {
            ID =  id;
        }
    }
    public class B
    {
        public int reftoAID;

        public B(int id)
        {
            reftoAID = id;
        }
    }

    public class TestMain
    {
        static void Main()
        {

            A a = new A(1);
            B b = new B(a.ID);
            a.ID = 3;
            Console.WriteLine(b.reftoAID); //prints 1, but I need 3
            Console.ReadLine();
        }
    }
}

I have tried creating a wrapper class with an int property that holds the id, but I haven't been able to figure out how to get it into the database using linqtodb. I thought I would be able to just define an (int) cast, but that doesn't work. From what I can tell, linqtodb uses the System.Data.DbType enum to determine the type and I don't know if there's a way to have it convert before the lookup.

I'm looking either for a new approach to the problem or a way of converting the wrapper class in such a way that it can be written to the database.

user3747260
  • 465
  • 1
  • 5
  • 14

2 Answers2

0

C# doesn't use pointers for referencing objects. Instead, C# has the concept of value types and reference types. All classes are reference types, which means that a variable of that type is a reference to an object. This is very similar to pointers in other programming languages.

Using direct references, your code would look like this (some name changes for clarity):

public class ClassA
{
    public int ID;

    public ClassA(int id)
    {
        ID = id;
    }
}

public class ClassB
{
    public ClassA A;

    public ClassB(ClassA a)
    {
        A = a;
    }
}

public class TestMain
{
    static void Main(string[] args)
    {
        ClassA a = new ClassA(1);
        ClassB b = new ClassB(a);
        a.ID = 3;
        Console.WriteLine(b.A.ID); //prints 3
        Console.ReadLine();
    }
}

The constructor for ClassB takes a reference to an object of type ClassA, so any changes to that object are reflected when it is accessed through the A property of ClassB.

BJ Myers
  • 6,617
  • 6
  • 34
  • 50
  • I'm writing it to a table in an sql database, so I need it in a separate variable in a type that can be converted. I could loop through all my B objects at the end, but because of the structure and number of objects, I would rather not resort to this. It may be what I end up doing though if there aren't alternatives. – user3747260 Mar 04 '16 at 00:34
0

I was able to solve this using properties, which I had completely forgotten about when I asked this question initially. Using BJ Myers' example:

public class ClassB
{
    ClassA A;

    public int AID { get {return A.ID;} }

    public ClassB(ClassA a)
    {
        A = a;
    }
}
user3747260
  • 465
  • 1
  • 5
  • 14