473,385 Members | 1,736 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.

best way/how to....?

Hi,

I've come across this situation a couple of times now and there must be a
better way of doing this.

Situation:

I need several values grouped together and then I need several of them. I
don't need to do any
thing much with the values so a struct would be better than a class
(performance/resource savings?).
i.e.
public struct Example {
double x, y, z;

public Example (double a, double b, double c) {
x = a;
y = b;
z = (y - x) / c;
}

// Properties for x, y, z......
}

Then in the code to use this I need to process a bunch of data that will
form these structs.
However say I process line 1 of data, I don't know if it forms all of 1
struct Example, so I have to read the next line of data. At this point I
find I have got enough data so I create a struct Example and add it to an
array. NOTE: I am not actually reading in lines of data I actually have an
arraylist of custom objects (of the same type) and I use some of their
properties, I'm just simplifying my case.

At present because I don't know how many struct Example's I will end up
creating I use an ArrayList.
When I am finished the method does: return
(Example[])myArrayList.ToArray(typeof(Example));
However here lies the problem the conversion causes all the structs to lose
there values.
I get around this by declaring as class Example instead of struct Example,
but using a class seems like a bit of overkill.

Can anybody think of a way I can create an array of structs where I don't
know how big the array needs to be when I declare the array.

Regards,
Peter
Nov 16 '05 #1
1 1044
Peter Row <fuck.off@bastard_spammers.com> wrote:
I've come across this situation a couple of times now and there must be a
better way of doing this.

Situation:

I need several values grouped together and then I need several of
them. I don't need to do any thing much with the values so a struct
would be better than a class (performance/resource savings?).
The difference between a struct and a class is nothing to do with
whether or not you want to do things with them. The difference is
between a value type and a reference type - you should work out what
kind of semantics you want and use the appropriate type.

<snip>
At present because I don't know how many struct Example's I will end up
creating I use an ArrayList.
When I am finished the method does: return
(Example[])myArrayList.ToArray(typeof(Example));
However here lies the problem the conversion causes all the structs to lose
there values.
It really shouldn't - and a quick test program doesn't show that
behaviour.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
I get around this by declaring as class Example instead of struct Example,
but using a class seems like a bit of overkill.


Why?

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

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

Similar topics

15
by: John J | last post by:
I've written the following code into a class to search for and display the results of all races entered (The complete code is in a previous thread). I wish to amend the code so as to display the...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
3
by: Irene | last post by:
Hi all, I have set up a simple VB program (and later on an ASP interface) to manage an Athletics database. I'm using Access 2000. To simplify, I have the Athlets, the Competitions and the...
5
by: l.woods | last post by:
I want your recommendation on which ASP.NET Shopping Cart software I should buy? Best code Best documentation Best support (if needed. I will buying source code, if possible) TIA, Larry...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
10
by: Ren | last post by:
Hi All, I'm still rather new at vb.net and would like to know the proper way to access private varibables in a class. Do I access the variable directly or do I use the public property? ...
4
by: Ned Balzer | last post by:
Hi all, I am pretty new to asp.net; I've done lots of classic asp, but am just beginning to get my mind wrapped around .net. What I'd like to do is include some code that tests if a user is...
24
by: Earl | last post by:
I have all of my data operations in a separate library, so I'm looking for what might be termed "best practices" on a return type from those classes. For example, let's say I send an update from...
11
by: ankitmathur | last post by:
Hi, I'm trying to overcome a situation whereby I have to search through 4-5 columns and produce the results with an order by according to the values matched in these columns. Example: My...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.