954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

error in c#

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;

sambafriends
Newbie Poster
21 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

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.

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 
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

public static index1 operator ++(index1 x)
        {
            x.count++;
            return x;
        }
Ramesh S
Posting Pro
580 posts since Jun 2009
Reputation Points: 165
Solved Threads: 113
 

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.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

>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) ?

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

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:cool: 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.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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.

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 
> 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

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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

try
{
}
catch (Exception ex)
{
File.AppendAllText(ExceptionFilePath, ex.Message);
}

BUT, They must have method logs the error

try
{
}
catch (Exception ex)
{
LogException(ex);
}
void LogException(Exception ex)
{
//Today DM (Development Manager) said we should use File
//Yesterday they said we should log in database
//tomorrow I think they aren't interested to know what exceptions happened
}

Centralize operations important.

Again, regardless small adding operation mentioned above.

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

He Ramy thanks for pointing out that Math.Add :( luckily does not exist...

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

it was example, i didnt claim it existed.

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 
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

If serkan uses then we should use it! We're all guys and we measure things .. we can't let serkan have a bigger call stack than us!//Today DM (Development Manager) said we should use File
//Yesterday they said we should log in database
//tomorrow I think they aren't interested to know what exceptions happened

You should talk to your DM and write your own error reporting routines across the web. I record exceptions locally to a file and push the error to my company's webserver so I can check on application problems throughout the day. I usually know a customers problem and solution before they call me. Unfortunately that means I am also admitting my programs sometimes crash :P

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

namespace first { public class index { int count; public index() { count = 0; } public static index operator++(index x) /* static fun cant manupulate the data member of object. you should use it as " public void operator++(int x)" here int x is just to differentiate the prefix & postfix increment not any other use. fun^ should'nt return any value. you should write count++ only within the fun body*/ { 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;

look between /* */

mahesh saini
Newbie Poster
1 post since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Ramesh already answered your quesiton. Change it to:

public static index operator ++(index x) 
    {
      x.count++;
      return x;
    }
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

mark this solved already!!

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

Scott :D I don't mean logging error there are changing in requirements in rapid way.

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You