943,909 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 610
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 20th, 2009
0

error in c#

Expand Post »
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;
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sambafriends is offline Offline
21 posts
since Jun 2008
Jul 20th, 2009
0

Re: error in c#

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.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Jul 20th, 2009
0

Re: error in c#

Quote ...
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
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 165
Solved Threads: 113
Posting Pro
Ramesh S is offline Offline
580 posts
since Jun 2009
Jul 20th, 2009
0

Re: error in c#

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.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Jul 20th, 2009
0

Re: error in c#

>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) ?
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Jul 20th, 2009
0

Re: error in c#

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.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Jul 20th, 2009
0

Re: error in c#

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.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Jul 20th, 2009
0

Re: error in c#

>
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
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 20th, 2009
1

Re: error in c#

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
C# Syntax (Toggle Plain Text)
  1. try
  2. {
  3. }
  4. catch (Exception ex)
  5. {
  6. File.AppendAllText(ExceptionFilePath, ex.Message);
  7. }
BUT, They must have method logs the error
C# Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jul 20th, 2009
0

Re: error in c#

He Ramy thanks for pointing out that Math.Add luckily does not exist...
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: How do I point to data in my project?
Next Thread in C# Forum Timeline: how to use paging in c#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC