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.
Solved! Go to Solution.
Solved by ed57gmc. Go to Solution.
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
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,
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
No, I only have just the one method definition. I thought about using named parameters, but I thought it should work without it.
I tried the following and it still caused an error. Scratching my head. 😟
oldBlk.Redefine(trans: trans, path: sTitleBlock, flag: Blocks.RedefineAction.PreserveAttValues, blockname: "");
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.
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
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.
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);
}
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);
}
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,
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 :
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.