none
How to print the function value outside of the function without using any other function RRS feed

  • Question

  • Hi ,I am storing a value inside the function. But I want to display that value outside of the function .I don't want using another function or method. How to solve this.

    I am trying this code :

              var value = '';
                con.query('SELECT Mobiles from Salestb' , function (err,rows,callback) {
                    if(err) {
                      throw err;
                      } else {
                     var rsp = rows[0].Mobiles;

                     value = rsp;
                    }
                });
                 console.log(value );

    But the above code output is Null value. Please help me how to solve this 

    Thank you .

                     
    Tuesday, April 23, 2019 10:31 AM

All replies

  • This is my conjecture about what I think might be happening:

    It looks like you are using an anonymous delegate to pass a callback function to con.query, but I am surprised at your use of "function" to do so. This is what I would expect from javascript code, but in C# I expected "delegate" instead of "function".

    Anyway, the "value" variable that you are attempting to set within the function would be captured from the external declaration by a closure. However, the closure captures the variable at the time of defining the function, not at the time that the callback is invoked.

    The closure will use a copy of the string. Even though String is a reference-type, it is peculiar in the sense that strings are immutable and they have copy-by-value semantics. So when you assign a value in value=rsp, the value is assigned to the copy of the variable that is contained in the internal class that the compiler generated to implement the closure. It is not propagated to the external "value" variable.

    In summary: you can only use closures to propagate values into the function in this way, but not out of the function, unless you use a real reference-type for the variable. The string behaves like a value-type in this context.

    Tuesday, April 23, 2019 11:16 AM
    Moderator
  • Hi

    Thank you for posting here.

    For your question, you want to print the function value outside of the function without using any other function.

    You could try the following code.

      class Program
        {
            public class student
            {
                public string mobile { get; set; }
                public string name { get; set; }
    
            }
            public static string value { get; set; }
            static void Main(string[] args)
            {
    
                List<student> rows = new List<student>();
                rows.Add(new student { mobile = "123456", name = "test" });
                bool er = true;
                string callback = "hello";
                function(er, rows, callback);
                Console.WriteLine(value);
                Console.ReadKey();
            }
            public  static void function(bool error,List<student> rows,string callback)
            {
              
                if(error==false)
                {
                    Console.WriteLine("error occurs");
                }
                else
                {
                    var rsp = rows[0].mobile;
                    value = rsp;
                }
            }
    }
    

    Result

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 24, 2019 6:05 AM
    Moderator