1

I am trying to write an anonymous(is that the right term?) method that accepts an object and some string parameters to work with.

This method needs to take these parameters, perform an API request, and then return those results.

In order for this to work, I need to be able to pass in the object calling the method, as I need this instantiated object returned with the results. If I can't achieve this then I can't make this method anonymous.

Reason for wanting to achieve this: I will have a dozen or so objects that will need these tasks done and I'd rather not have to copy and paste the bulk of reusable code.

Here is the method's current structure:

    public static async Task<object> gqlQueryMethod(object senderObject,string gqlQueryString, string gqlVarString, string gqlQueryName)
    {
        var query = new GraphQLRequest
        {
            Query = gqlQueryString,
            Variables = gqlVarString
        };
        var graphQLResponse = await _gqlClient.SendQueryAsync(query);

        if (graphQLResponse.Errors != null)
        {
            //Can't access the object that is being passed in as "senderObject"
            var errorExists = senderObject;
            errorExists.errorMessage = graphQLResponse.Errors[0].Message;
            return errorExists;
        }
        //More code logic down here, based on the error check passing or failing
        var apiFetchResults = graphQLResponse.GetDataFieldAs<senderObject>("gqlQuery");

This method attempt is probably the most complex thing I've attempted in my short .NET journey so I feel as though I'm missing something fundamental here, as to why I can't access the sender object inside of that if statement.

My other thought is perhaps I'm not leveraging inheritance correctly, which might help this situation?

Edit:

PasteBin of working "non anonymous" example

David Morin
  • 397
  • 5
  • 25
  • 1
    It's unclear what you want to achieve. What do you intend to pass for `senderObject` when calling this method and why? – GSerg Nov 21 '19 at 08:49
  • 1
    It would appear from your edit that you want `public static async Task gqlQueryMethod(string gqlQueryString, string gqlVarString, string gqlQueryName)` and `var apiFetchResults = graphQLResponse.GetDataFieldAs("gqlQuery");`. – GSerg Nov 21 '19 at 08:55
  • @GSerg I edited the code in my post. I need that senderObject to be dynamic, based on the object that called this method, so that the api fetch gets cast correctly to the matching type and the instantiated, cast, object is returned by the method. I'm starting to think my issue is that I'm passing an object instead of type. I will update my post with a known working snippet that should make my goal clearer. – David Morin Nov 21 '19 at 08:55
  • The paste does not make it too much clearer. Provided that it's `CheckUserExists` we're talking about, it does suggest towards `public static async Task gqlQueryMethod(T senderObject, string gqlQueryString, string gqlVarString, string gqlQueryName)`, but the code has numerous other problems, including [calling async from constructor](https://stackoverflow.com/q/8145479/11683) which is a [fire and forget](https://stackoverflow.com/q/46053175/11683). – GSerg Nov 21 '19 at 09:11
  • Oh my, you're right, I linked the wrong paste, from another question! I've edited it. Line 18 `Task` and line 38 `var existingUser= graphQLResponse.GetDataFieldAs("user");` are the keys to what I need to achieve. I need to anonymously, or generically, pass that "User" class(type?) so that the method can accept any type I pass it. – David Morin Nov 21 '19 at 09:19
  • 1
    So then [it is](https://stackoverflow.com/questions/58970550/unable-to-access-object-sender-parameter-from-if-statement#comment104195964_58970550) `Task gqlQueryMethod(T senderObject, string gqlQueryString, string gqlVarString, string gqlQueryName)` from which you can call `graphQLResponse.GetDataFieldAs("user")`. The question remains where you get the string `"user"` from, but I assume `senderObject` will contain it. – GSerg Nov 21 '19 at 09:38
  • Yes you are correct, it took a bit for me to wrap my head around it but this fixed my issue. I will have to mark Eriawan's answer as the solution though because he did in fact solve my initial question, even though I hadn't realized at the time I was asking two different questions. – David Morin Nov 21 '19 at 10:50

1 Answers1

1

This line of code is wrong:

var errorExists = new senderObject;

Because you can't instantiate an instance of an object, you can only instantiate a type/class. Also the senderObject is the parameter you passed, and it is not a type.

What are you trying to accomplish? Do you mean this, passing the senderObject to errorExists? Please check your code again.

var errorExists = senderObject;
Eriawan Kusumawardhono
  • 4,796
  • 4
  • 46
  • 49
  • If I follow you, when I'm passing "object senderObject", I'm actually instantiating that object when the method fires? Edit: edited my post. – David Morin Nov 21 '19 at 08:48
  • Your code is still wrong. You cannot instantiate an instance of an object. That semantic is wrong in C#. – Eriawan Kusumawardhono Nov 21 '19 at 08:53
  • I haven't edited that line you suggested for posterity.Your suggestion is correct but it doesn't fix what my issue is. I added a line under that to better show my end goal and hopefully make it clearer. – David Morin Nov 21 '19 at 08:58
  • I have implemented your suggestion and added a link to Pastebin showing a working example of the code I'm trying to turn anonymous. With your code change, I can now reference the 'senderObject' variable, but I'm unable to access its properties. – David Morin Nov 21 '19 at 09:07