Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Saving objects with Cross-reference

Saving objects with Cross-reference

Scheduled Pinned Locked Moved C#
csharphelplinqsalesjson
24 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    Looks good enough for me. Can you now post the definition of the Kund and Book-classes? I hope that each book has a unique number, or GUID, and I hope to see a list of those numbers in the Kund-object that borrowed them. My guess is that you're saving the lists without the references.

    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

    L Offline
    L Offline
    larsp777
    wrote on last edited by
    #9

    Well, you set the unique number when you register a book (Bok) or a customer (kund). It's not a Commercial application.

    Book:

    [Serializable()]
    public class Bok : ISerializable

    {
    protected int isbn = 0;
    protected string titel = null;
    protected string author;
    protected int price;
    protected String isType = null;
    protected Kund biblioteksKund = null;

        //egenskaper (properties). Används för att förhindra åtkomst till privata variabler.
        public int ISBN
        {
            get
            {
                return isbn;
            }
            set
            {
                isbn = value;
            }
        }
    
        public string Titel
        {
            get
            {
                return titel;
            }
            set
            {
                titel = value;
            }
        }
    
        public string Author
        {
            get
            {
                return author;
            }
            set
            {
                author = value;
            }
        }
    
        public int Price
        {
            get
            {
                return price;
            }
            set
            {
                price = value;
            }
        }
    
        public String IsType
        {
            get
            {
                return isType;
            }
            set
            {
                isType = value;
            }
        }
        
        public Kund BiblioteksKund
        {
            get
            {
                return biblioteksKund;
            }
            set
            {
                biblioteksKund = value;
            }
        }
    
    
        //Konstruktor (constructor) Metod som körs när objektet skapas. Denna körs om det inte finns parametrar.
        public Bok() 
        {
        }
    
        //Konstruktor med parametrar
        public Bok(int isbn, string titel, string author, int price, SerializationInfo info, StreamingContext ctxt)
        {
            ISBN = isbn;
            Author = author;
            Titel = titel;
            Price = price;
            biblioteksKund = new Kund();
            this.isbn = (int)info.GetValue("ISBN", typeof(int));
            this.author = (string)info.GetValue("Author", typeof(string));
            this.titel = (string)info.GetValue("Titel", typeof(string));
            this.price = (int)inf
    
    L 1 Reply Last reply
    0
    • L larsp777

      Well, you set the unique number when you register a book (Bok) or a customer (kund). It's not a Commercial application.

      Book:

      [Serializable()]
      public class Bok : ISerializable

      {
      protected int isbn = 0;
      protected string titel = null;
      protected string author;
      protected int price;
      protected String isType = null;
      protected Kund biblioteksKund = null;

          //egenskaper (properties). Används för att förhindra åtkomst till privata variabler.
          public int ISBN
          {
              get
              {
                  return isbn;
              }
              set
              {
                  isbn = value;
              }
          }
      
          public string Titel
          {
              get
              {
                  return titel;
              }
              set
              {
                  titel = value;
              }
          }
      
          public string Author
          {
              get
              {
                  return author;
              }
              set
              {
                  author = value;
              }
          }
      
          public int Price
          {
              get
              {
                  return price;
              }
              set
              {
                  price = value;
              }
          }
      
          public String IsType
          {
              get
              {
                  return isType;
              }
              set
              {
                  isType = value;
              }
          }
          
          public Kund BiblioteksKund
          {
              get
              {
                  return biblioteksKund;
              }
              set
              {
                  biblioteksKund = value;
              }
          }
      
      
          //Konstruktor (constructor) Metod som körs när objektet skapas. Denna körs om det inte finns parametrar.
          public Bok() 
          {
          }
      
          //Konstruktor med parametrar
          public Bok(int isbn, string titel, string author, int price, SerializationInfo info, StreamingContext ctxt)
          {
              ISBN = isbn;
              Author = author;
              Titel = titel;
              Price = price;
              biblioteksKund = new Kund();
              this.isbn = (int)info.GetValue("ISBN", typeof(int));
              this.author = (string)info.GetValue("Author", typeof(string));
              this.titel = (string)info.GetValue("Titel", typeof(string));
              this.price = (int)inf
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #10

      larsp777 wrote:

      It's not a Commercial application.

      That makes no difference.

      larsp777 wrote:

      Well, you set the unique number when you register a book (Bok) or a customer (kund).

      What does 'skrivUt' mean? There's no list on "who" borrowed "wich" book. Remember that a ISBN-number doesn't idenitfy a specific book, but a "title". One could have multiple copies of the same title :)

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      L 1 Reply Last reply
      0
      • L Lost User

        larsp777 wrote:

        It's not a Commercial application.

        That makes no difference.

        larsp777 wrote:

        Well, you set the unique number when you register a book (Bok) or a customer (kund).

        What does 'skrivUt' mean? There's no list on "who" borrowed "wich" book. Remember that a ISBN-number doesn't idenitfy a specific book, but a "title". One could have multiple copies of the same title :)

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        L Offline
        L Offline
        larsp777
        wrote on last edited by
        #11

        Well, since it is run in a controlled environment I can make sure that there is only one copy with a specific ISBN. Otherwise you are right of course, ISBN identyfies a title, not a specific book. No, there is no list of who borrowed which book but each book-object "knows" who borrowed it with a reference to a customer object. protected Kund biblioteksKund = null; SkrivUt means "print" and is simply printing data about a customer but I don't think is is ever used here. (It was initially a assignment from my University made in Java.) The reason I mentioned that it is not a Commercial application was that I could made sure that no two objects are the same. But maybe that doesn't matter. Shold I use something like a GUID?

        L 1 Reply Last reply
        0
        • L larsp777

          Well, since it is run in a controlled environment I can make sure that there is only one copy with a specific ISBN. Otherwise you are right of course, ISBN identyfies a title, not a specific book. No, there is no list of who borrowed which book but each book-object "knows" who borrowed it with a reference to a customer object. protected Kund biblioteksKund = null; SkrivUt means "print" and is simply printing data about a customer but I don't think is is ever used here. (It was initially a assignment from my University made in Java.) The reason I mentioned that it is not a Commercial application was that I could made sure that no two objects are the same. But maybe that doesn't matter. Shold I use something like a GUID?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #12

          larsp777 wrote:

          The reason I mentioned that it is not a Commercial application was that I could made sure that no two objects are the same. But maybe that doesn't matter. Shold I use something like a GUID?

          I dunno, and this is the place where things get complicated. Let's say I borrowed Pratchetts' book "Small Gods". You have three books of this story - how are you gonna track each book you lent? Answer; give every book a unique number (yeah, like a GUID) :)

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          L 1 Reply Last reply
          0
          • L Lost User

            larsp777 wrote:

            The reason I mentioned that it is not a Commercial application was that I could made sure that no two objects are the same. But maybe that doesn't matter. Shold I use something like a GUID?

            I dunno, and this is the place where things get complicated. Let's say I borrowed Pratchetts' book "Small Gods". You have three books of this story - how are you gonna track each book you lent? Answer; give every book a unique number (yeah, like a GUID) :)

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            L Offline
            L Offline
            larsp777
            wrote on last edited by
            #13

            Eddy Vluggen wrote:

            dunno, and this is the place where things get complicated.
             
            Let's say I borrowed Pratchetts' book "Small Gods". You have three books of this story - how are you gonna track each book you lent?
             
            Answer; give every book a unique number (yeah, like a GUID) :)

            Yes, but as I said it's a controlled environment where I make sure that there is only one copy of each book. The question is if this actually is the reason to why it doesn´t work? Or could it be that the list I am trying to save is a empty list even if I use the same name? This is the event for the button where I register the loan.

            private void btnRegister_Click(object sender, EventArgs e)
            {
            Kund lender = null;
            Bok bookToLend = null;

                    //Search for chosen customer
                    foreach (Kund K in CustomerList)
                    {
                        //Om rätt kund hittas
                        if (txtPersonNr.Text.Equals(K.PersonNr.ToString()))
                            lender = K; //Kunden som hittats läggs i ny variabel.
                    }
                    
                    //If customer wasn't found.
                    if (lender == null)
                    {
                        textBox7.Text = "Kund saknas!";
                        return;
                    }
            
                    //Search for chosen book.
                    foreach (Bok B in BookList)
                    {
                        //Om rätt bok hittas
                        if (textBox6.Text.Equals(B.ISBN.ToString()))
                            bookToLend = B;
                    }
            
                    //if book wasn´t found.
                    if (bookToLend == null)
                    {
                        textBox7.Text = "Bok saknas!";
                        return;
                    }
            
                                         
            
                    if (bookToLend.BiblioteksKund == null) //If book doesn´t have its customer object set.
                    {
                        bookToLend.BiblioteksKund = lender; //Sets the customerobject of the book.
            
                        lender.Loan.Add(bookToLend);    //Places the book to lend in the customers booklist.
            
                        textBox7.Text = "Lån registrerat!";
                    }
            
                   
            
            
                    
                }
            
            L 1 Reply Last reply
            0
            • L larsp777

              Eddy Vluggen wrote:

              dunno, and this is the place where things get complicated.
               
              Let's say I borrowed Pratchetts' book "Small Gods". You have three books of this story - how are you gonna track each book you lent?
               
              Answer; give every book a unique number (yeah, like a GUID) :)

              Yes, but as I said it's a controlled environment where I make sure that there is only one copy of each book. The question is if this actually is the reason to why it doesn´t work? Or could it be that the list I am trying to save is a empty list even if I use the same name? This is the event for the button where I register the loan.

              private void btnRegister_Click(object sender, EventArgs e)
              {
              Kund lender = null;
              Bok bookToLend = null;

                      //Search for chosen customer
                      foreach (Kund K in CustomerList)
                      {
                          //Om rätt kund hittas
                          if (txtPersonNr.Text.Equals(K.PersonNr.ToString()))
                              lender = K; //Kunden som hittats läggs i ny variabel.
                      }
                      
                      //If customer wasn't found.
                      if (lender == null)
                      {
                          textBox7.Text = "Kund saknas!";
                          return;
                      }
              
                      //Search for chosen book.
                      foreach (Bok B in BookList)
                      {
                          //Om rätt bok hittas
                          if (textBox6.Text.Equals(B.ISBN.ToString()))
                              bookToLend = B;
                      }
              
                      //if book wasn´t found.
                      if (bookToLend == null)
                      {
                          textBox7.Text = "Bok saknas!";
                          return;
                      }
              
                                           
              
                      if (bookToLend.BiblioteksKund == null) //If book doesn´t have its customer object set.
                      {
                          bookToLend.BiblioteksKund = lender; //Sets the customerobject of the book.
              
                          lender.Loan.Add(bookToLend);    //Places the book to lend in the customers booklist.
              
                          textBox7.Text = "Lån registrerat!";
                      }
              
                     
              
              
                      
                  }
              
              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #14

              larsp777 wrote:

              Or could it be that the list I am trying to save is a empty list even if I use the same name?

              Looks that way; a Kund holds a list of books. When you serialize the Kund, you write the PersonNr and the Name - but not the list. Again, I'd recommend not saving a list of books, but numbers that are linked to a book.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              L 1 Reply Last reply
              0
              • L Lost User

                larsp777 wrote:

                Or could it be that the list I am trying to save is a empty list even if I use the same name?

                Looks that way; a Kund holds a list of books. When you serialize the Kund, you write the PersonNr and the Name - but not the list. Again, I'd recommend not saving a list of books, but numbers that are linked to a book.

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                L Offline
                L Offline
                larsp777
                wrote on last edited by
                #15

                Eddy Vluggen wrote:

                Again, I'd recommend not saving a list of books, but numbers that are linked to a book.

                Ok, Think I missunderstood you some. But I still have to save the objects somehow so I'm not really sure how that solves anything. Please explain. Edit: Realised that you probably ment the list saved in customer, not the list that holds all the books. That could be a way of avoiding cross-reference I guess.

                L 1 Reply Last reply
                0
                • L larsp777

                  Eddy Vluggen wrote:

                  Again, I'd recommend not saving a list of books, but numbers that are linked to a book.

                  Ok, Think I missunderstood you some. But I still have to save the objects somehow so I'm not really sure how that solves anything. Please explain. Edit: Realised that you probably ment the list saved in customer, not the list that holds all the books. That could be a way of avoiding cross-reference I guess.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #16

                  larsp777 wrote:

                  But I still have to save the objects somehow so I'm not really sure how that solves anything. Please explain.

                  It'd be a translation from a database-design. Right now, each book would be stored in a Kund? That means that you'd be "moving" the book-data when the book moves between different Kunds; it'd be saved in a different list. If you had three lists, life could become easier; one list to hold a collection of Kunds, one to hold the Books, and one to hold a pointer to Kund/Book combinations. In SQL, it'd be something like below;

                  CREATE TABLE Customer
                  (
                  Id INT IDENTITY(1,1)
                  PRIMARY KEY (Id)
                  )

                  CREATE TABLE Book
                  (
                  Id INT IDENTITY(1,1)
                  PRIMARY KEY (Id)
                  )

                  CREATE TABLE LentItems
                  (
                  CustomerId INT,
                  BookId INT
                  FOREIGN KEY (CustomerId) REFERENCES Book(Id),
                  FOREIGN KEY (BookId) REFERENCES Book(Id)
                  )

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    larsp777 wrote:

                    But I still have to save the objects somehow so I'm not really sure how that solves anything. Please explain.

                    It'd be a translation from a database-design. Right now, each book would be stored in a Kund? That means that you'd be "moving" the book-data when the book moves between different Kunds; it'd be saved in a different list. If you had three lists, life could become easier; one list to hold a collection of Kunds, one to hold the Books, and one to hold a pointer to Kund/Book combinations. In SQL, it'd be something like below;

                    CREATE TABLE Customer
                    (
                    Id INT IDENTITY(1,1)
                    PRIMARY KEY (Id)
                    )

                    CREATE TABLE Book
                    (
                    Id INT IDENTITY(1,1)
                    PRIMARY KEY (Id)
                    )

                    CREATE TABLE LentItems
                    (
                    CustomerId INT,
                    BookId INT
                    FOREIGN KEY (CustomerId) REFERENCES Book(Id),
                    FOREIGN KEY (BookId) REFERENCES Book(Id)
                    )

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                    L Offline
                    L Offline
                    larsp777
                    wrote on last edited by
                    #17

                    Eddy Vluggen wrote:

                    Right now, each book would be stored in a Kund? That means that you'd be "moving" the book-data when the book moves between different Kunds; it'd be saved in a different list.

                    This is how it works (as far as I know): Every book is stored in a list BookList and every customer is saved in CustomerList; Every customer (Kund) has it´s own list loan where references are saved to books that are borrowed by that customer. Again, this was a project I made in Java for a University-course a while back. Been trying to transfer it to C#. Every book (Bok in Swedish) has a reference to the customer who borrowed the book, so it "knows" who borrowed it. The savingpart worked fine in Java but maybe works differently in C#.

                    L 1 Reply Last reply
                    0
                    • L larsp777

                      Eddy Vluggen wrote:

                      Right now, each book would be stored in a Kund? That means that you'd be "moving" the book-data when the book moves between different Kunds; it'd be saved in a different list.

                      This is how it works (as far as I know): Every book is stored in a list BookList and every customer is saved in CustomerList; Every customer (Kund) has it´s own list loan where references are saved to books that are borrowed by that customer. Again, this was a project I made in Java for a University-course a while back. Been trying to transfer it to C#. Every book (Bok in Swedish) has a reference to the customer who borrowed the book, so it "knows" who borrowed it. The savingpart worked fine in Java but maybe works differently in C#.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #18

                      larsp777 wrote:

                      Again, this was a project I made in Java for a University-course a while back. Been trying to transfer it to C#.
                      Every book (Bok in Swedish) has a reference to the customer who borrowed the book, so it "knows" who borrowed it. The savingpart worked fine in Java but maybe works differently in C#.

                      Those references are pointers; I don't think that the XmlSerializer is going to save the private loan-list on it's own.

                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                      L 1 Reply Last reply
                      0
                      • L Lost User

                        larsp777 wrote:

                        Again, this was a project I made in Java for a University-course a while back. Been trying to transfer it to C#.
                        Every book (Bok in Swedish) has a reference to the customer who borrowed the book, so it "knows" who borrowed it. The savingpart worked fine in Java but maybe works differently in C#.

                        Those references are pointers; I don't think that the XmlSerializer is going to save the private loan-list on it's own.

                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                        L Offline
                        L Offline
                        larsp777
                        wrote on last edited by
                        #19

                        Eddy Vluggen wrote:

                        Those references are pointers; I don't think that the XmlSerializer is going to save the private loan-list on it's own.

                        Yes, I know they are pointers. That is why you get cross-reference, isn´t it? Still, you could be right in that this is the problem. Edit: I think this was one thing I considered when I was trying to find the answer. Saving a list of books was no problem.

                        L 1 Reply Last reply
                        0
                        • L larsp777

                          Eddy Vluggen wrote:

                          Those references are pointers; I don't think that the XmlSerializer is going to save the private loan-list on it's own.

                          Yes, I know they are pointers. That is why you get cross-reference, isn´t it? Still, you could be right in that this is the problem. Edit: I think this was one thing I considered when I was trying to find the answer. Saving a list of books was no problem.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #20

                          Does the original Java-generated have it's loan-list in the XML?

                          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                          L 1 Reply Last reply
                          0
                          • L Lost User

                            Does the original Java-generated have it's loan-list in the XML?

                            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                            L Offline
                            L Offline
                            larsp777
                            wrote on last edited by
                            #21

                            No, I don't think I did it in XML. I saved the objects in a binary format.

                            L 1 Reply Last reply
                            0
                            • L larsp777

                              No, I don't think I did it in XML. I saved the objects in a binary format.

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #22

                              larsp777 wrote:

                              No, I don't think I did it in XML. I saved the objects in a binary format.

                              :) If you save a collection using the binary-formatter, then it'll also save the pointers. If you use XML, you'll have to save the relations yourself, or keep a list.

                              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                              L 1 Reply Last reply
                              0
                              • L Lost User

                                larsp777 wrote:

                                No, I don't think I did it in XML. I saved the objects in a binary format.

                                :) If you save a collection using the binary-formatter, then it'll also save the pointers. If you use XML, you'll have to save the relations yourself, or keep a list.

                                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                                L Offline
                                L Offline
                                larsp777
                                wrote on last edited by
                                #23

                                So...what do you suggest I do...

                                L 1 Reply Last reply
                                0
                                • L larsp777

                                  So...what do you suggest I do...

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #24

                                  Use the binaryformatter[^] to store it as binary data. It's that, or doing some extra work to have it correctly in XML.

                                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups