Ken
Ken

Reputation: 41

valuetuple doesn't work for one of my 4.7.2 projects

I have multiple projects in the solution, all targeting 4.7.2. I then add a method in a lib project that returns valueTuple. Multiple projects call this method, all projects are fine(compiled) except one project. This one project generates compiler error.

Error   CS1061  ValueTuple<IEnumerable<User>, Dictionary<string, string>, Dictionary<string, string>>' does not contain a definition for 'CreatedUsers' and no extension method 'CreatedUsers' accepting a first argument of type 'ValueTuple<IEnumerable<User>, Dictionary<string, string>, Dictionary<string, string>>' could be found (are you missing a using directive or an assembly reference?)

I've compared all nugets pkgs in this project with other projects, minor difference, I then unisntall those different nugets, still the same error. I searched around, knowing 4.7 has built in valuetuple, I then checked this project, it doesn't not have System.ValueTuple nuget, or hardcoded System.ValueTuple.dll anywhere.

Please help. I can always go back to the Tuple<> way, but valuetuple is cleaner. Unless you tell me there's more hidden issues ahead.

Thanks!

The method that returns valuetuple 

public static async Task<(IEnumerable<User> CreatedUsers, Dictionary<string, string> ErrorMsgsA, Dictionary<string, string> ErrorMsgsB)> CreateNewUsers(string param1, CreateUserDTO param2)

{

... ...

... ...

return (createdUs, ErrorMsgs_a, ErrorMsgs_b);

}

The line that causes error:

var createUserResult = await CreateNewUsers(param1, param2);
var created = createUserResult.CreatedUsers; //error

So that's it, compiler complains about not finding items in returned tuple. I listed here the compiler error which complains about the first item, "CreatedUsers", in valuetuple. There are actually one compiler error for each of other valuetuple items, "ErrorMsgsA", ErrorMsgsB. And remember, my other projects who reference itmes in this valuetuple are fine, no error.

Upvotes: 1

Views: 1337

Answers (3)

Schwarzie2478
Schwarzie2478

Reputation: 2276

My problem was not at compile error, but during runtime.

But I suspect the fix is still usefull to some and could help somebody here too.

After switching my project to .Net framework 4.7.2 I had to manually update the hintpath for System.ValueTuple from net461 to net47

<HintPath>..\Solutions\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>

Upvotes: 2

Ken
Ken

Reputation: 41

Okay, I found out why.

The problem project is using an older compiler.

I had to remove two packages, which were there for years, likely came with the source code we bought. Made me sweat thinking for these years we've been using software compiled by such an old compiler, but it is what it is:

Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0 Microsoft.Net.Compilers.1.0.0

Now problem is gone.

Upvotes: 3

Eriawan Kusumawardhono
Eriawan Kusumawardhono

Reputation: 4896

Your method signature is having syntax error of declaring ValueTuple. Because you cannot have ValueTuple property name initialization inside a generic type declaration such as Task<T>. All of your declared property name inside a ValueTuple must have a concrete type.

Change your method signature from:

public static async Task<(IEnumerable<T1> CreatedUsers, Dictionary<string, string> ErrorMsgsA, Dictionary<string, string> ErrorMsgsB)> CreateNewUsers(string param1, dtotype param2)

to:

public static async Task<ValueTuple<IEnumerable<T1>, Dictionary<string, string>, Dictionary<string, string>> CreateNewUsers(string param1, dtotype param2)

Or, you could change T1 as concrete type, for example an Object:

public static async Task<(IEnumerable<Object> CreatedUsers, Dictionary<string, string> ErrorMsgsA, Dictionary<string, string> ErrorMsgsB)> CreateNewUsers(string param1, dtotype param2)

Then you could compile your code.

Upvotes: 0

Related Questions