Showing posts with label what is wrong. Show all posts
Showing posts with label what is wrong. Show all posts

Feb 27, 2023

Fun with references in C# - Part I / II

Leia este post em português

Lire cet post en Français.

I'll begin 2023 (late February :)) posts with a quick puzzle about C#.

Given the program below answer the following questions:

1. What is its output ?

2. Can you spot/explain the issues in the code?

3. How could one pinpoint the issue?

Of course you are free to compile/run it, and I do encourage you to do so, but I do suggest to only do it after you have formulated an hypothesis or if only inspecting the code does not ring any bells.

See you in the next post.

Have fun!

Adriano

Amusement avec les références en C# - Partie I / II

Leia este post em português

Read this post in English.

Je commencerai les messages de 2023 (fin février :)) avec une petite casse-tête sur C#.

Compte tenu du programme ci-dessous, répondez aux questions suivantes:

1. Quelle est sa sortie ?

2. Pouvez-vous repérer/expliquer les problèmes dans le code ?

3. Comment identifier le problème ?

Bien sûr, vous êtes libre de le compiler/l'exécuter, et je vous encourage à le faire, mais je suggère de ne le faire qu'après avoir formulé une hypothèse ou si seulement inspecter le code ne vous dit rien. 

A bientôt

Amusez-vous!

Adriano

Diversão com referências em C# - Parte I / II

Read this post in English.

Lire cet post en Français.

Vou começar os posts de 2023 (no final de Fevereiro :)) com um pequeno quebra-cabeças em C#.

Dado o programa abaixo, responda as seguintes questões:

1. O que será impresso?

2. Você é capaz de apontar/explicar os problemas no mesmo?

3. Como poderíamos notar o problema apenas fazendo inspeção do código?

Claro que você pode compilar/rodar o programa (inclusive eu recomendo que você o faça), contudo seria interessante criar uma hipotese antes.

Até o próximo post.

Divirta se!

Adriano

Dec 29, 2010

What's wrong with this code (Part XII)

In the last post in this series ("what's wrong with this code") we discussed how db4o's reference system remembers which objects have been stored / retrieved.

In this post I'll show you another piece of code that doesn't always work as the developer expects:

class User
{
   public string Name;
   public User Manager;
   public IList Addresses;

   public User(string name, User manager)
   {
      Name = name;
      Manager = manager;
   }
}

class Address
{
   public string Street;

   public Address(string street)
   {
      Street = street;
   }
}  


using (var db = Db4oEmbedded.OpenFile(databaseFileName))
{
   List<Address> addrs = new List();
   addrs.Add(new Address("Street1"));
   addrs.Add(new Address("Street2"));
   User fooManager = new User("Foo") { Addresses = addrs };

   User fooBarManager = new User("FooBar", fooManager);
   User barManager = new User("John Doe",  fooBarManager);
   User user = new User("Adriano",  barManager);
    
   db.Store(user);
}

using (var db = Db4oEmbedded.OpenFile(databaseFileName))
{
   var query = db.Query();
   query.Constrain(typeof (User));
   query.Descend("Name").Constrain("Adriano");
   
   User user = (User) query.Execute()[0];
   Console.WriteLine(user.Manager.Manager.Manager.Addresses[0].Street);
}

Can you see the problem? No, I am not talking about Law of Demeter "violation" :) (By the way, if you are interested in this subject you can find a nice discussion here).

Again, if you have been working with db4o for some time I am sure you already spent some time debugging this kind of issue.

As always, the answer comes in a future post.

Have fun!

Adriano

Jul 16, 2009

What's wrong with this code(Part XI) ?

Since I joined db4o I'm actively following our forums and trying to help developers to get their questions / issues answered.

Starting with this post I want to present the most recurring topics (don't know exactly how many) in the format of "What's wrong with this code".

But before we start you may be wondering where are the other parts (since this is titled as Part XI, where are parts from I to X). Well, they do exists in the portuguese posts (titled as "O que esta errado com este código (Parte ?)") so I decided to continue with the numbering schema ;)

Ok, for this first one, take a look in the following code:
using Db4objects.Db4o;
using Db4objects.Db4o.Linq;
using System.Linq;

class Item 
{
 public string Name { get; set; }
 public Item Father { get; set ; }
}

public class Test
{
    private const string DatabaseFile = "wwwtc1.odb";
    private static void Main()
    {
        Item item1 = new Item { Name = "Adriano" };
        using (var db = Db4oEmbedded.OpenFile(DatabaseFile))
        {
           db.Store(item1);
        }
  
        using (var db = Db4oEmbedded.OpenFile(DatabaseFile))
        {
          Item item2 = new Item {Name = "Carol",  Father = item1 };
          db.Store(item2);
        }  
    }
}
If you have done some work with db4o you probably will easily spot the problem.

I'll provide the answer in a future post.

Have fun!