views:

35

answers:

3

this http://stackoverflow.com/questions/3975659/calling-the-function-which-is-stored-in-a-string-variable

wrote the following, but there are two errors which i can not solve, do not understand why they are coming

(the error is mentioned with the statement causing it as //error is)

please help

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
//using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string function_name;
        function_name = "one";
        caller(function_name);


    }



    static void caller(String function_name)
    {
        // Get a type from the string 
        Type type = typeof(_Default);
        // Create an instance of that type
        Object obj = Activator.CreateInstance(type);
        // Retrieve the method you are looking for
        MethodInfo methodInfo = type.GetMethod(function_name);
        // Invoke the method on the instance we created above
        methodInfo.Invoke(obj, null);




    }




    public void one()  //function  
    {

        string a = "\n this is the alphabet a \n";


        ***//error is***
        ////Object reference not set to an instance of an object.
        ////Label1.Text = "one i called";



        ***//error is***
        /////Response is not available in this context.
         //// Response.Write(a);
    }// function one ends





}
+3  A: 

it looks like you want to work with the current page (instance of a _default) instead of creating a new one.

try passing this into caller, and replacing obj with it.

Rob Fonseca-Ensor
that's your solution
remi bourgarel
i tried what u said, but the "this" does not come within the scope of the function, would you be more specific as to where to add this??
and why would i pass this in to the caller, the caller has the method name. it has got nothing to do with the obj. i dont understand..
@user287745, that's because your `caller` method is static. And you need to call the method on the current page, not on a new page that will never be sent to the client.
Thomas Levesque
i got it, ) have posted the correct code, for others..
+1  A: 

Response belongs to the current HttpContext that is set to the Page's Response property and you are not getting the right context using Activator.CreateInstance() I guess. If you use HttpContext.Current.Response.Write(a) instead of Response.Write(a), it works:

HttpContext.Current.Response.Write(a)

For the label case, you need:

Label lbl = (HttpContext.Current.Handler as Page).FindControl("Label1") as Label;
lbl.Text = "one i called";

This exactly does what you mean I guess. But do you really need to do this, or is it just for practice.

Musa Hafalır
okay but what about label? and other functions
I have added label case now.
Musa Hafalır
thanks, your method is great to understand how to access things on the same page indirectly +1
A: 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string function_name;
        function_name = "one";
        caller(function_name, this);


    }



    static void caller(String function_name, object obj)
    {
        // Get a type from the string 
        //Type type = Type.GetType("_Default");

        Type type = typeof(_Default);
        // Create an instance of that type
        ////////Object obj = Activator.CreateInstance(type);
        // Retrieve the method you are looking for
        MethodInfo methodInfo = type.GetMethod(function_name);
        // Invoke the method on the instance we created above
        methodInfo.Invoke(obj, null);

    }




    public void one()  //function  
    {

        string a = "\n this is the alphabet a \n";


        //error is NOT
        ////Object reference not set to an instance of an object.
        Label1.Text = "one i called";



        //error is NOT
        /////Response is not available in this context.
        Response.Write(a);
    }// function one ends





}