|
I meant within the class itself. Consider this int wrapper class (I've just written it out quickly so is very rough!) and compare the two different == operator overloads...
public class MyIntWrapperClass : IEquatable<MyIntWrapperClass>
{
private int _Value;
public MyIntWrapperClass(int value)
{
_Value = value;
}
public static bool operator ==(MyIntWrapperClass a, MyIntWrapperClass b)
{
bool result = false;
if (!ReferenceEquals(a, null))
{
if (!ReferenceEquals(b, null))
result = a._Value == b._Value;
else
result = false;
}
else
result = ReferenceEquals(b, null);
return result;
}
public static bool operator !=(MyIntWrapperClass a, MyIntWrapperClass b)
{
return !(a == b);
}
public int Value
{
get { return _Value; }
}
public override bool Equals(object obj)
{
return Equals(obj as MyIntWrapperClass);
}
public bool Equals(MyIntWrapperClass other)
{
return this == other;
}
public override int GetHashCode()
{
return _Value; ;
}
public override string ToString()
{
return _Value.ToString();
}
}
modified on Wednesday, October 7, 2009 5:24 PM
|
|
|
|
|
LimitedAtonement wrote: I do this kind of thing ALL the time
So your job is to edit and refactor code?
From a performance and readability perspective I saw nothing wrong with the first snippet, except the casting the object twice (once for the is comparison and once for the as). If you had been working for me I'd question why you were wasting time like this.
All of the coding standards I have used call for curly braces with control statements and one statement per line. It is not only more readable but helps when debugging to see what statement is being executed.
Not checking an object after doing a cast, or as in this case, is just plain stupid.
only two letters away from being an asset
|
|
|
|
|
Don't hold back. Tell him what your really think.
Everything makes sense in someone's mind
|
|
|
|
|
Mark Nischalke wrote: except the casting the object twice (once for the is comparison and once for the as)
There is no casting happens when is keyword is used. It just emits isinst IL instruction. Here is what MSDN[^] says:
Tests whether an object reference (type O) is an instance of a particular class.
In the code:
if (obj is Insets)
{
Insets insets = obj as Insets;
}
return false; two times isinst instruction will be emitted, one for obj is Insets and the other for obj as Insets because as checks the types before converting. The real conversion happens only when executing obj as Insets . This can be avoided by doing only obj as Insets and check for NULL.
This is micro optimization and in my opinion, no one should worry about such things.
|
|
|
|
|
N a v a n e e t h wrote: There is no casting
You're right, poor choice of words on my part. You explained what I was meant though
only two letters away from being an asset
|
|
|
|
|
N a v a n e e t h wrote: This is micro optimization
No it isn't; the overall effect on performance may not be huge, but using both is and as on the same object is a sign that the developer doesn't understand what as does, and that's worse than the minimal hit to performance.
|
|
|
|
|
Dear Mr. Nischalke,
When I implement code from somewhere, that's when I wind up editing it. For instance, using the RectangleF structure, I noticed to get 'Bottom' to be a smaller number than 'Top' (exemplar gratia in an Euclidean space), I would have to rewrite the code. When copying and pasting code from the internet, when looking over my old code, that's what I mean by all the time.
What do you mean, <quote class="FQ">Not checking an object after doing a cast? I think I checked it with ReferenceEquals .
Do you mean that revisiting code that works is a waste of time? Or refactoring Java to C# is a waste? Or casting the object twice (in is and in as ) is the waste?
In Christ,
Aaron Laws
|
|
|
|
|
IMO, this is more readable
if (insets == null)
than this
if (ReferenceEquals(insets, null))
LimitedAtonement wrote: I cast the object first without asking about the cast.
Not checking an object is unforgivable for a professional developer.
Refactoring must be balanced with need. Just because you can dosen't mean you should. The code may be working perfectly fine and the refactor introduces defects downstream. I've also had developers waste time refactoring when they had other tasks to perform. If you follow coding standards this sort of readbility refactoring won't be necessary.
only two letters away from being an asset
|
|
|
|
|
|
Mark Nischalke wrote: casting the object twice
Whether it casts twice or not, I agree there shouldn't be any need to use both is and as on the same object.
|
|
|
|
|
If it ain't broke don't fix it.
An organization should have coding standards and everyone should follow them. If you have a standard and you find a non-complying file you may fix it (and alert a superior who should endeavor to enlighten the wayward developer). If you have no standard, then don't reformat the code just to "make it more readable".
Any edits you make to the code should otherwise fit the existing format.
LimitedAtonement wrote: I do this kind of thing ALL the time
Then you're wasting time.
LimitedAtonement wrote: ReferenceEquals, not `=='.
For what purpose? == is likely to be overridden to perform what the developer of the class thinks is best, which may simply be ReferenceEquals anyway.
LimitedAtonement wrote: I'm crazy about conserving lines
For what purpose? Other than with compiler directives; you can write C/C++/C# with no line breaks at all. I don't see you doing that; obviously you recognize that whitespace enhances readability (I simply take it to a much higher level ). Back in high school when I was learning BASIC I endeavored to put the most statements on a line as I could -- the result is unreadable by humans. I've matured since then.
LimitedAtonement wrote: some people swear by ALWAYS using braces
I wouldn't say that I swear by it; I recognize that they're not required in some cases. The primary reason is if I need to add an additional statement later, perhaps just during debugging, I don't need to constantly add and remove BRACEs as I work on the code. As an added bonus, by always using them, (I feel) my code has a more cohesive look and feel. Another concern is line length; I've worked in shops where the coding standard specified a maximum line length of eighty characters, BRACEs yield an obvious place to break a line.
<Aside>In my opinion, the BRACEs should be required and the parentheses should not be. Currently, when writing an if statement, you are required to type at least two and at most six delimiters. By requiring the BRACEs rather than the parentheses, the maximim is reduced to four while retaining the minimum of two. Any of you who want to "save keystrokes" should see the logic in this.</Aside>
As to multiple <codelang="text">returns in a method:
They are rarely needed and should only be used when the alternatives are much less readable. In my opinion, single-return methods show that the developer has a more organized concept of program flow and doesn't take shortcuts.
As for the posted code; why not:
Insets insets = obj as Insets;
return ( (insets != null) && (top == insets.top) && (left == insets.left) && (bottom == insets.bottom) && (right == insets.right));
|
|
|
|
|
foreach (Point a in _gr_pts)
{
if (prev != Point.Empty && prev != a)
{
g.DrawLine(_ap_pen, prev, a);
}
prev = a;
}
|
|
|
|
|
I agree with the previous responses: readability is the important bit, and line spacing can seriously improve this - that is one reason why the "how to get an answer" bit at the top of the page stresses to use "code block" or <pre> tags to preserve formating.
Three points:
1) Whatever the company style is, that is the one you use. Making it all look the same is the easiest way to make it all readable - because if the company style makes it all harder to read, then everyone will complain, and the company style will get changed. If everyone codes to a personal style, then everything gets muddled and harder to cope with.
2) My personal preference is always to use braces for a loop of conditional statement. That way, it is much harder to get it wrong when adding a second staement to a single line conditional. This is a lot easier nowadays, with VS etc auto indenting etc., but it is still easier to see what is going on. (It is similar to the reason why C++ accepts "if(A = B)" while C# wont - avoidance of potential problems.)
3) I also indent braces to match the statements in the functional block - I consider them to be a single indented statement in effect. I really hate the open brace at the end of an if statement, with it's matching brace indented. It looks clumsy and makes it difficult yo find matching braces by eye. But, if that is the company style, then I use it. They write the cheques...
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Hear hear!
OriginalGriff wrote: I consider them to be a single indented statement in effect
They are one statement -- a compound statement. At least that's how it is in C/C++, it seems the C# guys didn't like that concept for some reason.
|
|
|
|
|
I have a need to create an application for a Pocket PC, any variety, not settled on actual device yet.
Is C# the best tool for that and do I need other SDK's or other tools to do this?
Thanks for your advice,
Fred Blair
|
|
|
|
|
If you wish to use the .Net framework you would need to develope using the .Net compact framework [^], which you can get here[^](redistributable)
More information[^] can be found here.
Good luck, and again this framework will only support certain handsets/smartphones that have the Microsoft Mobile OS installed on it.
|
|
|
|
|
Where do you find the info on what software is used for what devices?
|
|
|
|
|
Google helps me out with that. Obviously devices running windows mobile ce have the .net compact framework, most other phones use the Java mobile runtime (unsure of the name) but you can, again, use Google.
|
|
|
|
|
i am very new in socket apps.so i need help regarding a idea. i just develop a simple 2 apps related with socket.
one is server and one is client. they are working fine in a LAN but i want that if my 2 apps is running in two different machine and there is no
lan connection, only internet connection is available then how two apps can interact with each by through socket programming other internet.
suppose there is two machine in two different office and there is no connection between two machine but both the machine can access internet.
in this type of situation how socket client can communicate with socket server which running in different office and internet is available in
2 different office.because we know socket server run on a port and wait for incoming connection and socket client will connect to socket server
through IP of that machine where socket server is running. but my situation we can provide IP address if both are member of same network/LAN but here i
told you that there is no connection between two machine where socket server & client is running. in this case what IP address i need to give to socket client to connect to socket server. i have no idea about this but i search google and found some scratch idea that this type of situation can be handle
through WAN IP. so please tell me is it right concept or not. if right then how socket client will know that what is the wan IP of that machine where socket server is running or suppose if i know the wan IP of that machine where socket server is running then can my socket client can communicate with
socket server just by WAN IP.
so please someone give me the idea in detail as a result i can handle this situation and also will be able that to program in such way that socket client
can communicate with socket server if there is no LAN connection but internet connection is available.
please help me with idea and if possible give me a sample code
tbhattacharjee
|
|
|
|
|
If your server has an static IP, then connect to that IP.
If your server has an dynamic IP, then use dynamic dns to connect to that dynamic dns name.
If the server behind a router and has only a local IP, like 192.168....
then you must set the port forwarding in router, to forward all request
from specific port to your server.
If you do that, then it is easy to connect, just for example :
Server : myserver.mydomain.com Port 123
You client can connect to myserver.mydomain.com Port 123, and it works just like in a LAN.
|
|
|
|
|
hi guys
i have table in sqlserver database named tels has columns tels_no , tels_name , tels_cost
ok
i want to enter a no in a textbox in the application then auto it get data of this no and set this in two different textboxes one for tels_name and other for tels_cost
how??????????
i do not know
thanks,
Mohamed El-Wehishy
|
|
|
|
|
You can use ADO.net or data set to get you data from DB.
|
|
|
|
|
Yeah, great answer. I'd mark it as "bestest ever" but the developers of this site probably
never knew you'd come along...
Blikkies wrote: You can use ADO.net or data set to get you data from DB.
What the hell does that bring to anything?
It's like somebody asked: "I'm having trouble fitting windows in my house, I know where I want them and I have a fitting kit for them" and you answered "You can use a hammer or its handle to bang nails into a wall."
As for the author of the question, you'd be better off reading a book on C# and/or a tutorial online.
Rome was not built in a day.
var question = (_2b || !(_2b));
|
|
|
|
|
Greg Chelstowski wrote: What the hell does that bring to anything?
at least i gave him something to google.
|
|
|
|
|
yesssssss
man you are right
sorry for this silly question
Mohamed El-Wehishy
|
|
|
|