
I would avoid nested classes whenever I can, but that's just my personal opinion.





This is the third time you have asked what is basically the same question. Nested classes is not the answer, as you obviously don't understand what they are. Take Pete O'Hanlon's advice and go and work through Charles Petzold's free book. You cannot learn how to write good code by repeating the same question in a forum like this.
Veni, vidi, abiit domum





I don't know where I made mistake.I checked everything !! but this error happened :
"Index was out of range. Must be nonnegative and less than the size of the collection. Parameter name: index"
thank you in advance.
int i = 1;
int RowNo = 0;
int CellNo = 0;
foreach (DataGridViewRow row in Firstdgv.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (i > Finaldgv.Columns.Count)
break;
Finaldgv.Rows[RowNo].Cells[CellNo].Value = cell.Value;
i++;
CellNo++;
}
RowNo++;
i = 1;
CellNo = 0;
}





At a guess  and that's about it it can be from that little code  you haven't allocated any rows or columns in the destination DGV, or you are trying to access different sized DGVs.
So when you try to access it's Rows collection via an index:
Finaldgv.Rows[RowNo] Or the resulting DataGridViewRow Cells collection:
Finaldgv.Rows[RowNo].Cells[CellNo] It complains becasue it doesn't exist.
Start this way: put a breakpoint on the if line, and single step through from there  look at the variables as you go, and look for null values. (It may help to "break out" the line assigning the row so that each part to the left of a "." is a separate variable  you don't need to, but it makes it easier to see without complex use of the debugger)
DataGridViewRow row = Finaldgv.Rows[RowNo];
DateGridViewCell cell = row.Cells[CellNo]; And so forth.
PS: Keep an eye on the value of i ...
This message is manufactured from fully recyclable noughts and ones. To recycle this message, please separate into two tidy piles, and take them to your nearest local recycling centre.
Please note that in some areas noughts are always replaced with zeros by law, and many facilities cannot recycle zeroes  in this case, please bury them in your back garden and water frequently.





First, the value of i isn't doing anything according to your code, you should be able to remove it and use CellNo instead. Personally I'd move the declaration of CellNo inside the first foreach loop, that way you can also remove the line CellNo = 0; at the end.
int RowNo = 0;
foreach (DataGridViewRow row in Firstdgv.Rows)
{
int CellNo = 0;
foreach (DataGridViewCell cell in row.Cells)
{
if (CellNo >= Finaldgv.Columns.Count)
break;
Finaldgv.Rows[RowNo].Cells[CellNo].Value = cell.Value;
CellNo++;
}
RowNo++;
}
If this doesn't fix your problem (I doubt it will) you need to figure out which of the two indexes is wrong  you can put a breakpoint on, or you could temporarily amend Finaldgv.Rows[RowNo].Cells[CellNo].Value = cell.Value; to:
var tempRow = Finaldgv.Rows[RowNo]
var tempCell = tempRow.Cells[CellNo]
tempCell.Value = cell.Value
And see which line breaks  my initial gut reaction is you have no rows.





my dear friend ,at first I am so appriciate for your attention.
I revised the code as you said but the error still exist !!
the error happens in the "***" line
when the RowNo=1 error occurs !!
int RowNo = 0;
foreach (DataGridViewRow row in Firstdgv.Rows)
{
int CellNo = 0;
foreach (DataGridViewCell cell in row.Cells)
{
if (CellNo >= Finaldgv.Columns.Count)
break;
var tempRow = Finaldgv.Rows[RowNo]; ***
var tempCell = tempRow.Cells[CellNo];
tempCell.Value = cell.Value;
CellNo++;
}
RowNo++;
}





When you put a break point on var tempRow..., and inspect the value, what do you find? The thing that stands out to me here is that you have an assumption that Finaldgv has more than one row, and there's no guarantee that this is the case.





I want to copy Firstdgv rows to Finaldgv rows.So how can I refer to the second,third and... rows in Finaldgv in order to copy the second ,third and .... Firstdgv rows .
I think your assumption is right.but what should I do?





If you are trying to copy the values from one grid into another (and I'm assuming the second one is empty), then you Add the rows into the other grid.





thank you my friend it works
thank you ALL





You're welcome. I'm glad I could be of service.





<pre lang="css">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Building
{
int[] list1 = new int[3] { 0, 91, 57 };
int[] list2 = new int[3] { 1, 67, 24 };
int[] list3 = new int[3] { 2, 27, 27 };
int[] list4 = new int[3] { 3, 97, 56 };
int[] list5 = new int[3] { 4, 25, 19 };
int[] list6 = new int[3] { 5, 95, 18 };
int[] list7 = new int[3] { 6, 5, 69 };
int[] list8 = new int[3] { 7, 46, 94 };
int[] list9 = new int[3] { 8, 29, 68 };
int[] list10 = new int[3] { 9, 92, 28 };
for (int i = 0; i < 4; i++)
{
int numberIn = list1[i];
System.Console.WriteLine(numberIn);
}
}
}
I wish to modify this class to allow me to read a csv file in and present an array of arrays that can be returned to a 'stem' class. What is the simplist code that will achieve this please?





Seriously, you could solve this part with a simple Google search. You really need to learn how to do that part. I'll give you a couple of hints here:
Your data is in a File. You want to Read All Lines from that file.
Once the data is in lines of strings, you want to Split each string into it's constituent parts.
It's that simple.





csv stands for comma separated values . The lines below your comment do not show any comma between the values, rather spaces. If all of your values are simple numbers and separated by spaces, just read the file line by line and get the contents with string[] contents = line.Split(' '); , then parse each item of contents into an int (int.Parse(...) or int.TryParse(...) ).





I am looking for my C# class to firstly work out distance between two coordinate points on the x plane and then work out the distance between the same coordinated points on the y plane.
There are potentially several ways of doing this. I was thinking:
Firstly calculate distance on the x plane (x1>x2):
in Excel form:
if (x2 > x1, x2  x1, x1  x2)
(then) (else)
Then calculate the distance for the y plane (y1>y2):
if (x2 > x1, x2  x1, x1  x2)
if we then think of a triangle (with right angle) we have
A(squared) + B(squared) = C(squared)
We would have the length A and B and therefore would be able to calculate C.
Is there an easy way of doing this in C# or is it best to code out the equations?
Any advise appreciated.





Feels like you need to find out the distance between 2 points on a 2D plane.
Point A (x1,y1)
Point B (x2,y2)
Distance between them would be=[((x1x2)^2)+((y1y2)^2)]^(1/2)
Use Math.Abs method to get the absolute value of the distance regardless of x1>x2 or x1





This looks like a good approach. I am really new to C# (after 10 years away), do you have a coded example?





Its basic maths buddy..
Let me try to write a simple app for you.
Distance between points (5,5) and the origin (0,0) should be:
public CalculateDistance()
{
double D;
int x1=5;
int x2=0;
int y1=5;
int y2=0;
D=Math.Abs(((x1x2)^2)+((y1y2)^2))^(1/2);
Console.WriteLine(D);
Console.ReadKey();
}
The above should do good.
Anurag





Your question states that what you want is the size of the distance between two x coordinates, and the size of the distance between two y coordinates, but your code suggests that what you really want is the length of a line between the two points: which is the hypotenuse of a right triangle.
Assuming it's obvious to you how to get the distances on x, and y, and since you already know how to calculate a hypotenuse, then the answer to your question is: no, C# does not contain a builtin function to calculate a hypotenuse. Yes, you have to code it.
And, the real answer to your question is that you need to focus on mastery of the basic mathematics operators in C#, and you need to examine the functions available in the Math library, and get familiar with what it offers, and what it does not offer.
A small effort on your part now to learn the basic mathematics operators, and commonly used functions in the Math library, will enable you to express any equation.
It's a good idea to think carefully about what you are asking, before you post a question.
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview





I am new to C#. I am not sure whether I am approaching my classes in the right way. I want my main class to act as a stem that activates other classes that performs various tasks like retrieving information or writing to file or performing calculation. I am not sure how to interrelate the classes. I know that some classes have to be public etc.
I was thinking that this stem class would take on the form:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Stem
{
static void Main(string[] args)
{
}
}
}
Firstly I want to know if I am approaching the problem in the right way.
Secondly I would like bits of code that would help me build this stem (if poss).
I would appreciate any help given.





Hmmm, I think you need to step back here as it sounds like you're trying to run before you understand the basics of building C# applications. Before you try building your own application, I'd recommend downloading and reading .NET Book Zero[^] by Charles Petzold. It's free, and the samples should help to teach you what you need to know.





I can see that I need to modify my approach. What approach to this would you take? Would you suggest that all the tasks that are on the list act as superclasses of the class that takes the main argument?





You're asking for a design when the problem isn't clear. That's why I suggest you start by reading the book.





I'm really short of time on this task.
The task is to take a csv file.
Process a list of x,y specified coordinate points.
Work out the distance between all these coordinate points.
Print results to file





Then, realistically speaking, you're stuffed. Without a basic understanding of the language, you aren't going to be able to write something that fulfils the criteria you laid out. Theoretically, you could do all of this procedurally in the Main method, but as that violates what I'm assuming the test is about, you would fail. At a high level, I would expect that they are looking at using an architecture similar to this:
A class that encapsulates a point (perhaps you could call it Point).
A class that, given a CSV file, would read it in and return a list of these points
Where it gets more subjective is, do you put the calculation of the distance into the point class, or is it in the class that contains the points? I'll leave you to decide that one.
What I wouldn't be worrying about here is superclass x or subclass y. That's irrelevant for a task like this.



