Suppose that there is a Product class,and there are 2 instances of Product, productA and productB
There is an another class Test:
Class Test ()
{
Product _product;
public Test(Product product)
{
this._product=product;
}
}
My question is how do i know _product is productA or productB???
Thanks!!!
Why do u want to know it..well if i were at ur palce i would simply the things this way..
I wud create a property named Type in the Product class. and when i create the object productA i will set its type as typeA and for productB i will set its type as typeB. Then I will read this propery and go on.[8-)]
Please mark this post as Answer if it is of help to you!
I would love to change the world, but they wont give me the source code.
Marked as answer by Figo Fei - MSFT on Apr 02, 2009 07:52 AM
But for better knwoledge, every object has a common method named 'GetHasCode()' that is unique to each instance of the class, If u create 5 instances of same class u will get 5 diffrenct HasCode. So the complecated example:
public class Product
{
public Product()
{
}
}
// Test class
public class Test
{
Product _product;
private string _productType;
Test t = new Test(productA, hasCodeColl);
System.Diagnostics.Debug.WriteLine(t.ProductType);
t = new Test(productB, hasCodeColl);
System.Diagnostics.Debug.WriteLine(t.ProductType);
// Your Debug Dump:
productA.GetHashCode() 51918134 int
productB.GetHashCode() 16538994 int
product.GetHashCode() 51918134 int
this._product.GetHashCode() 51918134 int
product.GetHashCode() 16538994 int
this._product.GetHashCode() 16538994 int
Soumen, India
Budhi Hin Tanu Janike, Sumirau Pavan Kumar
Bal budhi Vidya dehu mohe,Harahu Kalesa Vikar
[My Site: WCF, My Site and Silverlight site @ Silverlight version].
If two string objects are equal, the GetHashCode method returns identical values. However, there is not a unique hash code value for each unique string value.
Different strings can return the same hash code.
Budhi Hin Tanu Janike, Sumirau Pavan Kumar
Bal budhi Vidya dehu mohe,Harahu Kalesa Vikar
[My Site: WCF, My Site and Silverlight site @ Silverlight version].
My question is how do i know _product is productA or productB???
If you for some reason want an identity for specific instances, then you'll have to include a property or method to return that identity.
You can always compare two references to check if they happen to refer to the same instance by using the ReferenceEquals method. This won't tell if you if an instance is 'A' or 'B' - but it will tell you if two references refer to the same instance or not.
Svante
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com [Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.
try this class Program { static void Main(string[] args) { bool test ; A obj1 = new A(); A obj2 = new A(); test = obj1.Equals(obj1); // returns true test = obj1.Equals(obj2); // returns false } } public class A { int a; }
I think kavita_khandhadia's is the best answer till now,But my situation is the Product class was encapsulated in a dll, so i cannot add a property.Any guys have better resolution? Thanks all!!!
*** Pardon me if I am poor
in oops. But have an idea. try this:
1 public class ProductExtender : Product 2 { 3 public ProductExtender():
base() 4 { 5 6 } 7 8 public Guid UniqueID 9 { 10 get 11 { 12 return Guid.NewGuid(); 13 } 14 } 15 }
and code as:
1 ProductExtender pe = new ProductExtender();
2 pe.Name = "aa"; // // Set your Product Properties
3
4 ProductExtender pe1 = new ProductExtender();
5 pe1.Name = "bb"; // Set your Product Properties
Soumen, India
Budhi Hin Tanu Janike, Sumirau Pavan Kumar
Bal budhi Vidya dehu mohe,Harahu Kalesa Vikar
[My Site: WCF, My Site and Silverlight site @ Silverlight version].
Some modification but before use validate this from the top Bosses. [:P]
Code:
Class ProductExtender:
1 public class ProductExtender : Product 2 { 3 public readonly string ProductName; 4 public ProductExtender(string productName):
base() 5 { 6 ProductName = productName; 7 } 8 } // End class
Class: Product
1 public class Product 2 { 3 public int MyProperty {
get; set; } 4 public Product() 5 { } 6 }
And your consumer:
1 Product productA = new ProductExtender("ProductA"); 2 Product productB = new ProductExtender("ProductB"); 3 4 Test t = new Test(productA); 5 Response.Write("<BR> t is type of instance : "+((ProductExtender)t.Product).ProductName); 6 7 Test t1 = new Test(productB); 8 Response.Write("<BR> t1 is type of instance : " + ((ProductExtender)t1.Product).ProductName);
Pardon if going wrong!
Budhi Hin Tanu Janike, Sumirau Pavan Kumar
Bal budhi Vidya dehu mohe,Harahu Kalesa Vikar
[My Site: WCF, My Site and Silverlight site @ Silverlight version].
ddredstar@ho...
Member
351 Points
116 Posts
How to distinct different instances of the same class?
Mar 31, 2009 01:59 AM|LINK
Suppose that there is a Product class,and there are 2 instances of Product, productA and productB
There is another class Test:
Class Test ()
{
Product _product;
public Test(Product product)
{
this._product=product;
}
}
My question is how do i know _product is productA or productB???
Thanks!!!
kavita_khand...
Star
9767 Points
1930 Posts
Re: How to distinct different instances of the same class?
Mar 31, 2009 05:20 AM|LINK
Why do u want to know it..well if i were at ur palce i would simply the things this way..
I wud create a property named Type in the Product class. and when i create the object productA i will set its type as typeA and for productB i will set its type as typeB. Then I will read this propery and go on.[8-)]
I would love to change the world, but they wont give me the source code.
Rimbik
Participant
1359 Points
382 Posts
Re: How to distinct different instances of the same class?
Mar 31, 2009 06:05 AM|LINK
Kavita is absolutely right.
But for better knwoledge, every object has a common method named 'GetHasCode()' that is unique to each instance of the class, If u create 5 instances of same class u will get 5 diffrenct HasCode. So the complecated example:
public class Product
{
public Product()
{
}
}
// Test class
public class Test
{
Product _product;
private string _productType;
public Test(Product product, Dictionary<int, string> hasCodeColl)
{
this._product = product;
_productType = hasCodeColl[product.GetHashCode()];
}
public string ProductType
{
get
{
return _productType;
}
}
}
// Your code behind:
Product productA = new Product();
Product productB = new Product();
Dictionary<int, string> hasCodeColl = new Dictionary<int, string>();
hasCodeColl.Add(productA.GetHashCode(), "productA");
hasCodeColl.Add(productB.GetHashCode(), "productB");
Test t = new Test(productA, hasCodeColl);
System.Diagnostics.Debug.WriteLine(t.ProductType);
t = new Test(productB, hasCodeColl);
System.Diagnostics.Debug.WriteLine(t.ProductType);
// Your Debug Dump:
productA.GetHashCode() 51918134 int
Soumen, IndiaproductB.GetHashCode() 16538994 int
product.GetHashCode() 51918134 int
this._product.GetHashCode() 51918134 int
product.GetHashCode() 16538994 int
this._product.GetHashCode() 16538994 int
Bal budhi Vidya dehu mohe,Harahu Kalesa Vikar
[My Site: WCF, My Site
and Silverlight site @ Silverlight version].
SGWellens
All-Star
126031 Points
10310 Posts
Moderator
Re: How to distinct different instances of the same class?
Mar 31, 2009 12:46 PM|LINK
HashCodes are not guaranteed to be unique.
From the online help for String.GetHashCode:
Here is a simple test:
String S1 = "Hello"; String S2 = "Hello"; Response.Write(S1.GetHashCode().ToString() + "<br />"); Response.Write(S2.GetHashCode().ToString() + "<br />");My blog
Rimbik
Participant
1359 Points
382 Posts
Re: How to distinct different instances of the same class?
Mar 31, 2009 01:10 PM|LINK
Lot many things to learn. oops ... [:P]
Bal budhi Vidya dehu mohe,Harahu Kalesa Vikar
[My Site: WCF, My Site
and Silverlight site @ Silverlight version].
Svante
All-Star
18347 Points
2300 Posts
Re: How to distinct different instances of the same class?
Mar 31, 2009 07:07 PM|LINK
If you for some reason want an identity for specific instances, then you'll have to include a property or method to return that identity.
You can always compare two references to check if they happen to refer to the same instance by using the ReferenceEquals method. This won't tell if you if an instance is 'A' or 'B' - but it will tell you if two references refer to the same instance or not.
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
[Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.
fundu.ujjwal
Member
365 Points
102 Posts
Re: How to distinct different instances of the same class?
Mar 31, 2009 07:54 PM|LINK
Ujjwal ;)
More I learn I know, how less I know..
ddredstar@ho...
Member
351 Points
116 Posts
Re: How to distinct different instances of the same class?
Apr 01, 2009 01:33 AM|LINK
Rimbik
Participant
1359 Points
382 Posts
Re: How to distinct different instances of the same class?
Apr 01, 2009 05:54 AM|LINK
*** Pardon me if I am poor in oops. But have an idea.
try this:
1 public class ProductExtender : Product
2 {
3 public ProductExtender(): base()
4 {
5
6 }
7
8 public Guid UniqueID
9 {
10 get
11 {
12 return Guid.NewGuid();
13 }
14 }
15 }
and code as:
1 ProductExtender pe = new ProductExtender();
2 pe.Name = "aa"; // // Set your Product Properties
3
4 ProductExtender pe1 = new ProductExtender();
5 pe1.Name = "bb"; // Set your Product Properties
Soumen, India
Bal budhi Vidya dehu mohe,Harahu Kalesa Vikar
[My Site: WCF, My Site
and Silverlight site @ Silverlight version].
Rimbik
Participant
1359 Points
382 Posts
Re: How to distinct different instances of the same class?
Apr 01, 2009 06:25 AM|LINK
Some modification but before use validate this from the top Bosses. [:P]
Code:
Class ProductExtender:
1 public class ProductExtender : Product
2 {
3 public readonly string ProductName;
4 public ProductExtender(string productName): base()
5 {
6 ProductName = productName;
7 }
8 } // End class
Class: Product
1 public class Product
2 {
3 public int MyProperty { get; set; }
4 public Product()
5 { }
6 }
And your consumer:
1 Product productA = new ProductExtender("ProductA");
Pardon if going wrong!2 Product productB = new ProductExtender("ProductB");
3
4 Test t = new Test(productA);
5 Response.Write("<BR> t is type of instance : "+((ProductExtender)t.Product).ProductName);
6
7 Test t1 = new Test(productB);
8 Response.Write("<BR> t1 is type of instance : " + ((ProductExtender)t1.Product).ProductName);
Bal budhi Vidya dehu mohe,Harahu Kalesa Vikar
[My Site: WCF, My Site
and Silverlight site @ Silverlight version].