.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem with optional argument

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
ed57gmc
622 Views, 13 Replies

Problem with optional argument

Here is the signature of my method.

        public static void Redefine(this BlockReference blkRef, Transaction trans, string path, Blocks.RedefineAction flag, string blockname = "")
        {
            string text = "";
            if (blockname == "")
            {
                text = blkRef.Name;
            }

If I call it without specifying the last optional argument, I get an invalid input error. If I call it with a string, it works, but if I call it with and empty string "" to get around the first error condition, I still get an invalid argument error. What am I missing? Thanks in advance.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

13 REPLIES 13
Message 2 of 14
kdub_nz
in reply to: ed57gmc

I'd need to see your input Ed

 

This works for me ac2023 vs2022 4.8

 

    public class Commands
    {
        private static Document _doc = AcadApp.DocumentManager.MdiActiveDocument;
        private static Database _db = _doc.Database;
        private static Editor _ed = _doc.Editor;

        [CommandMethod("TEST")]
        public static void Test()
        {
            _ed.WriteMessage("Redefine Test\n");

            Redefine("");
            Redefine("", "oranges");
        }

        public static void Redefine(
                            string path,
                            string blockname = "")
        {
            string textValue = "something";
            //if (string.IsNullOrEmpty(blockname))
            if (blockname == "")
            {
                textValue = "nothing";
            }
            _ed.WriteMessage($"{textValue}\n");
        }
    }

I thought initially the issue may have been with the equality conditional, but it seems OK

 

Regards,


// Called Kerry in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect.
Sometimes the question is more important than the answer.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 3 of 14
ed57gmc
in reply to: kdub_nz

Well here are the scenarios:

// returns error
oldBlk.Redefine(trans, sTitleBlock, Blocks.RedefineAction.PreserveAttValues);

// returns erro
oldBlk.Redefine(trans, sTitleBlock, Blocks.RedefineAction.PreserveAttValues, "");

// No error
oldBlk.Redefine(trans, sTitleBlock, Blocks.RedefineAction.PreserveAttValues, "test");

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 4 of 14
kdub_nz
in reply to: ed57gmc

Ed, 

Would you happen to also have an overloaded version of that Method ??

 


// Called Kerry in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect.
Sometimes the question is more important than the answer.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 5 of 14
kdub_nz
in reply to: kdub_nz

FWIW :

_kdub_0-1668713213127.png

 


// Called Kerry in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect.
Sometimes the question is more important than the answer.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 6 of 14
ed57gmc
in reply to: kdub_nz

No, I only have just the one method definition. I thought about using named parameters, but I thought it should work without it.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 7 of 14
ed57gmc
in reply to: ed57gmc

I tried the following and it still caused an error. Scratching my head. 😟

oldBlk.Redefine(trans: trans, path: sTitleBlock, flag: Blocks.RedefineAction.PreserveAttValues, blockname: "");

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 8 of 14
ed57gmc
in reply to: ed57gmc

Below is what the signature looks like in the utility dll, but when I compile it, the referenced dll looks like the first post.

 

        public static void Redefine(this BlockReference blkRef,
                                    Transaction trans,
                                    string path,
                                    Blocks.RedefineAction flag,
                                    [Optional] [DefaultParameterValue("")] string blockname)
        {

            // store correct name
            string newName = "";
            if (blockname == "")
            {
                //use filename as blockname
                //assume path is .dwg
                newName =  Path.GetFileName(path);
                newName = newName.Remove(newName.Length - 4);
            }

 I modified this definition to get rid of the attribute tags and just assign a default value with (string blockname = ""), but this too still fails with invalid input error. The only thing I can see that I'm doing different from you is that this extension method is defined in a utility dll that is referenced into the solution.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 9 of 14
norman.yuan
in reply to: ed57gmc

with "string" type, null is not equal to "" (empty string). So, if you call the method with a string argument as optional and not passing anything, then that argument is null, not "". Now you can see why "if (blockname == "")' would raise error, because variable blockname is null: you cannot compare null to anything other than null.

 

The correct code should be

 

if (!string.IsNullOrEmpty(blockname)

{

    ...

}

Norman Yuan

Drive CAD With Code

EESignature

Message 10 of 14
ed57gmc
in reply to: norman.yuan

Ok, I changed the logic of the if statement, but during debug, I can't even step into the redefine method. I still get the invalid input error. It never makes it to the logic test.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 11 of 14
ed57gmc
in reply to: ed57gmc

I'm still fighting with this optional argument. I thought it might be a problem with assigning the default value to an empty string, so I changed the default to null, but this errors too.

//calling sub
oldBlk.Redefine(trans: trans, path: sTitleBlock, flag: Blocks.RedefineAction.PreserveAttValues);

//method signature
       public static void Redefine(this BlockReference blkRef, Transaction trans, string path, Blocks.RedefineAction flag, string blockname = null)
        {
            string text = "";
            if (string.IsNullOrEmpty(blockname))
            {
                text = Path.GetFileName(path);
                text = text.Remove(text.Length - 4);
            }

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 12 of 14
_gile
in reply to: ed57gmc

Hi,

 

You should give more informations because there's nothing wrong in the extract of code you show.

What's the error?

Which expression causes this error?

 

Here's a little testing example which focus on the optional string argument:

        [CommandMethod("CMD1")]
        public static void Cmd1()
        {
            OptionnalArgumentTest(@"B:\Desktop\Example.dwg");
        }

        [CommandMethod("CMD2")]
        public static void Cmd2()
        {
            OptionnalArgumentTest(@"B:\Desktop\Example.dwg", "SomeBlockName");
        }

        static void OptionnalArgumentTest(string path, string blockName = null)
        {
            string text = string.IsNullOrEmpty(blockName) ?
                Path.GetFileNameWithoutExtension(path) :
                blockName;
            Application.ShowAlertDialog(text);
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 13 of 14
ed57gmc
in reply to: _gile

It was a stupid simple omission. I left out the 'else' keyword and newName was getting reset to "". 😧

        public static void Redefine(this BlockReference blkRef,
                                    Transaction trans,
                                    string path,
                                    Blocks.RedefineAction flag,
                                    string blockname = null)
        {

            // store correct name
            string newName = "";
            if (string.IsNullOrEmpty(blockname))
            {
                //use filename as blockname
                //assume path is .dwg
                newName =  Path.GetFileNameWithoutExtension(path);
            }
            else //<<missing
            {
                newName = blockname;
            }

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 14 of 14
kdub_nz
in reply to: ed57gmc

Ed,
I'm stumped too. 
For me on  ac2023, vs2022, 4.8,

 

 

//
public static void OptionTest(
                    string path,
                    string blockname = null)
{
    string textValue = (string.IsNullOrEmpty(blockname)) ? "nothing" : "something";

    _ed.WriteMessage($"\n{textValue}\n");
}

//-----------
// Test statements
    _ed.WriteMessage("Optional Parameter Test\n");

    OptionTest("YellowBrickRoad");
    OptionTest("YellowBrickRoad", "oranges");
    OptionTest("YellowBrickRoad", "");
    OptionTest("YellowBrickRoad", null);

 

 

Result :

_kdub_0-1669055834666.png

_kdub_1-1669056302967.png

 

What happens if you take something like this to a console app ??

 

 

added:
ahhh, it was resolved while I was composing this . . .  must learn to type faster 🙂


// Called Kerry in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect.
Sometimes the question is more important than the answer.

class keyThumper<T> : Lazy<T>;      another  Swamper

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report