Arrays and properties in a class

A

A Ratcliffe

Here's where I show my C++ roots, and the fact that I am still learning C#,
but...

In C++, if I had a small array in a class, I would simply define it in the
class to the correct dimensions. If I wished to access the array via
operator, I would overload the [] operator for the class. For example...

class cPoint3
{
private:
float _v[3];

public:
float& operator [](int i);
}

In C#, this all changes. On to my questions:-

A) In C#, as I understand it, I have to define the array as [], as in

private float [] _v;

and then set its size in the constructor, as

_v = new float[3]

Is this right? it seems a long winded way of setting something that I KNOW
is always going to be that size.

B)
How do I create the equivalent overloaded [] for the class in C#?

Sorry for bothering you all with such simplicity, but I was expecting
something closer to the C++ way of doing things, and it threw me into a spin
when VS.NET started highlighting everything as incorrect syntax.

Thanks in advance,

Yours,

Ann-Marie Ratcliffe
 
A

A Ratcliffe

As an addendum...

Once I had created the overloaded [] for my C# class, if I then had an array
Points[], if I referred to Points[a], would be looking at...

The value of Point at index [a] in Points, or vice-versa?

Thanks again,

Ann-Marie
 
J

Jon Skeet [C# MVP]

A Ratcliffe said:
Here's where I show my C++ roots, and the fact that I am still learning C#,
but...

In C++, if I had a small array in a class, I would simply define it in the
class to the correct dimensions. If I wished to access the array via
operator, I would overload the [] operator for the class. For example...

class cPoint3
{
private:
float _v[3];

public:
float& operator [](int i);
}

In C#, this all changes. On to my questions:-

A) In C#, as I understand it, I have to define the array as [], as in

private float [] _v;

and then set its size in the constructor, as

_v = new float[3]

You don't need to do it in the constructor - you can do it along with
the declaration:

float[] _v = new float[3];

(Note that private is the default access for instance/static variables.
I omit it.)
B)
How do I create the equivalent overloaded [] for the class in C#?

You need an indexer, eg:

public string this [int x]
{
get
{
return someStringArray[x];
}

set
{
someStringArray[x]=value;
}
}

(Potentially with more bounds checking etc.)

Look up indexer in the MSDN for more information.
Sorry for bothering you all with such simplicity, but I was expecting
something closer to the C++ way of doing things, and it threw me into a spin
when VS.NET started highlighting everything as incorrect syntax.

No problem at all :)
As an addendum...
Once I had created the overloaded [] for my C# class, if I then had an array
Points[], if I referred to Points[a], would be looking at...

The value of Point at index [a] in Points, or vice-versa?


it would be the equivalent of (Points[a]) - that's the way to think
about it. Hope that helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top