Click here to Skip to main content
15,845,330 members
Home / Discussions / C#
   

C#

 
GeneralRe: Datagridview row replay Pin
Richard MacCutchan1-Mar-15 23:38
mveRichard MacCutchan1-Mar-15 23:38 
GeneralRe: Datagridview row replay Pin
Eddy Vluggen2-Mar-15 9:02
professionalEddy Vluggen2-Mar-15 9:02 
Questioncreating a generic factory class that handles ValueTypes ? Pin
BillWoodruff1-Mar-15 0:36
professionalBillWoodruff1-Mar-15 0:36 
AnswerRe: creating a generic factory class that handles ValueTypes ? Pin
OriginalGriff1-Mar-15 1:11
mveOriginalGriff1-Mar-15 1:11 
GeneralRe: creating a generic factory class that handles ValueTypes ? Pin
BillWoodruff1-Mar-15 2:23
professionalBillWoodruff1-Mar-15 2:23 
AnswerRe: creating a generic factory class that handles ValueTypes ? Pin
manchanx1-Mar-15 2:53
professionalmanchanx1-Mar-15 2:53 
GeneralRe: creating a generic factory class that handles ValueTypes ? Pin
BillWoodruff1-Mar-15 7:26
professionalBillWoodruff1-Mar-15 7:26 
AnswerRe: creating a generic factory class that handles ValueTypes ? Pin
Richard Deeming2-Mar-15 3:24
mveRichard Deeming2-Mar-15 3:24 
The core of the problem seems to be that you need to be able to add an increment to an instance of the generic type parameter T. There are various solutions for this - I tend to use some code adapted from the MiscUtil library[^]:
C#
public static class GenericOperator<T>
{
    private static readonly Func<T, T, T> _addChecked = Create<T>(Expression.AddChecked);

    public static Func<T, T, T> AddChecked
    {
        get { return _addChecked; }
    }

    private static Func<T, T, TResult> Create<TResult>(Func<Expression, Expression, BinaryExpression> body)
    {
        try
        {
            Type typeT = typeof(T);
            var left = Expression.Parameter(typeT, "left");
            var right = Expression.Parameter(typeT, "right");

            if (typeT.IsEnum)
            {
                Type enumType = Enum.GetUnderlyingType(typeT);
                var x = Expression.Convert(left, enumType);
                var y = Expression.Convert(right, enumType);

                Expression op = body(x, y);
                if (op.Type == enumType) op = Expression.Convert(op, typeT);

                return Expression.Lambda<Func<T, T, TResult>>(op, left, right).Compile();
            }

            return Expression.Lambda<Func<T, T, TResult>>(body(left, right), left, right).Compile();
        }
        catch (InvalidOperationException ex)
        {
            string message = ex.Message;
            return delegate { throw new InvalidOperationException(message); };
        }
        catch (ArgumentException ex)
        {
            string message = ex.Message;
            return delegate { throw new InvalidOperationException(message); };
        }
    }
}


Since there's no way to get an instance of the generic type parameter that represents "1", you'll need to pass in the increment value to your Add function:
C#
public class VTBunchOLists<T> : List<IEnumerable<T>>
{
    private static readonly Func<T, T, T> AddChecked = GenericOperator<T>.AddChecked;

    public VTBunchOLists()
    {
    }

    public void Add(T startAt, T increment, int howMany, bool ascending = true)
    {
        IEnumerable<T> range = CreateRange(startAt, increment, howMany);
        if (!ascending) range = range.Reverse();
        Add(range);
    }

    private static IEnumerable<T> CreateRange(T startAt, T increment, int howMany)
    {
        T current = startAt;
        for (int i = 0; i < howMany; i++)
        {
            yield return current;
            current = AddChecked(current, increment);
        }
    }
}


If you want the contained ranges to be evaluated eagerly, you can do that in the Add method:
C#
Add(range.ToList());


You can now use this class for any type which implements the addition operator:
C#
var bunchOInt32s = new VTBunchOLists<int>();
bunchOInt32s.Add(10, 1, 15);

var bunchODoubles = new VTBunchOLists<double>();
bunchODoubles.Add(Math.PI, Math.E, 10, false);

public class Foo
{
    public int Number { get; set; }
    
    public static Foo operator+(Foo left, Foo right)
    {
        return new Foo { Number = left.Number + right.Number };
    }
}

var bunchOFoos = new VTBunchOLists<Foo>();
bunchOFoos.Add(new Foo(), new Foo { Number = 5 }, 3);




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


QuestionIs there a way to make "speakers" pickup 440hz? Pin
Member 1148826328-Feb-15 19:13
Member 1148826328-Feb-15 19:13 
AnswerRe: Is there a way to make "speakers" pickup 440hz? Pin
OriginalGriff28-Feb-15 21:05
mveOriginalGriff28-Feb-15 21:05 
AnswerRe: Is there a way to make "speakers" pickup 440hz? Pin
Frankie-C1-Mar-15 2:21
Frankie-C1-Mar-15 2:21 
AnswerRe: Is there a way to make "speakers" pickup 440hz? Pin
GrooverFromHolland1-Mar-15 2:25
GrooverFromHolland1-Mar-15 2:25 
QuestionIs there Any Way to Set the Namespace of a Web Service in Configuration instead of Code? Pin
jojoba2027-Feb-15 23:52
jojoba2027-Feb-15 23:52 
AnswerRe: Is there Any Way to Set the Namespace of a Web Service in Configuration instead of Code? Pin
Dave Kreskowiak28-Feb-15 5:08
mveDave Kreskowiak28-Feb-15 5:08 
AnswerRe: Is there Any Way to Set the Namespace of a Web Service in Configuration instead of Code? Pin
jojoba2028-Feb-15 23:35
jojoba2028-Feb-15 23:35 
QuestionDataGridView Entry Disappears (C#) Pin
Member 1148588827-Feb-15 9:53
Member 1148588827-Feb-15 9:53 
AnswerRe: DataGridView Entry Disappears (C#) Pin
Dave Kreskowiak27-Feb-15 12:26
mveDave Kreskowiak27-Feb-15 12:26 
GeneralRe: DataGridView Entry Disappears (C#) Pin
Member 1148588827-Feb-15 13:08
Member 1148588827-Feb-15 13:08 
GeneralRe: DataGridView Entry Disappears (C#) Pin
Dave Kreskowiak27-Feb-15 14:16
mveDave Kreskowiak27-Feb-15 14:16 
GeneralRe: DataGridView Entry Disappears (C#) Pin
Member 114858881-Mar-15 12:32
Member 114858881-Mar-15 12:32 
GeneralRe: DataGridView Entry Disappears (C#) Pin
Dave Kreskowiak1-Mar-15 13:43
mveDave Kreskowiak1-Mar-15 13:43 
GeneralRe: DataGridView Entry Disappears (C#) Pin
Member 114858881-Mar-15 14:45
Member 114858881-Mar-15 14:45 
GeneralRe: DataGridView Entry Disappears (C#) Pin
Dave Kreskowiak1-Mar-15 16:29
mveDave Kreskowiak1-Mar-15 16:29 
QuestionHow can I pass a Lync url from C sharp to Lync and launch the meeting? Pin
turbosupramk327-Feb-15 7:27
turbosupramk327-Feb-15 7:27 
AnswerRe: How can I pass a Lync url from C sharp to Lync and launch the meeting? Pin
Eddy Vluggen1-Mar-15 23:48
professionalEddy Vluggen1-Mar-15 23:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.