Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations SSS148 on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does Annotations.Count return a value in some cases, but not others?

Status
Not open for further replies.

solid7

Mechanical
Jun 7, 2005
1,403
In the following code, I have annotated what works, and what doesn't. The code worked fine, until I put the handling in, to catch the cases where there are no annotations.

Code:
Annotations oAnnotations = (Annotations)oAnnotationSet.Annotations;

MessageBox.Show("annotation count = " + oAnnotations.Count); [b]<== Does NOT work here - throws error[/b]

if (oAnnotations.Count != 0)
{
     for (int i = 1; i <= oAnnotations.Count; i++)  [b]<== Works here - no error, expected result[/b]
     {
          Annotation oAnnotation = (Annotation)oAnnotations.Item2(i);
          oAnnotationsList.Add(oAnnotation);
     }
}

 
Replies continue below

Recommended for you

I do understand that by the documentation, there is no actual method for this. I'm actually relying on the C#/.NET framework to provide the method.

 
Hi.

solid7 said:
I do understand that by the documentation, there is no actual method for this.

I'm pretty sure Count property is documented in Automation docs. AnnotationSets is inherited from Collection that have Count property. Parent classes are displayed at the top of each page of
DSYAutonation.chm

With all of that being said your code throws an error because "plus" operator inside MessageBox.Show() defined for string arguments, not string and integer. The easiest way to fix it is to add .ToString() call
 
Those message boxes work all day long. Point taken, and I'll work on that. But that is not the issue. The message box was an afterthought. A test. I get exactly the same message, with or without it. And everywhere else it's used, the plus is not a problem. Technically, I should be using ampersand. That works, too.

Also... Count method may be inherited from Collection, but it certainly does not work in all types. I don't know if that's because of how they defined the class that overloads it. But I just had a call with Dassault about this last week. Certain collections error out on .Count. Instances was the one that I speak primarily of. I've never been able to count those. In fact, I have to resort to the primitive "foreach" method to do anything with them.

 
What error does your code throw?

UPD
Those message boxes work all day long

Agreed, operator+ is defined for string and compiles into string.Concat()

Technically, I should be using ampersand. That works, too.

That's quite shocking to be honest. What version of .NET and C# do you use? Are you on VSTA?

Also... Count method may be inherited from Collection, but it certainly does not work in all types.

Agreed again, but I totally get why is that.

The thing is that when you implement an interface that inherits Collection you have to implement Count method by yourself as there's no base implementation provided by DS.
Also there are cases when entities can only be counted by discovering them all, which can be very costly timewise. In fact I find that in most cases for..each does a better job both logically and perfomance-wise.

In fact, even in the code you posted Count is called (N + 2) times where N is the number of items in oAnnotations, and that is inefficient. What you could do is to (if applicable):

a) invert iteration order: for (int i = oAnnotations.Count; i > 0; --i)
b) use for..each loop or even LINQ extension methods that would call it for you internally: oAnnotationsList.AddRange(oAnnotations.Cast<Annotation>())
 
I don't fully understand all of the LINQ functionality, but I'll look it up. I use it quite extensively, when it's just data. (amazing tool set) But I have also had some problems with LINQ, as there is INFITF reference for both Catia and Visual Basic, and I haven't fully figured out when and how I can cross lines.

I'm using VS 2013 with C# 7.0 and VSTA 2013

 
UPDATE: I have been in meetings, and I forgot to update this. I was looking for full understanding: but I got around the issue with a simple "try" and "catch" statement. I sometimes I forget that I can do that...

Code:
try
{
     if (oAnnotations.Count != 0)
     {
          for (int i = 1; i <= oAnnotations.Count; i++)
          {
               Annotation oAnnotation = (Annotation)oAnnotations.Item2(i);
               oAnnotationsList.Add(oAnnotation);
          }
     }
               else { MessageBox.Show(oNoText); }
}
catch
{
     MessageBox.Show("There are no annotations in the model");
}


 
So I don't understand why one instance of .Count works, and the other doesn't. But I do understand that it occurs.

 
So I don't understand why one instance of .Count works, and the other doesn't.

Post your full code, please. I believe there could be something with the way your original fragment of code is called.
Did you run it with the exact same inputs?

Technically, I should be using ampersand. That works, too.
I'm using VS 2013 with C# 7.0 and VSTA 2013

Ampersand never worked that way in C#. Ampersand operator denotes bitwise and, not string concatenation.
And since "That works, too" it's possible that the whole fragment just doesn't get called at all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor