Click here to Skip to main content
15,877,384 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# listbox Pin
Richard MacCutchan21-Mar-18 22:30
mveRichard MacCutchan21-Mar-18 22:30 
AnswerRe: C# listbox Pin
Gerry Schmitz22-Mar-18 8:48
mveGerry Schmitz22-Mar-18 8:48 
QuestionBuilding Biometeric Application Pin
Member 1351208221-Mar-18 5:37
Member 1351208221-Mar-18 5:37 
AnswerRe: Building Biometeric Application Pin
OriginalGriff21-Mar-18 6:47
mveOriginalGriff21-Mar-18 6:47 
AnswerRe: Building Biometeric Application Pin
Gerry Schmitz22-Mar-18 6:15
mveGerry Schmitz22-Mar-18 6:15 
Questiongeneralizing a function to operate on any type with operator * defined Pin
Alexander Kindel20-Mar-18 13:16
Alexander Kindel20-Mar-18 13:16 
AnswerRe: generalizing a function to operate on any type with operator * defined Pin
Alexander Kindel20-Mar-18 14:52
Alexander Kindel20-Mar-18 14:52 
AnswerRe: generalizing a function to operate on any type with operator * defined Pin
BillWoodruff21-Mar-18 18:34
professionalBillWoodruff21-Mar-18 18:34 
See if this givs you some ideas:
public static class GenericMathExtensions
{
    public static double? Exp<T>(this T tvalue, double topower)
    where T : 
        struct, 
        IComparable, 
        IComparable<T>, 
        IConvertible, 
        IEquatable<T>, 
        IFormattable
    {
        if (typeof(T).IsNumeric())
        {
            try
            {
                return Math.Pow(Convert.ToDouble(tvalue), topower);
            }
            catch
            {
                 // throw invalid cast exception ?
            }
        }

        return null;
    }

    // based on:
    // https://stackoverflow.com/a/13179018/133321
    public static bool IsNumeric(this Type type)
    {
        switch (Type.GetTypeCode(type))
        {
            case TypeCode.Byte:
            case TypeCode.SByte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.Decimal:
            case TypeCode.Double:
            case TypeCode.Single:
                return true;
            case TypeCode.Object:
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    return Nullable.GetUnderlyingType(type).IsNumeric();
                    //return IsNumeric(Nullable.GetUnderlyingType(type));
                }
                return false;
            default:
                return false;
        }
    }
}
Note: the Convert.ToDouble method can handle other types than those this code restricts the usage to.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12

QuestionMessage Removed Pin
20-Mar-18 3:37
Member 1373673020-Mar-18 3:37 
Questioni want to create a chat application without the need of a server Pin
Ma Lek20-Mar-18 0:09
Ma Lek20-Mar-18 0:09 
AnswerRe: i want to create a chat application without the need of a server Pin
OriginalGriff20-Mar-18 0:26
mveOriginalGriff20-Mar-18 0:26 
GeneralRe: i want to create a chat application without the need of a server Pin
Ma Lek20-Mar-18 1:32
Ma Lek20-Mar-18 1:32 
SuggestionRe: i want to create a chat application without the need of a server Pin
Ralf Meier20-Mar-18 1:43
mveRalf Meier20-Mar-18 1:43 
GeneralRe: i want to create a chat application without the need of a server Pin
OriginalGriff20-Mar-18 1:44
mveOriginalGriff20-Mar-18 1:44 
AnswerRe: i want to create a chat application without the need of a server Pin
Ralf Meier20-Mar-18 1:40
mveRalf Meier20-Mar-18 1:40 
AnswerRe: i want to create a chat application without the need of a server Pin
Kevin Marois20-Mar-18 7:14
professionalKevin Marois20-Mar-18 7:14 
AnswerRe: i want to create a chat application without the need of a server Pin
MadMyche20-Mar-18 8:47
professionalMadMyche20-Mar-18 8:47 
QuestionSending byte to comport Pin
Iftikhar_Ahmed19-Mar-18 7:13
Iftikhar_Ahmed19-Mar-18 7:13 
AnswerRe: Sending byte to comport Pin
Richard MacCutchan19-Mar-18 9:38
mveRichard MacCutchan19-Mar-18 9:38 
QuestionHow to build a PDF maker program at C# Pin
User 1367511418-Mar-18 5:44
User 1367511418-Mar-18 5:44 
AnswerRe: How to build a PDF maker program at C# Pin
Richard MacCutchan18-Mar-18 5:48
mveRichard MacCutchan18-Mar-18 5:48 
AnswerRe: How to build a PDF maker program at C# Pin
Gerry Schmitz18-Mar-18 8:01
mveGerry Schmitz18-Mar-18 8:01 
GeneralRe: How to build a PDF maker program at C# Pin
User 1367511419-Mar-18 0:19
User 1367511419-Mar-18 0:19 
GeneralRe: How to build a PDF maker program at C# Pin
Gerry Schmitz19-Mar-18 6:49
mveGerry Schmitz19-Mar-18 6:49 
GeneralRe: How to build a PDF maker program at C# Pin
User 1367511424-Mar-18 2:19
User 1367511424-Mar-18 2:19 

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.