|
You use either the DataView you might have assigned to the DataGrid , or get the CurrencyManager and query for the DataRow with that index. Don't simply use the DataSet or DataTable because after sorting the DataGrid those rows our out of sync. An example follows:
CurrencyManager cm = (CurrencyManager)dataGrid1.BindingContext[
dataGrid1.DataSource, dataGrid1.DataMember];
if (cm != null)
{
DataView view = cm.List as DataView;
if (view != null)
{
DataRowView rowView = view[rowIndex];
object value = rowView[0];
if (value != null) return value.ToString();
else return null;
}
} An IndexOutOfRangeException may be thrown in this sample at view[rowIndex] , but that should actually be a desired effect from this code (make sure you handle it, though).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hello everybody!
I have the problem of effective implementation of multidimensional arrays in C#. In my application I'm using arrays with doubles as a fundamental structure. The number of dimensions and their length is known only in runtime, but typical array is approximately 4-10 dimensional with the length 1-15 for each dimension. Currently (because of historical reasons) these "arrays" are implemented as nested ArrayLists. This means there is a root-level ArrayList that contains "references" to the array lists of the second level and so on (the array list of the last level contains the double values).
Correspondly, if I want to get an element with "coordinates" [2, 3, 4, 5] in a 4-dimensional space I need to run through the levels of array list taking the arraylist with index 2 from the root list, from it the array list with index 3, from the last the array list with index 4, and from the very last array-list the double value with index 5. This is certainly quite ineffective, and I tested an alternative with built-in multidimensional array that is created with the following construction:
System.Array tempSpace = Array.CreateInstance(System.Type.GetType("System.Double"), allDimensions);
where allDimensions is a one-dimensional array with ints - dimensíons for my multidimensional space.
Suprisingly, I found only 2-3 times difference in comparison with implementation on array lists (I tested looping through all elements, incrementing all elements, random access. Code can be provided if needed).
I suspect that the performance problem with built-in multidimensional array is mainly occured by boxing/inboxing (I do not know how to create a multidimensional array so that it will not require boxing/inboxing not knowing the number of dimensions in compile-time). Accordingly, I have following questions:
1. Is it possible to create a multidimensional array so that it will not require boxing/inboxing not knowing the number of dimensions in compile-time? (type of elements is known in compile time - double).
2. If the answer on the second question is no, what will be the better implementation for multidimensional array of doubles of my size (4-10 dimensions with the length 1-15 of each dimension):
a) Built-in multidimensional arrays that suffer (supposedly) from boxing/inboxing.
b) Large one-dimension array, and I need to write some code for index managing (calculating the position of element in one-dimensional array that corresponds to the position in the multidimensional array given its indices).
c) Your solution.
So, with my solutions on the one hand I have boxing/unboxing issue, on the other hand - index managing (which also requires some computational resources) and my time for implementing the solution.
I expect the following functionality from the implementation:
1. Fast random access (write and read) basing on given indices characterizing the position in the multidimensional space (indices are not predictable - random).
2. Fast sequential access (write and read) basing on given indices. By "sequential" here I mean that I will start at some position of the space and get all "neighboring" positions sequentially in some dimension (get a slice from the space).
3. The dimensions of the array are quite rigid and basically are not changed after creation of the array. The increase of dimensions/length of dimension is never required, but there can be situations when I will set to zero a whole "slice" (whole dimension), if the amount of zeroed dimensions is high it may be advantegeous to "compress" the array. This is not a "need to be" thing, it will be always possible to realize it making a compressed array and copying elements to it.
4. It will be also not bad to have some method to efficiently set all elements of array to some value (0 or 1, for instance).
I would like to use only managed code. I have already read "Arrays Undocumented" article at http://www.codeproject.com/dotnet/arrays.asp
I would be glad to here any opinions on this topic, in particular answers on my questions. Thank you very much for your help.
Andrew Kuklin.
|
|
|
|
|
OT: You may want to be a little careful with the usage of language in a help forum, using wording like "I expect" sounds far too much like "I want this done for me", not as it should sound "I would like some help with this please" - the sentiment expressed in the last line of your post.
Anyhoo, It sounds to me like the functionality you are after is closer to a Binary tree than it is to a multidimensional array, maybe its worth you googling that and having a look.
Failing that would continuing with a M.D. ArrayList format encapsulated into an easy-to-use object not fit the bill?
|
|
|
|
|
Hello, J4amieC.
Probably, I've formulated what I wanted to say in a very inadequate way, by "I expect the following functionality from the implementation:" I meant "I would like to use the result solution for following purposes:". I do not want anyone to write code for me, I would like to hear some thoughts of people that are more experienced in this topic as I am.
Concerning your solution: at first, thank you for your help. Secondly, I'm afraid that I don't quite understand what you're proposing. I know what a binary tree is, what kind of data do you suggest to store in the nodes of the tree? Besides, I think it will be better to store minimum data for access to array (in your example with binary tree, I will need some kind of "links" to the children of parent node). I would like to give some estimation of typical "spaces" that are currently used. For example, a typical 10-dimensional array with following dimensions: {4, 7, 8, 6, 8, 2, 6, 7, 2, 7}. This will result (let's take 8 byte pro double-value) in
(4*7*8*6*8*2*6*7*2*7)*8 bytes=101154816 bytes=96,46875 Mb. And that is already quite big. I can have up to 10-20 of those "spaces". I just cannot throw away another 96 Mb for control structures. And of course Arraylist-based solution will not save the situation because of memory- and time overhead connected with boxing/unboxing.
Here is also the code with which I tested the performance. I've commented it as much as possible, but if anything is not clear, please, do not hesitate to ask:
//Create a test 10-dimensional array
int[] allDimensions = new int[10]{4, 7, 8, 6, 8, 2, 6, 7, 2, 7};
System.Array testArray = Array.CreateInstance(System.Type.GetType("System.Double"), allDimensions);
//currentDimensions is an array representing "counters" of dimensions
//I'll explain this on example. If as tempTable we have a three-dimensional array
//with allDimensions=[4, 8, 2].
//Then currentDimensions will run through the following combinations:
//[0; 0; 0], [1; 0; 0], [2; 0; 0], [3; 0; 0], [0; 1; 0], [1; 1; 0], [2; 1; 0] etc.
int[] currentDimensions = new int[allDimensions.Length];
//done variable checks whether all "places" of the space were visited
//(i.e. with the same example of three-dimensional array bool will be true when
//currentDimensions=[3, 7, 1]
bool done=false;
while(!done)
{
//Increment the current place in the space
double curValue = (double)testArray.GetValue(currentDimensions);
testArray.SetValue(++curValue, currentDimensions);
//Try to get the next combination of currentDimensions
done=true;
for(int j=0; j
|
|
|
|
|
I'm developing my first Managed DirectX app at the minute and although this isn't going to stop me moving on it might change my design ideas. All i want to know is which is quicker/more efficient after it's compiled.
Scenario 1.
public class Ball
{
public void DrawBall
{
}
}
public class Game
{
Ball ball = new Ball();
public void Draw()
{
ball.DrawBall();
}
}
or Scenario 2
public class Ball{}
public class Game
{
Ball ball = new Ball();
public void Draw()
{
}
}
So, which is faster or are they both the same speed?
Both will have the same draw code but scenario 1 does the drawing inside the ball.DrawBall method which is called from the class game.
Scenario 2 does the drawing inside the game class so there isn't the extra function call.
Bear in mind that this is gonna be done over 100 times a frame (potentially) and around 30 frames per second.
Hope someone out there knows.
Kev
|
|
|
|
|
I think this is more of a design structure question than a performance question. Imagine you have the scenario where you're drawing 2 teams of 11 players, a ball and 2 goals, would you want your code to look like this:
public class Game
{
public void Draw()
{
}
}
or this
public class Game
{
private Ball ball;
private Team team1;
private Team team2;
private Gaol goal1;
private Gaol goal2;
public void Draw()
{
ball.Draw();
team1.Draw();
team2.Draw();
goal1.Draw();
gaol2.Draw();
}
}
I dont know which is quicker, but for readability and to save having a very long method/list of methods in the game class I would go with the second option above.
|
|
|
|
|
I know that, i would rather use the second option myself, but the question is all about speed. If there is no speed difference then i will go with the more OO oriented approach because it's easier, but if it's slower then i will have to use the less readable first option.
Kev
|
|
|
|
|
If you are that concerned about speed why not write it in assembler?
Do you want to know more?
Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.
|
|
|
|
|
2 Reasons.
1. I don't know assembler
2. That would defeat the object of me trying to learn Managed DirectX
Kev
|
|
|
|
|
why not loop through it 10^6 times to see which is faster?
there are no facts, only interpretations
|
|
|
|
|
second source is faster than first..
why ?
because in second source, construction operation is automated..
Oguz Bastemur
|
|
|
|
|
Hi,
We currently have a sutuation where we have a C# application and a MFC/C++ application.
To synchronize events between them we have used a Mutex. This is created with the following code:
mutexInterface = new System.Threading.Mutex(false, strMUTEX);
strMUTEX is a constant string used to name the Mutex.
On the MFC side the mutex is constructed with the following code:
m_muxInterfaceMutex(FALSE, strMUTEX);
Everything seems pretty simple so far.
If we start the MFC app first, then the C# one everything runs fine.
However if we run the C# app first the construction of the mutex in the MFC app fails and displays 'A required resource was unavailable'
Does anybody know how we can overcome this situation. We would rather avoid telling the users they have to start one app before the other, but it may come to that.
|
|
|
|
|
sjhart wrote:
We would rather avoid telling the users they have to start one app before the other, but it may come to that.
You could write a 3rd application/tiny script that starts both applications in the desired order you want them to be started.
--
Affordable Windows-based CMS: www.zeta-producer.de/enu
|
|
|
|
|
Do someone know a simple way to hook 2 forms to each other like winamp ?
I have a SDI application with a parent form from which i open child forms.
For example, in winamp there is the main window which is the player, then we can open the playlist.
We can move the playlist and put it where we want, but if we move it near the main window, it automaticaly sticks to it (like a magnet) then after, when we move the player, the playlist follow...
I would like my forms to do this magnet effect.
thanks
|
|
|
|
|
There is obviously no built-in way of doing this but you can always:
- keep track of where your forms are and the sizes of the forms
- respond to events when a form is moved
- calculate whether a form is close enough to "snap" and then move the form...
This posting is provided "AS IS" with no warranties, and confers no rights.
Alex Korchemniy
|
|
|
|
|
Hi,
my problem is, that if I want to change the row by pressing the return key, I found no event which fired.
I have added the DataGridTextBoxColumn to my DataGrid and so I have the possibility to add events on several columns.
Does anybody know a solution?
Sorry, if my English was not perfect, I am from Germany.
Thanx
Steffen
|
|
|
|
|
Because your problem is not very clear to me, I assume that your datagrid's data source is from a dataset.
You can try set an event handler for one of these dataset events.
1. RowChanged
2. RowChanging
3. ColumnChanged
4. ColumnChanging
For the changing event, it occurs when data is changing. You can cancel or accept the changes in this event.
For the changed event, it occurs when data has been changed successfully.
|
|
|
|
|
I mean with change the row not to change the value in the special cell. I mean that the cursor is after pressing the return key in the row below the row before.
Example:
Rows:
0
1*cursor* -> then pressing return --> event should fire
2
-->
0
1
2*cursor* (one position higher in the row collection)
I hope my English was understandable.
|
|
|
|
|
Hello,
I'm looking for a C# parser that can give me access to a C# program's code inside a running C# program. The parser should not just build an abstract syntax tree but also give a types hierarchy of the types created and used in the program and some further refinements. For example, it would be important to know the type of an object if an object's method is called. (the highest type in an inheritance hierarchy would be enough, of course).
Does anyone know about a library providing this? I only found some pure parsers that give me an AST. But with the tree alone there's much more work to do to get the required information out of the program.
Thanks
Stefan
|
|
|
|
|
|
I think reflection is not capable of getting information about the inside of a method. I need t know which classes' methods are called by a method. I want to create a call/use graph from a software (can be from source code).
|
|
|
|
|
|
Ok, I'm missing something fundamental here - I know it. Would one of you gurus be so kind as to point out my error, please.
I have a class Called ProcessInfo in the namespace Mycompany.Project that is used to store custom process information...
namespace MyCompany.Project
{
public class ProcessInfo
{
private string m_sStatus;
.
.
.
}
}
This class is used in two projects. Project A is a dll that uses the same (MyCompany.Project) namespace. Project B is an exe also uses the same namespace.
Project B dynamically loads and invokes the Project A dll. Project B passes an instance of an ArrayList to Project A, who adds instances of the ProcessInfo class to the ArrayList. Project B attempts to loop through the ArrayList to access the ProcessInfo instances as follows:
private void SomeFunction()
{
foreach (ProcessInfo oInfo in oArrayList)
{
.
.
.
}
}
I always get invalid cast exceptions on the foreach line. The debugger correctly show the oArrayList as containing instances of type {MyCompany.Project.ProcessInfo} and the contents of the individual ArrayList entries can be read (in the debugger).
What am I missing? I've tried various permutations of the above code - including fully qualifying the namespace, using IEnumerator, etc...
I have searched MSDN, but have had little luck specifying the correct query to find answers.
I would very much appreciate any help in solving this. I thank you in advance.
Peff
|
|
|
|
|
I want to retrieve the VID and PID of usb device,such as mp3 and u-disk.I know how to enum the device infomation by using some of windows API step by step.I just do not know how to define the GUID structure,how to pass the GUID as a parameter to the api,such as SetupDiGetClassDevs(),and especially how to marshal the type of those parameters.
Can you give me an detail example in C#?
|
|
|
|
|
Hi guys
Is there any way that I can set the small/large imagelists for a ListView control to the handle of the system's standard image list?
I know I can use SHGetFileInfo() function for retreiving the system image list, but how do I set it to ListView.SmallImageList? (ie. ListView.SmallImageList.Handle is READ-ONLY)
Any pointers would be appreciated
Thanks
|
|
|
|
|