|
Hi
I have used one of the typed collection generator tools available for generating type safe .Net collection for my custom object (for example, say customer) . However, I had a question with the Indexof and Contains method. How is it normally implemented? If I pass in a customer object to the contains method or the Indexof method, then for it to function properly, do I need to override the ToString() method for the Customer object so that it returns the key of that object?
Any help is appreciated?
Thanks
|
|
|
|
|
tsramkumar wrote: Indexof and Contains
IIRC, both of them use Equals[^] method to determine equality. The default implementation of Equals in System.Object checks if both references refer to the same object, you might want to override it to define equality in whatever way you want.
If you override Equals, it's recommended to override GetHashCode also, as per this[^] recommendation.
For example,
class Customer
{
int id;
public override bool Equals(object obj)
{
if (obj == null) return false;
Customer b = obj as Customer;
if ((object)b == null) return false;
return this.id == b.id;
}
public override int GetHashCode()
{
return id;
}
}
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Thanks Senthil, that really helps. Btw, if the unique identifier of my custom object is a string (alphanumeric) rather than an int, I cannot override the GetHashCode method right? Can you give me the reason behind your recommendation for overriding this method and where it will be used?
Btw, I got around with this by making the custom object implement the IComprable interface and implement the compareto method which would compare the identifiers adn return if they are equal or not. I am wondering if this is something that is not normally done in this scenario?
Thanks.
|
|
|
|
|
tsramkumar wrote: Can you give me the reason behind your recommendation for overriding this method and where it will be used?
This will be used when you put your custom objects into a Hashtable, or in any associative container. You'd expect to get the same Customer, provided the id is the same, regardless of whether they are two different instances. What I mean is
Hashtable h = new Hashtable();
Customer c1 = new Customer(1);
h[c1] = "Senthil";
...
Customer c2 = new Customer(1);
Console.WriteLine((string)h[c2]);
Users of your class would expect "Senthil", right?
tsramkumar wrote: Btw, I got around with this by making the custom object implement the IComprable interface
I'm not sure if implementing IComparable is enough, does your code work correctly for standard .NET collections like ArrayList?
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
For a certain project i need to find the translation and rotation parameters between 2 'almost identical' paired-coordinate arrays.
The source coordinates need to be transformed to the target coordinates, therefore the translation and rotation parameters should be determined.
The Horn alghoritm is one, but i could not find any code regarding this.
Anyone has some info regarding the determination of rigid body transformation parameters?
Thanks,
Jan
|
|
|
|
|
I got my dynamic buttons to work now, however, when the user clicks the button, I want a message to show up saying which button was clicked. Whether it was the first, second, third, etc. however, when I click any button, it just says it was the last button created. Let's say I have three buttons, if I click Button_1 it will still say Button_3 was clicked. Here is what my function looks like.
<br />
<br />
protected void GenericButton_Click(object sender, System.EventArgs e)<br />
{<br />
<br />
<br />
string iden = "";<br />
string passedin = "";<br />
iden = GenericButton.ID;<br />
<br />
<br />
}<br />
|
|
|
|
|
Where does GenericButton come from?
What you should do is take sender and cast it back to a button:
<br />
Button TheButtonThatWasClicked = (Button) Sender;<br />
Then you can access all the properties of that button:
<br />
string name = TheButtonThatWasClicked.Name;<br />
string text = TheButtonThatWasClicked.Text;<br />
object tag = TheButtonThatWasClicked.Tag;<br />
I included Tag there because tag is a handy property of certain controls that you can use to store any object you want and be able to retrieve it later.
|
|
|
|
|
Hi,
I would like to loop through the following XML file to fill a combobox with the client name value. Does anyone know how to do this and where is an example?
FTP
Client>
Client_Name xmlns="Default1" /
/Client
Client
Client_Name xmlns="Default2" /
/Client
/FTP
Thanks in advance.
Stephen
-- modified at 15:36 Tuesday 1st November, 2005
|
|
|
|
|
Create a DataSet Object and then
<br />
ds.readXml("Path");<br />
for (int i=0;i < ds.tables(0).rows.count;i++)<br />
{<br />
cmbClientName.Items.Add(ds.Tables(0).Rows(i).Items("[Client Name]")<br />
}<br />
Regards
Rizwan Ahmed Malik
R A M
-- modified at 0:31 Wednesday 2nd November, 2005
|
|
|
|
|
|
Hello,
I have:
<br />
foreach(DataColumn col in this.dataview.Table.Columns) { <br />
ColumnIndex++;<br />
.<br />
.<br />
. <br />
} <br />
Is there a way of getting the col HeaderText (not col.ColumnName) within this foreach statement?
Thank you.
|
|
|
|
|
DataColumn doesn't have a HeaderText property. What you are probably looking for is the HeaderText of a DataGridColumnStyle. Try something this:
<br />
foreach(DataGridColumnStyle dgcs in MyDataGrid.TableStyles["MyTable"].GridColumnStyles)<br />
{<br />
string headertext = dgcs.HeaderText;<br />
.<br />
.<br />
.<br />
}<br />
-- modified at 16:51 Tuesday 1st November, 2005
|
|
|
|
|
|
I'm trying to find the straight forward way to sort a single array of dates.
string[] datearray = new string[10]
here is a sample...
i want this:
12/27/2005
12/28/2005
01/01/2006
01/02/2006
01/04/2006
01/05/2006
12/29/2005
12/30/2005
12/31/2005
01/03/2006
to look at this:
12/27/2005
12/28/2005
12/29/2005
12/30/2005
12/31/2005
01/01/2006
01/02/2006
01/03/2006
01/04/2006
01/05/2006
any clues?
|
|
|
|
|
Why are you storing them as strings instead of DateTimes?
Jon Sagara
Look at him. He runs like a Welshman. Doesn't he run like a Welshman? Doesn't he? I think he runs like a Welshman.
Sagara.org | Blog | My Articles
|
|
|
|
|
thats all you needed to say... i fixed it. I needed the output in string format so i had a brain fart and started converting it early... thanks
|
|
|
|
|
I'm looking to access some file properties such as the Author and comments. Where is this information stored? Is there a .Net class that allows access?
|
|
|
|
|
Have you tried hte FileInfo Class?
|
|
|
|
|
I have but it does not expose the information that I'm looking for.
For example if you right click a file choose properties then choose the summary tab. The information that might be present here is what I'm after.
|
|
|
|
|
In that case you'll probably have to pinvoke a win32 api call to do this. You might want to try asking in the c++ forum since win32 is provided as c++ style dlls and 'documented' as .h files.
|
|
|
|
|
Simple question--one would think (...but I'm pulling my hair out).
I'm trying to drag a control (say a button) in a Windows app at runtime.
The button, as I drag it, appears to split into two button images (with each
"instance" having its location updated about half the time.)
My code, in the Form1 class:
-----------------------------------------------------
bool isDragging = false;
Point anchor;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragging = true;
anchor = new Point(e.X,e.Y);
}
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && isDragging)
{
button1.Location += new Size(e.X-anchor.X, e.Y-anchor.Y);
anchor = new Point(e.X,e.Y);
}
}
-----------------------------------------------------
Please save my hair!!
THANKS,
Tom
|
|
|
|
|
|
If i want to store a value in the registry,somthing like:
[Intalation path]+"\\HelpFiles\\Help.htm"
How would i get the inatalation so i can enter it into the setup project registry editor?
Thanks.
|
|
|
|
|
I have an application that retrieves data from a socket. The process is periodically not returning any results. I have tried the following methods for connecting to the socket in an attempt to resolve the issue but have been unsuccessful. Is this a problem in my code or is it related to network issues? What can be done to better track this issue and resolved it?
Code Option #1
<br />
Dim Client As System.Net.Sockets.TcpClient<br />
Dim netStream As NetworkStream<br />
''
Client = New System.Net.Sockets.TcpClient<br />
Client.NoDelay = False<br />
Client.Connect(Switch.IPAddress, Switch.Port)<br />
netStream = Client.GetStream()<br />
''
Do<br />
numberOfBytesRead = netStream.Read(bytes, 0, uCVT.IntNulls(Client.ReceiveBufferSize))<br />
Loop While netStream.DataAvailable<br />
''
If Not netStream Is Nothing Then netStream.Close()<br />
If Not Client Is Nothing Then Client.Close()
Code Option #2
<br />
Dim Socket As Socket<br />
''
Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)<br />
Dim rEP As New IPEndPoint(IPAddress.Parse(Switch.IPAddress), Switch.Port)<br />
Socket.Connect(rEP)<br />
''
Dim bytes(1024) As Byte<br />
Dim bytesRec As Integer = Socket.Receive(bytes, 0, Socket.Available, Net.Sockets.SocketFlags.None)<br />
''
If Not Socket Is Nothing Then Socket.Close()
Thank you,
Jason W.
|
|
|
|
|
I'm confused on the following:
public class mydef
{
int int1;
int int2;
...
pubic void SetValues(int f, int s)
{...}
}
mydef myobj;
myobj.SetValues(1,2);
mydef myobj2;
myobj2 = myobj; // CAN I DO THIS?
myobj.SetValues(3,4);
... What have I just done?
Is it as straight forward as it seems? Did I simply set the values in myobj, create a new object and set it's values to those in myobj, and then modify the values in myobj without affecting myobj2?
If not, how would I do this?
|
|
|
|