473,385 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Arrays and properties in a class

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
Nov 15 '05 #1
2 1330
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][b], would be looking at...

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

Thanks again,

Ann-Marie
Nov 15 '05 #2
A Ratcliffe <ar********@archiNOSPAMmagic.net> wrote:
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][b], would be looking at... The value of Point[b] at index [a] in Points, or vice-versa?


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
6
by: Luke | last post by:
Here is my emails to Danny Goodman (but probably he is very busy so he didn't answered it). First email(simple): Subject: JavaScript Arrays " We all know the array can act like HashMap, but is...
3
by: alan | last post by:
I wonder if someone could help me. I've got a 2 Dimension Array that I'm working with and want to display the results from this array in a datagrid. I'm quite new to C# and seem unable to link...
2
by: David | last post by:
Hi all, I am fairly new to C#. so go easy on me :-) Anyhow, I have a class file that I have set up properties and a method. I am calling this class file directly from and aspx.cs file. So...
0
by: hazz | last post by:
i have an array (or collection if necessary) of Customers with CustomerID and several properties. For each of these Customers I will have a one to many relationship with Customer products....
1
by: Ben | last post by:
Hi There I am learning about reflection so apologies if this post is vague ... I am doing some unit testing and have written a function that accepts two objects and tests to see if their...
7
by: steve bull | last post by:
If I create an array, e.g. private int x, inside a parent class whose elements can be accessed by a child class - what is the recommended way of doing this? When I create a property for the array...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
0
by: PeterW | last post by:
I have a class which uses three other classes, two of which are used in arrays. This class is only ever used for incoming messages. All classes inherit from ISerializable and impliment...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.