Click here to Skip to main content
15,902,832 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
I want to make a variable of type double as:

C#
double[,] tempData = new double[5000,2];


but I don't want to define the size as 5000.
I want the size (i.e. 5000) to be defined by the number of lines which are read from external sources (e.g a *.csv file). I don't want to use the Lists!
Can anybody help?

regards
AM
Posted
Updated 15-Jul-15 4:38am
v2
Comments
PIEBALDconsult 15-Jul-15 10:45am    
"I don't want to use the Lists!"
Why not? They seem better suited to the requirement.
Wendelius 15-Jul-15 10:47am    
Agree, what's the point of not using ready made functionality?
PIEBALDconsult 15-Jul-15 10:49am    
I actually suspect it's a matter of homework and the teacher wants them to try it both ways to see the power of Lists.
ARMS_DEIR 15-Jul-15 10:55am    
Assuming that you are suspecting that it is a homework, isn't it worth to come here and see different ideas. Unfortunately you are wrong andd it is not a homework. I am a mechanical engineer that just started to programming. That is all. and it made me frustrated when I asked a question somebody comes and directly say it is .... may be better to google it or so on, If I have the answer I wouldnt be here!

Better to ask a question and appear stupid now, than not ask a question and remain stupid forever.
If you give a man a fish, you feed him for a day. If you teach him how to fish, you feed him for a lifetime.
Sergey Alexandrovich Kryukov 15-Jul-15 11:17am    
Please understand: all your "I want" should be reasonably motivated. Perhaps you want to know why? This is simple: people want to help you, and not to waste time on someone who won't use the advice anyway.

And please set aside your "frustration" and other emotions, if you want to be taken as an engineer and not as a child. It you think that something or someone makes you look stupid, think again: maybe it's just your fantasy. If your are afraid of looking stupid, this is only your problem. No one blames you for asking wrong questions, but be ready to some comments which may not praise you. What's wrong with that?

When people advise to Google, they are trying to teach you how to fish, and when they ask you immediately, it might be just giving a fish.

Now, back to business. Why do you want to avoid List<>? It seems to be the most reasonable approach, unless you need more advanced type of collection.

—SA

Well, read the CSV file, identify how many lines there are, and allocate that number of array elements. Then process the CSV data into your array.

If you don't want to use a "flexible" array like List<T> (and it is at heart an array that is copied when it expands) then you either need to construct a linked list of some description, or know in advance how many items to allocate.
 
Share this answer
 
Comments
ARMS_DEIR 16-Jul-15 5:01am    
just have tried to read the file first and put it in a tempdata, and then count the read lines, then I put the rows of tempdata which are indiced by count and modify my main variables. seems basic but works!!
OriginalGriff 16-Jul-15 5:18am    
Glad to hear it!
I'd go with a List myself! :laugh:
In Array.Resize<t> Method[^] documentation there's a proposal for resizing two-dimensional arrays. Perhaps that would help you.

Depending on the situation I'd still consider going to lists (or similar) especially if you think you need more complex handling in the future, like removing items from the middle, query operations and so on...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Jul-15 12:07pm    
This method is as inefficient as creation of a new array each time. The really rare case when a reference type is passed by reference indicates that the referential identity of the source array is lost at the call, so a brand new array is created. Practical applicability of this method is extremely limited and certainly unsuitable to the case in question.

The inquirer simply has no valid objections against System.Collections.Generic.List<>. This is the only reasonable approach, unless more advanced collection type is needed. As you see from the comments to the question, the objection is simply based on false idea that using the list appears to be to difficult, by some mysterious reason. This is just the misconception which also maybe demonstrating wrong approach to engineering in general. The only effective help could be pointing out this misconception.

—SA
Wendelius 15-Jul-15 12:55pm    
Yes, the method in documentation is quite.. peculiar and is basically based on creating always a new array. That's why be both agree on using list :)
int numLines;
//...set numLines...
double[,] tempData = new double[numLines,2];
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900