error in c#

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 20
Reputation: sambafriends is an unknown quantity at this point 
Solved Threads: 0
sambafriends sambafriends is offline Offline
Newbie Poster

error in c#

 
0
  #1
Jul 20th, 2009
namespace first
{
public class index
{
int count;
public index()
{
count = 0;
}
public static index operator++(index x)
{
x.count++;
return count;
}
}
class Program
{
static void Main(string[] args)
{
index i = new index();
i++;

Console.WriteLine(i);
}
}
}


when i am typing this program i got the error like.
first.index.operator++(first.index):not all code paths return a value;
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 120
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: error in c#

 
0
  #2
Jul 20th, 2009
Operator overloading is the least useful thing in C#, the operators are not any different than method calls, it is just good for mathematic obsessive people who like to use smybols rather than words to feel special as they are not valued when they go to techno bar to catch some chicks.
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 446
Reputation: Ramesh S will become famous soon enough Ramesh S will become famous soon enough 
Solved Threads: 82
Ramesh S Ramesh S is offline Offline
Posting Pro in Training

Re: error in c#

 
0
  #3
Jul 20th, 2009
public static index operator++(index x)
{
x.count++;
return count;
}
The return type of this function is of type 'index'. But you return an integer type. This fucntion should be written as
  1. public static index1 operator ++(index1 x)
  2. {
  3. x.count++;
  4. return x;
  5. }
Last edited by Ramesh S; Jul 20th, 2009 at 2:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,995
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 294
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: error in c#

 
0
  #4
Jul 20th, 2009
Operator overloads sure have their use. I don't see why you want to overload the C# ++ operator! It is there for your use! So use it as such, don't get fancy because you like to do it.
An index can easely be incremented with ++. Einstein once said: keep it simple, not simpler.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 120
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: error in c#

 
0
  #5
Jul 20th, 2009
>keep it simple, not simpler.

that is what i told Scott just yesterday night

Einstein and i must think in paralel

But back to topic, even the + operator in math can be used with the Add name as a method. at compiler level each operator and function does the same thing. it is just for us, representation of that functionality is what changes.

what would you lose if you instead of int a = b + c use int a = Math.Add(b,c) ?
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,995
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 294
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: error in c#

 
0
  #6
Jul 20th, 2009
Serkan:
I think a=b+c is faster than a = Math.Add(b,c).
But for example the string class overloads the + operator to perform string concatenation. You could also use the Concat method for this, but a simple + is IMHO simpler. If you look in the produced IL code you would see that this + is translated into Concat by the C# compiler Let the compiler worry about the gory details, that way you don't have to do that.
But you are right, most of the time we don't need operator overloading.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 120
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: error in c#

 
0
  #7
Jul 20th, 2009
i got what you mean, what i say is that "+" is a paradigm that we use for addition, to enable polimorphysm for addition they have the same operator concat strings when the type is string. It is our brain what makes the abstraction here, so instead of associating addition or concatenation with + sign, we could do it with a word "Add". the only problem would be of what data structure would this Add be part of. + does not belong to any class since it is an operator whereas Add is implemented inside different classes.
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,328
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 600
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: error in c#

 
0
  #8
Jul 20th, 2009
Originally Posted by serkan sendur View Post
>
what would you lose if you instead of int a = b + c use int a = Math.Add(b,c) ?
In addition to what danny said calling Math.Add() pushes another call on the stack where as a=b+c does not
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: error in c#

 
1
  #9
Jul 20th, 2009
Math.Add ??? Exists in Math class??! The answer shouldn't be to use it or not to use it.
Regardless this small operation in sometimes I shouldn't do that. Try we log error\exceptions
I can't permit developers team to do that
  1. try
  2. {
  3. }
  4. catch (Exception ex)
  5. {
  6. File.AppendAllText(ExceptionFilePath, ex.Message);
  7. }
BUT, They must have method logs the error
  1. try
  2. {
  3. }
  4. catch (Exception ex)
  5. {
  6. LogException(ex);
  7. }
  8. void LogException(Exception ex)
  9. {
  10. //Today DM (Development Manager) said we should use File
  11. //Yesterday they said we should log in database
  12. //tomorrow I think they aren't interested to know what exceptions happened
  13. }
Centralize operations important.

Again, regardless small adding operation mentioned above.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,995
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 294
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: error in c#

 
0
  #10
Jul 20th, 2009
He Ramy thanks for pointing out that Math.Add luckily does not exist...
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC