|
That's a code horror. You are calling GetProperties far too many times -- you probably only need to once.
|
|
|
|
|
Thanks sir, will keep it in mind in future
♫ 99 little bugs in the code,
99 bugs in the code
We fix a bug, compile it again
101 little bugs in the code ♫
|
|
|
|
|
To understand this behavior, you'll need to know the difference between a value type and a reference type.
Your 'DObject' is a reference type and hence assignment results in the variable pointing to the same object instead of a new object being created as in the case of a value type.
You'll need to implement IClonable interface and implement its Clone method, and then do something like this
D1 = D.Clone();
|
|
|
|
|
In your example you don't need to initialize the objects.
|
|
|
|
|
I propose another solution, seeing problems with my previous solutions, although working.
You can write your own
Clone() function inside your class. You need to mark your class as
[Serializable]
Function can be written as:
public object Clone()
{
object oClone;
using (MemoryStream myStream = new MemoryStream())
{
BinaryFormatter binFormatter = new BinaryFormatter();
binFormatter.Serialize(myStream, this);
myStream.Position = 0;
oClone = binFormatter.Deserialize(myStream);
}
return oClone;
}
You can call this function as
eg:
Person p2 = (Person)p1.Clone();
♫ 99 little bugs in the code,
99 bugs in the code
We fix a bug, compile it again
101 little bugs in the code ♫
|
|
|
|
|
Hi, I think this is your answer here.
Use of MemberWiseClone method of System.Object.
Please refer below example, you will come to know how to pass value of object and if you change one then other will not change like any other value type.
class Program
{
static void Main(string[] args)
{
D1 d = new D1();
d.Prop1 = "Sample...";
D1 d2 = d.ShalloCopy();
d2.Prop1 = "Some else";
}
}
class D1
{
public string Prop1 { get; set; }
public D1 ShalloCopy()
{
return (D1)this.MemberwiseClone();
}
}
In above example, when you make change into d2 then d will remain same it will not change.
|
|
|
|
|
I have a datagridview in a winform application, that gets data from an external dll and displays it dynamically.
As the new rows are getting added I want to show the newly added row to the user (so that the old rows keep moving upwards).
I am adding the new rows and showing the last added row as follows
string[] currentRow = new string[5];
currentRow[0] = curr.sequence().ToString();
currentRow[1] = typeString;
currentRow[2] = curr.stat().ToString();
currentRow[3] = detail;
currentRow[4] = curr.timestamp().ToString();
this.dataGridView1.Rows.Add(currentRow);
this.labelCount.Text = curr.sequence().ToString();
dataGridView1.CurrentCell = dataGridView1.Rows[curr.sequence()-1].Cells[0];
I am getting an exception of
[System.ArgumentOutOfRangeException] = {"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}
at the line where I set the CurrentCell.
Although it occurly at random rows everytime. Sometime it occurs as early as 25th row. Other times at 1000th row.
What could be causing it? I see that the sequence is displayed correctly at labelbox.
Please help.
Thanks in advance.
|
|
|
|
|
What is curr ? and what value does curr.sequence() return?
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
curr the object that I receive from the api/dll. curr.sequence() returns int value.
|
|
|
|
|
Good. Now tell us something that isn't so bloody obvious
What is the VALUE it returns? Is it 10, 25, -7? Does it return something that is random or sequential? Do you know what it does? Is it perhaps returning values that are out of order?
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
I apologize for my vague reply.
The sequence is like a counter that the dll increments as it creates new objects (here it is curr. The dll takes care of the object destruction). It ranges from 0-1500. This part of the code that I submitted is inside the an event that gets fired from the dll once the curr is available.
The sequence returns the correct value, as I display it the labelCount.
But anyways, I am storing the curr in a local list as I get. If I use list.Count() instead of curr.sequence(), then everything works fine. The problem is solved, but I don't understand why the datagridview has problem with the curr.sequence().
|
|
|
|
|
So what VALUE did it return at the point of the exception? Telling us what values it may have during the running of the program is no use at all.
manumith wrote: The sequence returns the correct value, as I display it the labelCount.
So what value gets displayed in this instance?
You have all the information, please share it.
The best things in life are not things.
|
|
|
|
|
There could be any number of reasons why there's a problem here, but they all come back to the same thing. curr.sequence is in the wrong order - this could be because of threading being used to retrieve values, but it's more likely to do with the item that's sorted on isn't curr.sequence . In other words, the order doesn't match the sort order.
|
|
|
|
|
I don't know, however:
1.
I would not call curr.sequence() over and over, if it were to change its mind in between calls, you'd be in trouble; so just call it once, store the value in a local variable, and use it.
2.
as the new row is at the bottom, how about
int nRows=dataGridView1.Rows.Count;
if (nRows>0) dataGridView1.CurrentCell = dataGridView1.Rows[nRows-1].Cells[0];
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
How come there's no such properties as "List|Dictionary.Capacity" - and, does anybody knows if inserting to last slot what's dotnet Collection behavior? Allocate 2xCapacity? In absence of MyDict.Capacity, how can I manipulate this?
Also, when you insert an element if Reference type into collection class, Collection class only stores a "Pointer" or "Reference", not a "Copy"
<br />
IDictionary SomeDict = new Dictionary<SomeClass, string>(MAXITEMS);<br />
SomeClass o = new SomeClass();<br />
o.a = -1;<br />
o.b = -1;<br />
o.c = -1;<br />
o.d = -1;<br />
SomeDict.Add(o, Guid.NewGuid().ToString());<br />
Observer this on debugger:
<br />
*o<br />
*SomeDict.ElementAt(0).Key<br />
They'd give you same reference.
So, I'm not quite sure why I'd pick a LinkedList over a Dictionary - my guess is, Dictionary's Hash table in the back is still one contiguous block of memory, where as LinkedList is not (but also because it's slots of memory all over the place, access is slower?) But there's no MSDN doc or article which describes this (For example, LinkedList references to payload/elements are stored in usual "array" of reference? If so it too has allocation problem if you keep adding). So really is allocation the only concern?
Thanks
Good reference but a bit unclear in some respect...[^]
Another discussion[^]
dev
modified on Monday, April 25, 2011 7:16 AM
|
|
|
|
|
There is List<T>.Capacity[^]. You can add elements till Count equals Capacity before the internal array gets resized automatically. This[^] might interest you.
There isn't a Capacity for hash-based collections, as it wouldn't be very meaningful; what can be added to a Dictionary, without growing it, depends on the current situation of the buckets and the hash value of the new entries.
[ADDED AFTER OP GOT MODIFIED]
Yes, a collection of reference types stores references. Form.Controls contains the (references to) the Controls of a Form, storing a copy of the Controls would be quite useless as the copy would become stale pretty soon, and changing a property would be irrelevant to the Form.
As far as storage goes, there are three types of collections:
- the regular ones, not hash-based, such as List, SortedList, Set, ...; they internally have a single array.
- the dictionary ones, based on hashing; they use a more complex scheme to improve the search performance.
- the linked-list ones; they store individual items, without using arrays.
If you're interested, google "skip list" and/or read A Skip List in C#[^]
[/ADDED]
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Thanks Luc, but for Dictionary, it's internal Hash table still needs to be stored some where right? I presume allocation resembles that of IList - i.e. a dict would allocate a block of contigious memory where hash+reference to actual elements are stored. (i.e. if you keep adding to it, eventually it'd reach "Capacity" specified by Dictionary's constructor)
Further if LinkedList does the same internally, sounds to me real difference between SortedDictionary and LinkedList, don't you think? (LinkedList needs to store an array of references some where and in which case it must have fixed size, unless internally it uses a List<void*>). Forgot to say, both Dictionary and LinkedList supports "ElementAt($INDEX$)", but LinkedList cannot do lookup by Key
dev
|
|
|
|
|
I suggest you look up "linked list" and "hash table" in Wikipedia, or in a relevant book. The former is normally not implemented with an array, the latter probably uses an array and more.
And if you need to know the details of the (current) implementation in .NET, read the documentation and investigate using a tool such as Reflector.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
hum... I implemented/handcoded linked list before when I was doing C++, just want to know how it's done in dot net, internally (And hopefully don't have to observe on debugger for everything...)
dev
|
|
|
|
|
Further, note
LinkedList constructor - you may not specify Capacity (Suspect internal when you add an element SomeClass to the list, LinkedList class would wrap it inside a "Node" object, and node object would have reference to "Next" node. (It's a double linked list behind the scene)[^]
Dictionary - you *can* specify capacity (I suspect therefore when inserting to last slot, reallocation would occur - not sure grow by how much however.)
dev
modified on Monday, April 25, 2011 7:59 AM
|
|
|
|
|
devvvy wrote: I suspect therefore when inserting to last slot, reallocation would occur - not sure grow by how much however.
I think dictionaries are typically implemented to grow once a certain percent of capacity has been reached, though "capacity" would be somewhat of a misnomer. So maybe once the array is at least 70% full, then it resizes (read about load factor). Also, dictionaries don't need to resize, as they typically contain collision lists... the resizing is done for performance. As far as how much they would grow, that would probably be some constant multiplier (e.g., by 2x each growth). Some implementations allow you to specify these values (growth factor and resize threshold).
If you want to see how .Net implements the Dictionary class, I recommend using Reflector or ILSpy. You might also consider monitoring your memory usage with very large dictionaries. The MSDN documentation might also contain some info about the constants used.
Fixign now. | But who's fixing the fixign? |
|
|
|
|
|
One of the reasons the implementation details are not in the documentation, is because they can change with new versions of .NET. The implementation details are intentionally hidden from the user so they can be modified/improved if needed, while providing the same external functionality (and general performance characteristics).
If you want to explore the details of the implementation for a particular version of .NET, then Reflector/ILSpy/etc is the way to go. But don't count on those details to be the same in the next release.
|
|
|
|
|
devvvy wrote: I'm not quite sure why I'd pick a LinkedList over a Dictionary
Linked lists and dictionaries are very different data structures. Linked lists contain a list of items. Dictionaries contain a bag of key/value pairs. Linked lists are typically ordered by the order items are inserted. You typically access items in a linked list by nodes (current, next, first). In a dictionary, you access values using a key. Dictionaries may use an array and a bunch of linked lists to store data internally. Linked lists will typically use independent nodes that point to other nodes (i.e., nodes are linked using pointers).
As far as why you would choose one over the other, there are plenty of reasons for that (e.g., random access time for a certain element).
Fixign now. | But who's fixing the fixign? |
|
|
|
|
|
AspDotNetDev wrote: Linked lists are typically ordered by the order items are inserted.
That may be the case, since I'm not sure about "typical" usage. However, linked lists have the special feature that it is easy to insert elements in the middle. Therefore, I'd argue that they aren't ordered by insertion order. An array seems more likely to have elements ordered by order of insertion.
But I'm surprised nobody before you pointed out how different linked lists and dictionaries are in terms of algorithmic efficiency and functionality.
|
|
|
|
|
Hi All,
Iam New To C#
I Writed Code Like This
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Data.OleDb;<br />
<br />
<br />
namespace Access<br />
{<br />
<br />
public class Connection<br />
{<br />
OleDbConnection con;<br />
OleDbCommand com = new OleDbCommand();<br />
<br />
public void connect(string connectionstring)<br />
{<br />
<br />
con = new OleDbConnection(connectionstring);<br />
con.Open();<br />
}<br />
<br />
public void Command(string Commandtext)<br />
{<br />
<br />
OleDbCommand com = new OleDbCommand(Commandtext);<br />
<br />
}<br />
public void ExecuteNonQuery()<br />
{<br />
<br />
com.ExecuteNonQuery();<br />
<br />
}<br />
public void CloseConnection()<br />
{<br />
<br />
con.Close();<br />
}<br />
<br />
<br />
<br />
<br />
}<br />
<br />
<br />
<br />
}
Thease Statements are not working Correctly Really I Dont Know What to Do.
Can Any one Help me With A example Code?
Please Help Me.
Arunkumar
|
|
|
|
|