|
I have a custom class within my code and I place multiple instances of this class into an ArrayList. At certain points in the code I would like to check if an instance of an object already exists in the array (not really an instance as such, more an object with the same values).
Now this doesnt work, I am certain that I am expecting far to much from the .Contains() function - so if anybody has any guidance on another solution please post it up.
As an example of the "problem" :
The following code returns false
private bool TestMe()
{
testClass tc1 = new testClass("abc", "def", "ghi");
testClass tc2 = new testClass("123", "456", "789");
testClass tc3 = new testClass("xxx", "yyy", "zzz");
testClass tc4 = new testClass("abc", "def", "ghi");
ArrayList array1 = new ArrayList();
array1.Add(tc1);
array1.Add(tc2);
array1.Add(tc3);
return array1.Contains(tc4);
}
The code above uses the following class
internal class testClass
{
public string abc;
public string def;
public string ghi;
public testClass(string abc, string def, string ghi)
{
this.abc = abc;
this.def = def;
this.ghi = ghi;
}
}
But interestingly this returns true
private void TestMe2()
{
string tc1 = "abc";
string tc2 = "123";
string tc3 = "xxx";
string tc4 = "abc";
ArrayList array1 = new ArrayList();
array1.Add(tc1);
array1.Add(tc2);
array1.Add(tc3);
return array1.Contains(tc4);
}
I assume that it works with basic types but not anything custom
post.mode = postmodes.signature;
SELECT everything FROM everywhere WHERE something = something_else;
> 1 Row Returned
> 42
|
|
|
|
|
use this test class and it will work:
internal class testClass
{
public string abc;
public string def;
public string ghi;´
public testClass(string abc, string def, string ghi)
{
this.abc = abc;
this.def = def,
this.ghi = ghi;
}
public override bool Equals(object obj)
{
testClass t = obj as testClass;
if (t == null) return false;
return t.abc == this.abc && t.def == this.def && t.ghi == this.ghi;
}
}
|
|
|
|
|
Thats great it works a treat.
But one small question, when I implement the override Visual Studio gives me the following warning :
c:\_development\csharp\containstest\form1.cs(131,18): warning CS0659: 'ContainsTest.Form1.testClass' overrides Object.Equals(object o) but does not override Object.GetHashCode()
If I put in the default override for GetHashCode(), the warning is no longer shown :
public override int GetHashCode()
{
return base.GetHashCode ();
}
Why does VS need that default override?
Also would it be possible to somehow manipulate the override so that it performs case insensitive searches based on the value of a boolean parameter?
post.mode = postmodes.signature;
SELECT everything FROM everywhere WHERE something = something_else;
> 1 Row Returned
> 42
|
|
|
|
|
The reason for the warning from the compiler is simple, when two objects return true from the .Equals override, they should always return the same hashcode... in essence equality and equal hashes go hand in hand. Also, when overriding .Equals it is good practice to override the == and != operator.
for case-insensitive it should be pretty easy:
public override bool Equals(object obj)
{
testClass t = obj as testClass;
if (t == null) return false;
return t.abc.toLower() == this.abc.toLower() && t.def.toLower() == this.def.toLower() && t.ghi.toLower() == this.ghi.toLower();
}
Another way is to use a CaseInsensitiveComparer , which im sure can be found easily on MSDN.
|
|
|
|
|
My problem relates to the code below, and I cant understand why its happening. I have to use the int test as the values I declared do not work. If you add a watch to guid, amount or serial, the watch value says that these are out of scope, and as such, nothing is assigned to them. If however I add in the int test, and assign it (as shown below), the program works. Any ideas?
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
string guid = "a";
int amount = 1;
byte[] serial = {1};
int test;
test = guid.Length;
test = amount;
test = serial.Length;
amount = r.ReadInt32();
guid = r.ReadString();
serial = r.ReadBytes(32);
r.Close();
fs.Close();
|
|
|
|
|
didn't understand
toticow wrote:
If you add a watch to guid, amount or serial, the watch value says that these are out of scope
when are they out of scope? after u've assigned them, without the int test ?
there are no facts, only interpretations
|
|
|
|
|
I a list view control before removing item(s) I am prompting the user with a message box "if he really wants to delete the item(s)". If the answer is "YES" then the item(s) are removed. when the answer is "NO" then the item(s) are not removed as it should be; but the selected item(s) are not displayed as selected that is hot items with rectacle showing the selection effect. How to show that rectangle?
In another way is to send the LVM_SETHOTITEM message which will show the items as selected. But I don't know how to send a message in C#
|
|
|
|
|
eDip wrote:
In another way is to send the LVM_SETHOTITEM message which will show the items as selected. But I don't know how to send a message in C#
Try the following:
int LVM_SETHOTITEM = 0x103C;
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);
SendMessage(lvwCtrl.Handle, LVM_SETHOTITEM, index - 1, 0);
- Nick Parker My Blog | My Articles
|
|
|
|
|
Hi guys,
I want to ask how to call an external app from C# (a command like Shell command in VB.Net or ShellExcute in VC++).
Thank your.
|
|
|
|
|
|
Thank you for your answer. I had tried and ok. But I want still to know a command in C#, with functionality as Shell or ShellExcute command. Is there a such function in C#? and if yes, What is this? Could you tell me. Thank you very much
|
|
|
|
|
I'm pretty sure, there is not, but the combination of Process and ProcessStartInfo classes should provide everything to customize the startup of an exeternal program.
If not, try to p\invoke the ShellExecute method.
www.troschuetz.de
|
|
|
|
|
I'm trying to remove some items from a listbox but always get failed.
Assume one listbox item's text is "ItemText", value is "New", and index is 1.
I tried the following code to remove this item from a listbox:
ListBox.Items.Remove("ItemText");
Nothing happened after I run this code.
But when I used this: ListBox.Items.RemoveAt(1);
Then this item was removed.
Now I'm in a situation that I cannot get the index of a certain item. Then how can I remove this item if I only know it's text?
Thank you for your help!;)
|
|
|
|
|
You can check if an item is present by using the method Contains.
if (myListBox.Items.Contains(targetObject))
{
//then do something
}
You can get the index of a particular item by using the IndexOf method:
myListBox.Items.RemoveAt(myListBox.Items.IndexOf("ItemText"))
Also, here is some uncalled for advice: When you are deleting multiple rows in the listbox, start deleting items from the bottom.
Cheers
~/sathishvj
|
|
|
|
|
Thank you for your reply;).
I tried Items.IndexOf("ItemText") but the argument of indexof() must be a listitem but not a string or an integer.
Now I find I could make it in this way:
ListItem myitem = new ListItem("ItemText", "ItemValue");
myListBox.Items.Remove(myitem);
That could solve my current problem.
But if I couldn't get the value of t;)he item then I still can not remove it.
|
|
|
|
|
You can always get the index of a ListItem. Try using:
int itemIndex=listBox.FindStringExact("ItemText");
if (itemIndex>-1)
{
listBox.Items.RemoveAt(itemIndex);
}
|
|
|
|
|
Hi,
I am trying to write a service that will automatically change the wallpaper. The SystemParametersInfo call works from a normal windows application but not from a service.
The return value is 0 and I can debug that the error I get is "Unable to find an entry point named SystemParametersInfo in User32.dll".
Would anybody be able to help me figure out why this happens and how to get around it?
Thanks and regards
Sathish
Some code:
public class WinAPI{
//various stuff I tried in DllImport
//[DllImport("user32.dll", EntryPoint="SystemParametersInfoA", CharSet=CharSet.Auto, SetLastError=false, ExactSpelling=true)]
//[DllImport("user32.dll", CallingConvention=CallingConvention.Winapi, SetLastError=false, ExactSpelling=false)]
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SystemParametersInfo (int uAction , int uParam , string lpvParam , int fuWinIni) ;
}
//different ways I tried to call it from within the service
int nResult = WinAPI.SystemParametersInfo(20, 1, bmpFilePath, 0x2);
//int nResult = WinAPI.SystemParametersInfo(20, 0, bmpFilePath, 0x1 | 0x2);
~/sathishvj
|
|
|
|
|
I tried doing something like this. When I changed the properties of the service to allow interaction with the desktop it changed the return value of SystemParametersInfo to 1 instead of 0, but i did not see a change to the desktop still as I do in a regular app.
Maybe it is because I have windows 7, try this for XP.
|
|
|
|
|
Hi everybody!
I am using Net.2005b.
When i used unsafe functiuon with fixed keyword in it , the watch window work not properly. There are ""Unable to evaluate expresion"" message for all variables.
The "Autos" and "Locals" windows are empty.
For example
namespace ConsoleApplication1
{
class Program
{
public int index;
static void Main(string[] args)
{
Program pr = new Program();
pr.test();
}
public unsafe void test()
{
index=10; //at this point watch window not work correctly
fixed (int* j = &index)
{
*j = 20;
}
}
}
}
What is the problem?
|
|
|
|
|
|
I want to know is this problem in beta version or only in my installed version of Net?
|
|
|
|
|
Is there a way to make a MDI or SDI application, which already have menus, like File, Edit, View, just like VC++6 does? Heavy job, creating the menus manually
<italic>Work hard and a bit of luck is the key to success. You don`t need to be genius, to be rich.
|
|
|
|
|
If you are using VS.NET <= 2003 : No.
The next version will have that feature
This posting is provided "AS IS" with no warranties, and confers no rights.
Alex Korchemniy
|
|
|
|
|
<italic>Work hard and a bit of luck is the key to success. You don`t need to be genius, to be rich.
|
|
|
|
|
Oh comon. It can't be that hard.
I takes about 5 minutes to get the base items in VS.net and attach all the handlers.
This posting is provided "AS IS" with no warranties, and confers no rights.
Alex Korchemniy
|
|
|
|