0

I have the following situation which I think is a perfect use of delegates, however, I I'm new to using delegates and am having some problems getting it to work.

I call the AddVariable method from this code, which decides the appropriate value for T.

switch (datum.DataType) {
            case SupportedDataTypes.Int:
                table = AddVariable<int>(table, datum);
                break;
            case SupportedDataTypes.Float:
                table = AddVariable<float>(table, datum);
                break;
            case SupportedDataTypes.String:
                table = AddVariable<string>(table, datum);
                break;
            case SupportedDataTypes.ChooseType:
                throw new ArgumentException("type not chosen");
            default:
                throw new ArgumentOutOfRangeException();
        }

But AddVariable just selects the appropriate method based on another switch. These methods need to be generic since they act on several different types.

static DataTable AddVariable<T>(DataTable table, Datum datum) {

    DataTable newTable = table.Clone();
    AddVariableColumn(datum, newTable);

    switch (datum.MixingTypeOfVariable) {
        case VariableMixingType.Balanced:
            table = AddBalancedValues<T>(table, datum);
            break;
        case VariableMixingType.Looped:
            table = AddLoopedValues<T>(table, datum);
            break;
        case VariableMixingType.EvenProbability:
            table = AddEvenProbabilityValues<T>(table, datum);
            break;
        case VariableMixingType.CustomProbability:
            throw new NotImplementedException();
        default:
            throw new ArgumentOutOfRangeException();
    }

    Debug.Log($"table now has {table.Rows.Count} rows");
    return newTable;
}

I thought of making AddLooped, AddBalanced, and AddEvenProbability values to be delegates, but I can't seem to make any delegates with < T >. Then I could choose the delegate and call that method without a need for AddVariable at all. They all have the same signature (except for T) and basically do the same thing, but modified slightly.

Edit: pseudo code expected:

AddVariable< int >(table, datum, function to apply values);

Note that the function to apply values needs to know it’s < int > too. Since it’s generic as well.

I found some discussions online about using generic delegates but they seemed to all have something to do with a Stack. Which doesn't sound like what I want.

I saw some things online about using Func<> but it wasn't clear that that is the right path, and I didn't fully understand it.

It's also possible that my approach is backwards, so if there are any ideas on how to improve design, that would be great as well.

Adam B
  • 3,662
  • 2
  • 24
  • 33
  • How do you expect it to look in target state (in pseudocode)? – DarkWanderer Jan 31 '19 at 16:11
  • [Generic delegate](https://codereview.stackexchange.com/q/18095/79220)? What is Stack? – Sinatr Jan 31 '19 at 16:39
  • I added pseudo code expected. I don’t know what a stack is. That’s why I didn’t understand the examples. My question is about how to do a delegate (or something similar) for a generic function. – Adam B Feb 01 '19 at 03:51
  • @AdamB - There are ways of doing what you want, but you really need to provide a [mcve]. I would like to know the all the types, all the methods, all the input data, etc, you are using. I don't want to guess what your code means. – Enigmativity Feb 01 '19 at 07:14
  • @Enigmativity Ok, based on your feedback I made a small program that highlights my issue more clearly with all the side code cut out. Should I edit my question? or delete this one and post a new one? – Adam B Feb 13 '19 at 18:56
  • @AdamB - Since you have no answers you could edit your question. The general rule is that you should not change a question so that it invalidates existing answers (and possibly comments). In any case you can always append to an existing question. – Enigmativity Feb 13 '19 at 22:02
  • You may be looking for the strategy in this answer: https://stackoverflow.com/a/15579884/176877 – Chris Moschini Sep 12 '22 at 13:56

0 Answers0