1

I have a code , i get it from my question from stack overflow

# Read data
var data = new SortedDictionary<(string id, string idr, string email), 
    List<(string reference, decimal amount)>>();

using (var input = File.OpenText("input.txt"))
{
    List<(string reference, decimal amount)> currentInvoice = null;

    for (var line = input.ReadLine(); line != null; line = input.ReadLine())
    {
        var fields = line.Split(new char[] { '@' }, 6);
        switch (fields.Length)
        {
            case 2:
                // Sanitize input
                if (fields[0] != "INV")
                {
                    throw new Exception("Unknown record type.");
                }
                if (currentInvoice == null)
                {
                    throw new Exception("Invoice without context.");
                }

                // Parse
                var invoiceEntry = fields[1].Split(new char[] { ' ' }, 
                    StringSplitOptions.RemoveEmptyEntries);

                if (invoiceEntry.Length == 2)
                {
                    decimal amount;
                    if (decimal.TryParse(invoiceEntry[1],
                        NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands,
                        CultureInfo.InvariantCulture,
                        out amount))
                    {
                        currentInvoice.Add((invoiceEntry[0], amount));
                    }
                }

                break;

            case 6:
                var currentContext = (id: fields[0], idr: fields[2], email: fields[5]);
                if (!data.TryGetValue(currentContext, out currentInvoice))
                {
                    currentInvoice = new List<(string reference, decimal amount)>();
                    data.Add(currentContext, currentInvoice);
                }
                break;

            default:
                throw new Exception("Unknown record.");
        }
    }
}

SortedDictionary can't be used it says

' using system.collection.generic.sorteddictionary<TKey,TValue> requires 2 agruments '

the List<(string reference, decimal amount)> currentInvoice = null ; has an error too , it says

'Only assignment,call,increment, decrement, new object expression can be used as a statement'

how to fix this?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
trytocode
  • 393
  • 4
  • 22
  • 1
    Am i missing something here? there is no error with this code in my environment (though thats not to say there is not other issues) – TheGeneral Oct 17 '19 at 01:48
  • @TheGeneral hmm im sure you're not missing something , i dont know why this code error , the SortedDictionary function cannot be used , and i dont know why .. – trytocode Oct 17 '19 at 01:50
  • The compiler on VS2017 tells me: *Impossible de définir une classe ou un membre qui utilise des tuples, car le type 'System.Runtime.CompilerServices.TupleElementNamesAttribute' nécessaire au compilateur est introuvable. Une référence est-elle manquante ?* => ? –  Oct 17 '19 at 02:02
  • Is there anyway to fix this? And i cant understand the language sorry – trytocode Oct 17 '19 at 02:03
  • 1
    @Olivier I think in the past you had to include the `System.ValueTuple` package from NuGet. – ProgrammingLlama Oct 17 '19 at 02:03
  • I think you are using pre C#7 (maybe) – TheGeneral Oct 17 '19 at 02:04
  • @Enrico It says "It's impossible to define a class or member which uses tuples, because the type System.RunTime.CompilerServices.TupleElementNamesAttribute necessary to compile couldn't be found. Are you missing a reference?" - it's not exactly relevant to your situation. You said that you're using Visual Studio 2010, which I thinks limits you to an older version of the .NET Framework (4.0?) which doesn't support value tuples (introduced in 4.7). – ProgrammingLlama Oct 17 '19 at 02:04
  • 1
    @john Yes, it works. Good to know. –  Oct 17 '19 at 02:05
  • 1
    I think its time to upgrade your visual studio. – TheGeneral Oct 17 '19 at 02:06
  • So i cant use this code .. okay thankyou i will find another way to clear my task , thanks for the help – trytocode Oct 17 '19 at 02:06
  • @TheGeneral hehe i want too , but Office PC only has this IDE and i think i must work with my laptop to fix this task .. – trytocode Oct 17 '19 at 02:07
  • You could use a custom class with three string properties instead of the tuple, if that's the issue. – Rufus L Oct 17 '19 at 02:12

1 Answers1

3

For an older version of Visual Studio you may be able to fix this by installing the following from nuget

Note : Take notice of the dependency, you may need to install an older version that supports your targeted framework

It also should be noted, that if you are using VS 2010, it's time to tell your bosses/managers to upgrade, or use the community version.

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141