473,699 Members | 2,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to check if an object already exists

oll3i
679 Contributor
how to check if an object already exists and return reference to that object
Apr 5 '07
59 32831
oll3i
679 Contributor
Expand|Select|Wrap|Line Numbers
  1. public boolean equals(Object o) {
  2.     if (!(o instanceof Klient)) return false;
  3.     Klient k = (Klient) o;
  4.     return  nazwa ==k.nazwa && pieniadze == k.pieniadze;
  5.   }
  6.  
i did it like this is it okey
Apr 7 '07 #21
JosAH
11,448 Recognized Expert MVP
Expand|Select|Wrap|Line Numbers
  1. public boolean equals(Object o) {
  2.     if (!(o instanceof Klient)) return false;
  3.     Klient k = (Klient) o;
  4.     return  nazwa ==k.nazwa && pieniadze == k.pieniadze;
  5.   }
  6.  
i did it like this is it okey
If 'nazwa' and 'pieniadze' can be compared for equality using the '==' operator,
I think all is fine. If one of them is an object (i.e. not a primitive) you have to
compare those members using the equals() method again (think about Strings).
Don't forget to implement the hashCode() method too.

kind regards,

Jos
Apr 7 '07 #22
oll3i
679 Contributor
but i have a problem here cos when one button is clicked i create a client and add it to the list then when other button is clicked that client adds to the cart but the client created with the first button is not seen in the other portion of the code (the code for the second button)
Apr 7 '07 #23
JosAH
11,448 Recognized Expert MVP
but i have a problem here cos when one button is clicked i create a client and add it to the list then when other button is clicked that client adds to the cart but the client created with the first button is not seen in the other portion of the code (the code for the second button)
That's an entirely different problem. Forget about GUIs for now and just take
care that your equals() and hashCode() methods are correctly implemented.
Build a little main() method that sticks Klients in a List and tries to find them
again afterwards. If all that works, build your GUI.

kind regards,

Jos
Apr 7 '07 #24
oll3i
679 Contributor
i wdnt have to add to the list if the equals method worked :)
Apr 7 '07 #25
JosAH
11,448 Recognized Expert MVP
i wdnt have to add to the list if the equals method worked :)
True, but my guess is that your equals() method doesn't work yet and the less
circumstancial code around (such as buttons etc.) the less difficult it is to
debug your code. Note that a list can store duplicate objects (the equals()
method returns true) so you should test before adding:
Expand|Select|Wrap|Line Numbers
  1. if (!list.contains(klient)) list.add(klient);
... or you could use a Set for this but still then: your equals() method should
work correctly.

kind regards,

Jos
Apr 7 '07 #26
oll3i
679 Contributor
the equals method works i compared two clients
Apr 7 '07 #27
JosAH
11,448 Recognized Expert MVP
the equals method works i compared two clients
Good; did you override the hashCode() method too?

kind regards,

Jos
Apr 7 '07 #28
oll3i
679 Contributor
Expand|Select|Wrap|Line Numbers
  1. public int hashCode()
  2. {
  3. int result = 17;
  4. result = 37 * result + pieniadze.hashCode();
  5. result = 37 * result + nazwa.hashCode();
  6.  
  7. return result;
  8. }
  9.  
pieniadze is a double and i have a problem
and i dont know if it a good definition
Apr 7 '07 #29
JosAH
11,448 Recognized Expert MVP
Expand|Select|Wrap|Line Numbers
  1. public int hashCode()
  2. {
  3. int result = 17;
  4. result = 37 * result + pieniadze.hashCode();
  5. result = 37 * result + nazwa.hashCode();
  6.  
  7. return result;
  8. }
  9.  
pieniadze is a double and i have a problem
and i dont know if it a good definition
primitives don't have methods so 'double.hashCod e()' doesn't make sense; but
why not simply cast the double value to an int and use that as the double's
hash value:
Expand|Select|Wrap|Line Numbers
  1. int result = 17;
  2. result = 37 * result + (int)pieniadze;
  3. result = 37 * result + nazwa.hashCode();
Is 'nazwa' an object? If so you have to use its 'equals()' method in your own
'equals()' method (see a couple of replies back).

kind regards,

Jos
Apr 7 '07 #30

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2460
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what happens if one of those objects, when it is created, takes a reference to the object that contains it? Do bad things happen? class McShow {
2
11856
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an error message and stop the process every time someone loads the script after the table is created because that would mean the page could only ever run once which of course not the solution I was looking for. I simply want to know how I can check...
2
6030
by: Mike | last post by:
I´ve got a number of SPAN elements named "mySpan1", "mySpan2", "mySpan3" etc, and want to set their "style.display" to "inline". This works (only needs to work on IE5.5+): for (var x = 1; x < 20; x++) { document.all('mySpan'+x).style.display = "inline"; } But I don´t know how many SPAN elements there are, so I need to set x to a
9
7250
by: Carl Fenley | last post by:
I am successfully adding stored procedures to an Access database. However, I need to be able to check if the stored procedure of the same name already exists. Is there a way to do this other than waiting for the OleDbException caused when adding one that already exists? Here is the code snippet: Private Sub CreateStoredProcedures()
4
3011
by: jes | last post by:
hi, i have an open & delete btn. onclick of open as visio drawing opens in visio & onclick of delete the drawing gets deleted from the filesystem. The problem is i am unable to perform these actions if the file is already opened in another instance of visio. how do i check if it's already opened and then throw an error msg? thanks
14
46240
by: John Salerno | last post by:
What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I just want to check if a file of a certain name exists before the user creates a new file of that name. Thanks.
3
11044
by: byeung | last post by:
Hi, I am trying to check if a particular record already exists in an Access database through Excel vba code. Through code obtained at another forum, I got the following: *********************************************************************** Sub TheButton()
2
35085
by: Larry | last post by:
Thanks in advance. what is the C# equivalent of the VB.net IsNothing(object) function? just trying to check to see if an object instance already exists. does someone have an example? *** Sent via Developersdex http://www.developersdex.com ***
0
6453
by: bharathreddy | last post by:
This article will explain you how to check weather a column already exists in a table before you add the column to the table using alter command. Using the system tables you can check to see weather a column already belongs to a specific table. SYSCOLUMNS is the system table which stores all the table columns information, from this column you can check weather a specific column exists in a specific table. ...
0
8705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9197
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8897
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7785
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.