2

I am getting an error message that makes absolutely no sense to me!!

public class ChartInitializerBase
{
    #region Constructors
    public ChartInitializerBase(Func<object> retrieveData)
    {
        var Data = retrieveData;
    }
    #endregion
}

The idea of the constructor above is that I can pass different methods in to obtain data

For example, if I am displaying books I want to say

var intiialiser = new ChartInitializerBase(GetBooks(1234, 231))

If I am getting CDs I want to say

var intiialiser = new ChartInitializerBase(GetCDs(1234, 231, 34))

The parameters above are different, but both GetCDs and GetBooks would return a single object

In my exact situation I have

var chart = new DailyConsumptionChart(
            dataProvider.GetDataForDailyConsumptionChart("1", EUtilityGroup.Electricity, 321, 157, Convert.ToDateTime("01/01/2010"), Convert.ToDateTime("01/01/2010 23:30"), false));



 public class ProfileDataDashboardReportsDataProvider
 {
    #region Methods
    public object GetDataForDailyConsumptionChart(string idString, EUtilityGroup ug, int customerId, int userId, DateTime startDate, DateTime endDate, bool clockTime)
    {
        var mdp = new MeterDataProvider();
        var result = mdp.GetProfileDataForLocationForADay(idString, ug, customerId, userId, startDate, endDate, clockTime);

        return result;
    }
    #endregion
}

I have tried this as both a static an non static class as well as static and non static methods

It makes no difference which way I do it I still get Argument type object is not assignable to System.Func error

I am new to this kind of thing, please can someone suggest what I need to do to fix this?

I have seen this Parameter type not assignable when storing func in dictionary

But I dont think it helps me

I am not sure if I am tagging this in the correct areas please feel free to change if I am wrong

Paul


as per Codeigniter standard you have to follow the MVC pattern so:

Model -> Controller ->view

now, assuming you want to visualize layout2.php view you have 2 chances:

1 - load view directly where you need $this->load->view('layout2');

2 - create url function ad hoc kind of www.site.com/layout/layout1 and www.site.com/layout/layout2:

controller layout.php

    class Layout extends CI_Controller {

        function layout1(){
            $this->load->view('layout1');
    }
        function layout2(){
            $this->load->view('layout2');
    }
}

i really suggest you to look at How To Create a Controller in Codeigniter Doc

4

1 に答える 1

7

以下のコード例のように、メソッド実行の結果ではなく、ラムダをコンストラクターに渡すようにしてください。

var initialiser = new ChartInitializerBase(() => GetBooks(1234, 231));

ここでは、GetBooks メソッドの呼び出しという 1 つのステートメントでラムダを渡すだけです。

コンストラクターは次のようにする必要があります

public ChartInitializerBase(Func<object> retrieveData) 
{ 
      var Data = retrieveData(); //here the method GetBooks will be called and data returned
}
于 2013-03-31T22:25:09.620 に答える