Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2005

Tip: Looking for answers? Try searching our database.

Number of elements is an array

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter Oliphant - 27 Oct 2005 23:39 GMT
Say I have a line in my code something like the following (NOTE: Point is
__value class System::Drawing::Point):

Point point[] = new Point[3] ;

How do I now 'ask' 'point' how many elements it has (in this case the answer
is 3)?

This does NOT work:

int element_count = sizeof(point) ; // error C3608: can't apply sizeof to a
__gc array

Thanks in advance!!! : )

[==P ==]
Tom Serface - 28 Oct 2005 00:13 GMT
Of course, an obvious solution is to do something like:

#define POINT_ARRAY_SZ 3

Point point[] = new Point[POINT_ARRAY_SZ];

Then you could use it anywhere you want.

Tom

> Say I have a line in my code something like the following (NOTE: Point is
> __value class System::Drawing::Point):
[quoted text clipped - 12 lines]
>
> [==P ==]
Peter Oliphant - 28 Oct 2005 15:23 GMT
> #define POINT_ARRAY_SZ 3
>
> Point point[] = new Point[POINT_ARRAY_SZ];
>
> Then you could use it anywhere you want.

Hi Tom,

I see what you're saying, but what I need is the ability to find out the
number of elements in an *arbitrary* array. Put another way, when the number
of elements in the array is defined at runtime and is a variable.

This is exactly what I want to be able to do:

int Array_Size( Point point[] )
{
       return number_of_elements_in( point ) ;
}

To put this in context, I'm using the Graphics DrawPolygon method. Here it
takes as a parameter Point[], the list of the vertices of the polygon. But
the function does not take the number of elements in Point[] as a parameter,
hence DrawPoygon() must be able to figure out the number of elements in
Point[] just from Point[] (rather, an instance of). I need to be able to do
the same since I must process these points before I give them to
DrawPolygon, so of course I need to know the number of elements in Point[]
as well from the same info (i.e., I'm not given the Point array size in
number of elements).

> Of course, an obvious solution is to do something like:
>
[quoted text clipped - 22 lines]
>>
>> [==P ==]
Tom Serface - 28 Oct 2005 22:37 GMT
Sorry Peter, didn't understand your requirement. Length would be the way to
go in this case.  I thought you were using a generic example.

Tom

>> #define POINT_ARRAY_SZ 3
>>
[quoted text clipped - 3 lines]
>
> Hi Tom,
Peter Oliphant - 28 Oct 2005 15:36 GMT
My apologies, I was using it wrong. point->Length DOES work. Thanks! : )

[==P=]]

> Of course, an obvious solution is to do something like:
>
[quoted text clipped - 22 lines]
>>
>> [==P ==]
James Park - 28 Oct 2005 00:56 GMT
point->Length

> Say I have a line in my code something like the following (NOTE: Point is
> __value class System::Drawing::Point):
[quoted text clipped - 12 lines]
>
> [==P ==]
Peter Oliphant - 28 Oct 2005 15:26 GMT
Hi James,

Unfortunately points->Length doesn't work, but instead generates the error:

C2039: 'Length' is not a member of 'System::Drawing::Point'.

And I've tried 'length', 'size', and 'Size' as well, with no luck.

There HAS to be  way to get the number of elements in a Point[] array, but
what is it?

[==P==]

> point->Length
>
[quoted text clipped - 14 lines]
>>
>> [==P ==]
Peter Oliphant - 28 Oct 2005 17:15 GMT
My apologies, I was using it wrong. point->Length DOES work. Thanks!  : )

[==P=]

> Hi James,
>
[quoted text clipped - 28 lines]
>>>
>>> [==P ==]
Peter Oliphant - 28 Oct 2005 17:57 GMT
Ok, got another question. How do I return a Point[] (pointer) from a method?
The following won't work:

Point[] Get_Points() { return m_Points ; } // error: Point[] is not a valid
syntax here

where m_Points is defined by:

Point m_Points[] ;

I CAN return a pointer to the first element of the list, but I'm guessing
this doesn't retain the array info (such as 'Length'....hehe). That is, I
CAN do the following:

Point* Get_Points() {  return &(m_Points[0]) ; }

Is there a way to return a pointer to an array of Point's? Will the above do
the trick and preserve its interpretation of the return value as a pointer
to an array, not just a single element (which I doubt, but it might know
from context)?

Thanks (again) in advance!  : )

[==P==]

> point->Length
>
[quoted text clipped - 14 lines]
>>
>> [==P ==]
Arnaud Debaene - 28 Oct 2005 18:36 GMT
> Ok, got another question. How do I return a Point[] (pointer) from a
> method? The following won't work:
>
> Point[] Get_Points() { return m_Points ; } // error: Point[] is not a
> valid syntax here

Point Get_Points() []
{ return m_Points ; }

Geee, *why* oh why did they choose to keep this awfull K&R syntax ;-(

Arnaud
MVP - VC
Peter Oliphant - 28 Oct 2005 20:22 GMT
> Point Get_Points() []
> { return m_Points ; }

I never in a million years would have tried that syntax...but it definitely
works...Thanks!!! : )

[==P==]

>> Ok, got another question. How do I return a Point[] (pointer) from a
>> method? The following won't work:
[quoted text clipped - 9 lines]
> Arnaud
> MVP - VC
Arnaud Debaene - 29 Oct 2005 09:10 GMT
>> Point Get_Points() []
>> { return m_Points ; }
>
> I never in a million years would have tried that syntax...

Well, RTFM ;-)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspec/html/v
cManagedExtensionsSpec_4_5_2.asp


Arnaud
MVP - VC

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.