|
We have concrete classes, like MyObject as well as an interface for each, like IMyObject. We construct these objects on demand, so we have potentially many objects that are semantically identical, but are different CLR objects. This means that we can't do myObjectA == myObjectB, because that does a reference comparison and we can't overload the == operator for an interface. This StackOverflow question sums it up: http://stackoverflow.com/questions/728434/operator-overloading-with-interface-based-programming-in-c. It's loving annoying.
|
# ? Dec 1, 2010 21:56 |
|
|
# ? Apr 26, 2024 12:34 |
|
Like they say in your link, you should use Equals() to compare for semantic equality and leave operator== alone (except for immutable objects, where you can get away with it). It sucks that Equals() has to be called from one side, but the only other solution would be a utility class. To make things clearer, you could put IEquatable<IMySemanticallyIdenticalObject> onto IMyObject and vice versa.
|
# ? Dec 1, 2010 23:08 |
|
Sedro posted:Like they say in your link, you should use Equals() to compare for semantic equality and leave operator== alone (except for immutable objects, where you can get away with it). It sucks that Equals() has to be called from one side, but the only other solution would be a utility class. To make things clearer, you could put IEquatable<IMySemanticallyIdenticalObject> onto IMyObject and vice versa. To make sure I'm clear, the two objects that are semantically equal are both IMyObjects that are even both the same concrete class under the hood. This is guaranteed, which is why it's kind of annoying that by adding this interface as a level of indirection to keep the API clean (separating implementation from the interface) means that we can no longer use ==
|
# ? Dec 1, 2010 23:15 |
|
What's so bad about Equals?
|
# ? Dec 2, 2010 04:31 |
|
It's weird to compare two objects using A.Equals(B) because now A is in charge of the comparison, and comparing two objects should be neutral. And of course you have to check if A is null. You can get into cases where A.Equals(B) != B.Equals(A). It makes more sense to do ComparisonUtil.Equals(A, B). Operator== is exactly this plus nicer syntax. It's too bad that Object.operator== uses ReferenceEquals instead of Equals.
|
# ? Dec 2, 2010 06:46 |
|
Sedro posted:It's weird to compare two objects using A.Equals(B) because now A is in charge of the comparison, and comparing two objects should be neutral. And of course you have to check if A is null. You can get into cases where A.Equals(B) != B.Equals(A). It makes more sense to do ComparisonUtil.Equals(A, B). Operator== is exactly this plus nicer syntax. If "A.Equals(B) != B.Equals(A)", then your Equals implementation isn't correct. And you can simply call the static method object.Equals(object, object), which will first check reference equality and nulls, and then call the overridable obbject.Equals method. So just override Equals and call "object.Equals(a, b)" instead of "a == b", and you're set.
|
# ? Dec 2, 2010 09:45 |
|
dwazegek posted:If "A.Equals(B) != B.Equals(A)", then your Equals implementation isn't correct. Right. Maybe "just use .Equals and move on with your life" has already sunk in because I learned Java in school, and string comparison required the use of .equals.
|
# ? Dec 2, 2010 15:19 |
|
epswing posted:Right. Exactly. I've always felt that the whole Equals thing for strings is one of the weak points in Java, and it's annoying that I have to deal with the same problem in C# as a result of choosing to follow what I believe is a better design pattern (like having an assembly with the interfaces and having the application deal with the interfaces and then having a separate assembly that implements the interface so that we can swap out implementations easier, which is a design requirement.) If we were just dealing with the concrete class all over, then we could use the regular == because it's overloaded, but because operator overloading isn't available for interfaces (I assume because operators need non-virtual bindings maybe?) we can't specify our own == for the interface. Edit: I also worry about it being unobvious; since the team is obviously used to working with == doing what they want in all other cases that they might use it here and subtle bugs will creep in. This wouldn't be as big of a deal if we could test for that, but I'm not sure how I'd do that other than writing something that looks at the IL output for instances comparing two IMyObjects by their references (I'm not even sure if that's plausible.) POKEMAN SAM fucked around with this message at 17:26 on Dec 2, 2010 |
# ? Dec 2, 2010 17:24 |
|
While we are on operator overloading, lets remember to cover the null cases for ==/!= overloading. This just broke a pretty big system here. Yes, the tests should have covered this.
|
# ? Dec 2, 2010 18:19 |
|
I've found the problem with == in C# is it's ambiguous when it's a reference comparison and when it's a value comparison. The framework types often don't make it very obvious, whereas Equals is usually pretty clear. You can probably do fine just assuming "reference types do a reference comparison and value types do a value comparison" but I've run into enough exceptions that I'm gun-shy at this point. In practice I find that using Equals on strings is almost always required though because I nearly always have to specify a StringComparison value different from the default, which the == operator doesn't allow you to do.
|
# ? Dec 2, 2010 19:41 |
|
Not that it's any better, but if you really prefer to use == for strings you can always do x.ToLowerInvariant() == y.ToLowerInvariant()
|
# ? Dec 2, 2010 21:33 |
|
Orzo posted:Not that it's any better, but if you really prefer to use == for strings you can always do x.ToLowerInvariant() == y.ToLowerInvariant() Is this what the Equals function actually does though? This seems to me like it would be two more string allocations/copies/conversions than the comparison would do.
|
# ? Dec 3, 2010 01:09 |
|
nm
epswing fucked around with this message at 04:36 on Dec 3, 2010 |
# ? Dec 3, 2010 04:24 |
|
code:
|
# ? Dec 3, 2010 06:29 |
|
Ugg boots posted:Is this what the Equals function actually does though? This seems to me like it would be two more string allocations/copies/conversions than the comparison would do.
|
# ? Dec 3, 2010 07:15 |
|
bobua posted:this(working) code fills a listview box with the items in a dictionary. If the number 1 is found in one of the subitems, it colors that 'cell' green. I can't help but think there is a better way to do this. You can do this very concisely in xaml. There's no reason for you to use code-behind. code:
Sedro fucked around with this message at 08:50 on Dec 3, 2010 |
# ? Dec 3, 2010 08:47 |
|
Ugg boots posted:Is this what the Equals function actually does though? This seems to me like it would be two more string allocations/copies/conversions than the comparison would do. No, it doesn't. Using one of the StringComparers (or using the Equals overload that takes an StringComparison) is basically always faster than converting the string to upper/lower and then comparing it. And it's less code to, because you don't have to check for nulls first.
|
# ? Dec 3, 2010 09:30 |
|
dwazegek posted:No, it doesn't. Using one of the StringComparers (or using the Equals overload that takes an StringComparison) is basically always faster than converting the string to upper/lower and then comparing it. And it's less code to, because you don't have to check for nulls first. Who said I ever check for nulls?
|
# ? Dec 3, 2010 17:48 |
|
Sedro posted:You can do this very concisely in xaml. There's no reason for you to use code-behind. Yes, it's winforms. Why do you say that? I started with listbox instead of listview, but it seemed listview was closer to how I wanted to represent the data. It still isn't how I'd like it to look in the end, but I'm afraid I'm going to have to build a custom control or learn wpf to get what I want, which is daunting for someone still trying to get the basics of programming and c#.
|
# ? Dec 3, 2010 20:31 |
|
bobua posted:
Well that is kinda silly. You can easily just go and find the item your looking for using code:
|
# ? Dec 3, 2010 22:05 |
|
bobua posted:
According to MSDN, ListViewSubItemCollection's Add method returns the SubItem object it creates. So you could do something like this: code:
|
# ? Dec 4, 2010 00:26 |
|
Sprawl posted:Well that is kinda silly. Wouldn't this return the item, even if I told it to search the sub items? Phony, that seemed to work great, thanks.
|
# ? Dec 4, 2010 00:57 |
|
bobua posted:Wouldn't this return the item, even if I told it to search the sub items? One other thing you might want to consider as well. Hold off on adding all the ListItems to the ListView until you've created all of them. Then add them with a call to ListView.Items.AddRange. It'll speed up the rendering of it dramatically.
|
# ? Dec 4, 2010 22:16 |
|
PhonyMcRingRing posted:One other thing you might want to consider as well. Hold off on adding all the ListItems to the ListView until you've created all of them. Then add them with a call to ListView.Items.AddRange. It'll speed up the rendering of it dramatically. code:
|
# ? Dec 5, 2010 18:01 |
|
That is what he meant. Or, more simply and about as effective, you can just call l.BeginUpdate before the loop, and l.EndUpdate afterwards. If you add items one at a time without doing that, the listview will do some redrawing automatically after every single one, adding a substantial amount of overhead.
|
# ? Dec 5, 2010 20:38 |
|
Hello, quick question: Is it possible in Visual Web Developper 2010 to make a deployment build where the code-behind files (.aspx.cs) are editable? I'm making an app for my client and they would like to: 1. Review the code 2. Maybe modify things in the future Since I don't think giving them the entire .sln solution/project is the best idea, I thought it'd be good to have some way of having the code NOT compiled, just make it do on-the-fly compilation. I tried setting, in the package/publish settings, to deploy all files, but editing the .cs files doesn't do anything. All the code is compiled in a .dll in the bin/ folder. PS: In case you can't tell I'm new to Visual Web Developper and the whole ASP.NET shenanigans Thanks
|
# ? Dec 6, 2010 00:32 |
|
Would this be the right place to ask a simple question about a WP7 app? I am a complete beginner to programming in general, but cannot understand this problem even after staring at the documentation from msdn. For learning purposes, I'm trying to make a simple app that will convert a user imputed height in feet and inches format to display what the total inches would be. Here is what I have so far: code:
|
# ? Dec 6, 2010 03:32 |
|
Maybe it would work better if you did feetString = feetTextBox.Text;?
|
# ? Dec 6, 2010 03:39 |
|
Zhentar posted:Maybe it would work better if you did feetString = feetTextBox.Text;? Hah, that worked. I knew it was something simple.
|
# ? Dec 6, 2010 03:50 |
|
Sir Davey posted:
Actually, giving them the entire solution/project is the best idea. This will let them fix it after you are out of the picture.
|
# ? Dec 6, 2010 05:00 |
|
Sir Davey posted:Hello, quick question: Haven't used Visual Web Dev, but there should be a "Allow this precompiled site to be updatable" checkbox or something similar either in the website properties or the publish dialog that you'd want to have checked.
|
# ? Dec 6, 2010 16:45 |
|
Need a point in the right direction here. I want to display some information in a way that a normal control won't let me(I think). Just as an example I'd like a listbox that works more like this one http://www.telerik.com/products/aspnet-ajax/listbox.aspx That's web\ajax so I don't think it really applies to what I'm doing(winforms\c#), but I guess that's what I need to do, build a custom control? Every tutorial I find either has nothing to do with the graphical side of building your own control, or has something like changing the color of a button, which is just not much help. So the question is, do I actually need to build my own control? Do I need to buy a book specific to the subject, or am I going about this all wrong?
|
# ? Dec 7, 2010 00:08 |
|
Have you tried the ajax tool kit controls? http://www.asp.net/ajax/ajaxcontroltoolkit/samples/
|
# ? Dec 7, 2010 01:31 |
|
Sprawl posted:Have you tried the ajax tool kit controls? bobua posted:That's web\ajax so I don't think it really applies to what I'm doing(winforms\c#), but I guess that's what I need to do, build a custom control?
|
# ? Dec 7, 2010 18:18 |
|
bobua posted:Need a point in the right direction here. I want to display some information in a way that a normal control won't let me(I think). Just as an example I'd like a listbox that works more like this one http://www.telerik.com/products/aspnet-ajax/listbox.aspx What features from that list box do you need specifically? If you just need to move items from one list box to another, its not too difficult. However, if you want to make it so each item in the list box has an image and other fancy poo poo, you might have to roll your own custom control, and I have never done that since it has always looked like a real pain in the rear end. There are other companies though that sell winforms controls and might have something that does what you want. If possible, you might want to look at WPF, it might be easier to go that route instead of doing your own custom C# control.
|
# ? Dec 7, 2010 20:15 |
|
I did some reading on wpf, and it looks like quite a mess for what I'm trying to do, but probably a smarter way of doing it. I ended up spending all evening yesterday trying to figure out and build the control, which is really just a composite control, a panel with a few labels on it, much like an address card view in outlook. I actually ended up pretty happy with it, but wiring up the events was over my head. I got it done, and it works, but I'm sure it's terrible code.
|
# ? Dec 7, 2010 22:12 |
|
Sprawl posted:Have you tried the ajax tool kit controls? Good god, even though that's not what he needed, the ajax toolkit should be avoided like the plague. At work I just replaced a pre-existing menu system that was made up of Ajax Toolkit's Accordion controls and regular ASP's TreeView controls. They were rendering roughly 250k of markup, inline scripts, and inline stylesheets for what should have been a very simple navigation system.
|
# ? Dec 8, 2010 01:12 |
|
PhonyMcRingRing posted:the ajax toolkit should be avoided like the plague This. "Just ajaxified the site by using the masterpage to wrap all content in an UpdatePanel!"
|
# ? Dec 8, 2010 04:16 |
|
Do any of you guys use F#? I have been reading through this site: http://en.wikibooks.org/wiki/F_Sharp_Programming and following along in VS but it's so different than what I'm used to and a little hard to wrap my head around. It seems really fun, though. I've done a bunch of the problems on http://projecteuler.net using C# and I am going to attempt to go back through and use F# to solve them. Hopefully it will be a good learning experience.
|
# ? Dec 8, 2010 15:46 |
|
|
# ? Apr 26, 2024 12:34 |
|
^^^nope, but finding an excuse here. I'm reading this book myself and figured out I've been using lots of concepts of functional programming for a while, I just didn't have a name for them . .
|
# ? Dec 8, 2010 16:22 |