Resolved Calling function with parameter params object[]

Ali Bawi

New member
Joined
Jan 10, 2023
Messages
1
Programming Experience
10+
I have a function that accept parameter (params object[]).
When the function is called with null value for the parameter the called function received a value for this parameter {object[0]} this parameter passed to another function
in the function implementation the parameter has a value of {object[2]} with values [0] null, [1] {object[0]}.
It is as the function is making new object {} for each time we pass the parameter.

Why is this happening we only want to pass the null value without creating new object.
As the images shown
 

Attachments

  • Method1.jpg
    Method1.jpg
    21.8 KB · Views: 14
  • Method2.png
    Method2.png
    39.9 KB · Views: 13
  • Method3.png
    Method3.png
    56.6 KB · Views: 16
Please do not post only pictures of code. If a screenshot adds value then you can provide that in addition but ALWAYS post the code as text, formatted as code. We can't copy your code from a picture if we want to run it ourselves or modify it to show you what's wrong with it.
 
I wanted to check that this thread was not marked Resolved by accident. I thought I approved it but perhaps I accidentally resolved it. If the issue really is resolved, it is considered good form to post that resolution so that it might help others with similar issues.
 
*sigh* this would be so much easier if your OP had posted text in code tags rather than as screenshots. Now the OP is forcing us to type in code to show where the problem is. But no! I'm going to do unto the OP as he has done unto us by also posting scribbles on top of his screenshots.

1673357677129.png


1673358121309.png


There is a extra null parameter being passed into QueryAsync<T>(IDbConnection, string, int?, CommandType?, params object[]). The extra null ends up taking up the slot of the commandType parameter, so the real commandType parameters ends up being the first parameters optional parameter. So the parameters you are passing into your call don't line up with parameters this method.

I suggest redesigning your method signatures to let the compiler help you find the error. Don't use nullable types for default parameters where they can be mistaken for optional parameters. Or use stricter type for the optional parameters (e.g. don't use object[]).
 
Tip: C# supports named parameters. Using them will help straighten out a mess like this

Perhaps just throw this method away and use Dapper instead
 
Back
Top Bottom