Question if (arg is int) not working?

justhumm

Member
Joined
Jun 2, 2023
Messages
15
Programming Experience
Beginner
I was putting together an Excel add-in with some User Defined Functions (UDFs), using C#. Based on excelDNA documentation, I created a class that checks to see if OPTIONAL Excel arguments are present in a UDF. And if not, it assigns a default value.

I created a couple of simple test functions to see if the OPTIONAL class is working "correctly". They just add 100 to the Excel argument. (Excel screenshot attached below) Checks for double data types (and others) seem to be working, but INT data types aren't working. It seems to me like the "if (arg is Int)" isn't catching anything and I can't figure out why.

Stupid beginner question...Can anyone point out what I'm doing wrong? Thanks!

C#:
    public static class OptionalArg
    // https://excel-dna.net/docs/guides-basic/optional-parameters-and-default-values/
    // internal static class OptionalArg
    {
            // check to see if optional argument, if present, is an integer
        internal static int Check(object arg, int defaultValue)
        {
            if (arg is int)
                return (int)arg;
            else if (arg is ExcelMissing)
                return defaultValue;
            else
                throw new ArgumentException(); // Will return #VALUE to Excel
        }
        // check to see if optional argument, if present, is a double
        internal static double Check(object arg, double defaultValue)
        {
            if (arg is double)
                return (double)arg;
            else if (arg is ExcelMissing)
                return defaultValue;
            else
                throw new ArgumentException();  // Will return #VALUE to Excel
        }
    }


    public class FuncString : XlCall
    // public static class FuncString   
    // static classes are non-instantiable. Static classes cannot be inherited from another class.
    {
       // ==================== START of Function ====================
        // Description for IntelliSense Tool Tip
        [ExcelFunction(Description = "Function to test the optional argument (integer) class.")]
        public static int C_TestInt(object intArg)
        {
            int addend = OptionalArg.Check(intArg, 1); // "1" is the default value

            return 100 + addend;
        }
                // ==================== START of Function ====================
        // Description for IntelliSense Tool Tip
        [ExcelFunction(Description = "Function to test the optional argument (double) class.")]
        public static double C_TestDbl(object dblArg)
        {
            double addend = OptionalArg.Check(dblArg, 1.5); // "1.5" is the default value

            return 100 + addend;
        }
    }

exceltest.png
 
If I remember correctly, Excel keeps all numbers as doubles internally, but displays depending on user settings. I think that you need to test if the value has any fractional parts.
 
Thanks @Skydiver . That sorta' makes sense.

A while back, I remember reading that INT's aren't actually used in native VBA anymore. I've always used just them in VBA macros because INT's still work and it makes it a little easier to follow the code in my feeble, little mind.

I'll have to look into that a bit more.

The patient says, "Doctor, it hurts when I do this."
The doctor says, "Then don't do that!”
 
It seems as though you haven't actually debugged your code. You ALWAYS need to debug your code BEFORE posting a question here. If you don't know how to debug then you should stop what you're doing and learn because it's a required skill for all developers.

What you should have done in this case was place a breakpoint on that line and then, when it was hit, you'd have been able to see that arg wasn't type int to begin with, whether you thought it should be or not. That doesn't necessarily mean that you wouldn't have a question to ask but you would have been able to ask the right question, rather than asking why something isn't working when it is doing exactly what it should. NEVER simply assume that the data your code is using is what you think it is. ALWAYS check for yourself by debugging and confirming.
 
@jmcilhinney I suffer from the fact that I'm only a limited part-time developer for my own masochistic endeavors.

I had breakpoints while debugging, but don't use visual studio enough to keep an eye on the LOCALS window. So I was mostly just hovering over variables while debugging, which won't show you the data type.

On the bright side, I just discovered the GetType Method. And now I know not to expect integers when pulling values from Excel!

locals.png


check-type.png
 
Sounds like we should introduce you to the Immediate Window too

Debug menu
Windows
Immediate

Write a line of code in it to have it compiled and evaluated in the current context (put your getType call in it, for example)

Be aware that it works a bit funky (logical, but not intuitive) - moving the cursor to a previous line and pressing return duplicates the line again so you can tweak it and run it again
 

Latest posts

Back
Top Bottom