c# Client Server Application
-
Hello Everyone I have created a Client/Server application on C# on WPF IDE and I have a little problem on my UserClient property class... Within this class I have a method called:
public override bool Equals(object obj)
{
UserClient temp = obj as UserClient;
if (temp != null)
{
return (userId == temp.userId);
}
if (temp != null)
{
return (userPass == temp.userPass);
}
return false;
}The problem I'm having is: The class UserClient is highlighted with a green line under-neath where when I place MouseOver it sas: UserClient overrides Object.Equals but does not override Object.GetHashCode Here is the entire UserClient class code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;namespace DtposClient
{
public class UserClient
{
// User Client Global variables
// ====================================
private string UserFullName;
private string UserAccessLevel;
private int UserId;
private int UserPassword;public string fullName { get { return UserFullName; } set { UserFullName = value; } } public string accLevel { get { return UserAccessLevel; } set { UserAccessLevel = value; } } public int userId { get { return UserId; } set { UserId = value; } } public int userPass { get { return UserPassword; } set { UserPassword = value; } } public override bool Equals(object obj) { UserClient temp = obj as UserClient; if (temp != null) { return (userId == temp.userId); } if (temp != null) { return (userPass == temp.userPass); } return false; } public UserClient(string fullNam, string aLevel, int usId, int pass) { fullName = fullNam; accLevel = aLevel; userId = usId; userPass = pass; } }
}
Could someone please help me crack this problem.... thanks in advance kind regards lapeci
-
Hello Everyone I have created a Client/Server application on C# on WPF IDE and I have a little problem on my UserClient property class... Within this class I have a method called:
public override bool Equals(object obj)
{
UserClient temp = obj as UserClient;
if (temp != null)
{
return (userId == temp.userId);
}
if (temp != null)
{
return (userPass == temp.userPass);
}
return false;
}The problem I'm having is: The class UserClient is highlighted with a green line under-neath where when I place MouseOver it sas: UserClient overrides Object.Equals but does not override Object.GetHashCode Here is the entire UserClient class code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;namespace DtposClient
{
public class UserClient
{
// User Client Global variables
// ====================================
private string UserFullName;
private string UserAccessLevel;
private int UserId;
private int UserPassword;public string fullName { get { return UserFullName; } set { UserFullName = value; } } public string accLevel { get { return UserAccessLevel; } set { UserAccessLevel = value; } } public int userId { get { return UserId; } set { UserId = value; } } public int userPass { get { return UserPassword; } set { UserPassword = value; } } public override bool Equals(object obj) { UserClient temp = obj as UserClient; if (temp != null) { return (userId == temp.userId); } if (temp != null) { return (userPass == temp.userPass); } return false; } public UserClient(string fullNam, string aLevel, int usId, int pass) { fullName = fullNam; accLevel = aLevel; userId = usId; userPass = pass; } }
}
Could someone please help me crack this problem.... thanks in advance kind regards lapeci
-
Hello Everyone I have created a Client/Server application on C# on WPF IDE and I have a little problem on my UserClient property class... Within this class I have a method called:
public override bool Equals(object obj)
{
UserClient temp = obj as UserClient;
if (temp != null)
{
return (userId == temp.userId);
}
if (temp != null)
{
return (userPass == temp.userPass);
}
return false;
}The problem I'm having is: The class UserClient is highlighted with a green line under-neath where when I place MouseOver it sas: UserClient overrides Object.Equals but does not override Object.GetHashCode Here is the entire UserClient class code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;namespace DtposClient
{
public class UserClient
{
// User Client Global variables
// ====================================
private string UserFullName;
private string UserAccessLevel;
private int UserId;
private int UserPassword;public string fullName { get { return UserFullName; } set { UserFullName = value; } } public string accLevel { get { return UserAccessLevel; } set { UserAccessLevel = value; } } public int userId { get { return UserId; } set { UserId = value; } } public int userPass { get { return UserPassword; } set { UserPassword = value; } } public override bool Equals(object obj) { UserClient temp = obj as UserClient; if (temp != null) { return (userId == temp.userId); } if (temp != null) { return (userPass == temp.userPass); } return false; } public UserClient(string fullNam, string aLevel, int usId, int pass) { fullName = fullNam; accLevel = aLevel; userId = usId; userPass = pass; } }
}
Could someone please help me crack this problem.... thanks in advance kind regards lapeci
Moreover, this code fragment :
if (temp != null)
{
return (userPass == temp.userPass);
}will never be executed (if temp != null, the method will still have returned a value). The error you get is because when you override the Equals() method, compiler is expecting you also override the GetHashCode() one :
public override int GetHashCode()
{
return UserId;
}You can also have a more complex hashcode generation, for example :
public override int GetHashCode()
{
return UserId ^ UserPassword ^ UserFullName.GetHashCode() ^ UserAccessLevel.GetHashCode();
}Here it's up to you to decide which elements of your class will be taken for the hashcode generation.
-
Hello Everyone I have created a Client/Server application on C# on WPF IDE and I have a little problem on my UserClient property class... Within this class I have a method called:
public override bool Equals(object obj)
{
UserClient temp = obj as UserClient;
if (temp != null)
{
return (userId == temp.userId);
}
if (temp != null)
{
return (userPass == temp.userPass);
}
return false;
}The problem I'm having is: The class UserClient is highlighted with a green line under-neath where when I place MouseOver it sas: UserClient overrides Object.Equals but does not override Object.GetHashCode Here is the entire UserClient class code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;namespace DtposClient
{
public class UserClient
{
// User Client Global variables
// ====================================
private string UserFullName;
private string UserAccessLevel;
private int UserId;
private int UserPassword;public string fullName { get { return UserFullName; } set { UserFullName = value; } } public string accLevel { get { return UserAccessLevel; } set { UserAccessLevel = value; } } public int userId { get { return UserId; } set { UserId = value; } } public int userPass { get { return UserPassword; } set { UserPassword = value; } } public override bool Equals(object obj) { UserClient temp = obj as UserClient; if (temp != null) { return (userId == temp.userId); } if (temp != null) { return (userPass == temp.userPass); } return false; } public UserClient(string fullNam, string aLevel, int usId, int pass) { fullName = fullNam; accLevel = aLevel; userId = usId; userPass = pass; } }
}
Could someone please help me crack this problem.... thanks in advance kind regards lapeci
Let me help you with this method:
public override bool Equals(object obj)
{
UserClient temp = obj as UserClient;
return temp != null && userId == temp.userId && userPass == temp.userPass;
}I guess this is what you wanted it to do. For the warning you mention, I think someone has already told you to make a customized GetHashCode for your UserClient class.