0

I have a problem with function using ref parameter. This function call herself using linq like this :

public Champ(tabloidConfigColonne tcc,ref Dictionary<string, Jointure> jointures)
            {
              ...
             sousChamps = lc.ToDictionary(
                            o => o.nom,
                            o => new Champ(o, ref jointures));
             }

An error appear saying ref not available in anonymous function.

Full function is here

public Champ(tabloidConfigColonne tcc,ref Dictionary<string, Jointure> jointures)
        {
            nom = tcc.champ;
            description = tcc.titre;
            type = tcc.type;
            valeurDefaut = tcc.valeurParDefaut;
            modeEdition=new Template(tcc.editeur, tcc.editeurParam1, tcc.editeurParam2, tcc.editeurParam3);
            if (!string.IsNullOrEmpty(tcc.jointure))
            {
                jointure = jointures[tcc.jointure];
                nomTable = jointure.nomNouvelleTable;
            }

            visu=tcc.visu;
            Groupe=tcc.groupe;
            Id=tcc.nom;

            valideurs = tcc.valideurs;
            Obligatoire = tcc.obligatoire;

            if (tcc.colonnes.Count>0)
            {
                List<tabloidConfigColonne> lc = tcc.colonnes.GetColonnes(visibiliteTools.getFullVisibilite(),false);
                sousChamps = lc.ToDictionary(
                    o => o.nom,
                    o => new Champ(o, ref jointures));
            }
        }

thanks for your help.

JD11
  • 306
  • 3
  • 11
  • 13
    Yes, you can't use `ref` in an anonymous function. Do you understand why? It looks like you shouldn't be using `ref` in the first place - it's not like you change the value in the `Champ` constructor. Please read http://pobox.com/~skeet/csharp/parameters.html – Jon Skeet Jun 11 '13 at 16:16
  • Remove the `ref`, and your code will compile. You are not assigning `jointures`, so passing by `ref` does not really matter in your code (a reference to `Dictionary` will be passed by value, which is perfectly fine in your case). – Sergey Kalinichenko Jun 11 '13 at 16:23
  • Not sure why you'd pass a dictionary using ref anyway, being a reference type, unless you need to actually update the pointer to a new object its unnecessary. – Ashigore Jun 11 '13 at 16:23
  • I use ref to do not duplicate my dictionary. It work fine without but is it the better way? – JD11 Jun 11 '13 at 16:31
  • You don't copy the dictionary, you copy the reference. – Brian Rasmussen Jun 11 '13 at 16:43

1 Answers1

1

I don't have enough rep to comment, so...

No need to use ref for reference types (objects) unless you will make a new instance of that object inside the function.

See this SO post for more explanation on ref: C# ref is it like a pointer in C/C++ or a reference in C++?

Community
  • 1
  • 1
delrocco
  • 495
  • 1
  • 4
  • 23