I am absolutely new to C# , reading the Book , Beginning Visual C# 2005 ,byKarli Watsonet al. , Wrox Press . Using Visual Studio 2005 .

Okay , I was practicing some of the concepts from Functions chapter and i faced a wierd error.

This the code which is running OK now.

namespace Ch06Ex01
{
    class Program
    {
        static void Write()
        {
            Console.WriteLine("This is my first C# Function Code");
        }
        static sbyte AddTwo()
        {
            Console.WriteLine("Adding two numbers and returning the value");
            sbyte testNum = 2 + 5;
            return testNum;
        }
  
          static double AddTwoNum(double Num1,double Num2)
       {
            return Num1+Num2;
        }   
        static void Main(string[] args)
        {
            Write();
            Console.WriteLine("{0}",AddTwo());
            Console.WriteLine("Enter two numbers two be added by a test function :");
            double Num1,Num2;
            Num1 = Convert.ToDouble(Console.ReadLine());
            Num2 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("The Addtion of abive 2 numbers r = {0}", AddTwoNum(Num1,Num2)); 
            Console.ReadKey();
        }
    }
}

which is fine and dandy . But the problem crops in when in the function static double AddTwoNum(double Num1,double Num2) i use byte inplace of double .

static byte AddTwoNum(byte Num1,byte Num2)
        {
            return Num1+Num2;
        }   

// ..Same Code...
// ..Same Code...
            Byte Num1,Num2;
            Num1 = Convert.ToByte(Console.ReadLine());
            Num2 = Convert.ToByte(Console.ReadLine());
            Console.WriteLine("The Addtion of abive 2 numbers r = {0}", AddTwoNum(Num1,Num2));
//..Same Code..

The error is shown @ return Num1+Num2; , a blue curly wavy underlined , when cursor is brought, a message pops up

Cannot Implicitly convert type 'int' to 'byte'. An Explicit Conversion exists (Are you missing a cast ?)

WHY ? :( Please someone come to my rescue ?

You can't add two bytes!

commented: Ok, thanks!But thats bit illogical! lolz! +1

first you have to convert to decimal, double, float, int, uint, long, ulong, object, short or
ushort then add

Waah! Two Bytes can't be added ! That's ODD!!! lol...

Does it mean that any mathematical operation between two bytes is impossible ? Why is it So :O :(

Hi all, my first post here ^^
I'm new too in the programming world, but improovin fast.

I dont think thats the problem: u can make addiction between short bytes, but the result cant be one... sbyte goes from -128 to +127 i guess, so if u add 127+127 thats no more a sbyte...
u just have to return for example a int, and it should work...

using System;
class Program
 {
     static void Write()
         {
         Console.WriteLine("This is my first C  Function Code");
     }
     static sbyte AddTwo()
         {
         Console.WriteLine("Adding two numbers and returning the value");
         sbyte testNum = 2 + 5;
         return testNum;
     }
     
    static int AddTwoNum(sbyte Num1,sbyte Num2)
         {
         return Num1+Num2;
     }
     static void Main()
         {
         Write();
         Console.WriteLine("{0}",AddTwo());
         Console.WriteLine("Enter two numbers two be added by a test function :");
         sbyte Num1,Num2;
         Num1 = Convert.ToSByte(Console.ReadLine());
         Num2 = Convert.ToSByte(Console.ReadLine());
         Console.WriteLine("The Addtion of abive 2 numbers r={0}",AddTwoNum(Num1,Num2));
         Console.ReadKey();
     }
 }

Im not sure of it cuz of my noobness ^_^, but I hope it helps...

ok why you didn't try to overload the + operator it is possible in a C# context

u can even add two customized objects if u want

I give u an example of overloaded operator + :
suppose that we want add to ratios 2/3 and 5/3 and we want that the result is returned as a ratio I mean
21/9
I define a ratio class within witch I overload the + operator

public class Ratio
    {
      private double _Numerator;
      private double _Denominator;
      public double Numerator
      { 
              get{return _Numerator;}
              set{_Numerator=value;}
      } 
      public double Denominator
      { 
              get{return _Denominator;}
              set{_Denominator=value;}
      } 
      public Ratio(){}
      public Ratio(double Numerator, double Denominator)
      {
         this.Numerator = Numerator;
         this.Denominator = Denominator;
      }
      public void GetRatio()
      {
         Console.WriteLine(string.Format("Ratio value is : {0}/{1}",Numerator,Denominator));
      }
       /* Pay attention here, overloding is done only in static mode, I mean when we define a method that overloads an operator it must be static*/
      public static Ratio operator +(Ratio Ratio1, Ratio Ratio2)
      {
          double n1 = Ratio1.Numerator;
          double n2 = Ratio2.Numerator;
          double d1 = Ratio1.Denominator;
          double d2 = Ratio2.Denominator;
          return new Ratio(n1*d2 + n2*d1,d1*d2);
      }


}

then I use this class in the main prpgram to add to ratio objects:

        Ratio Ratio1 = new Ratio(2,3);
        Ratio Ratio2 = new Ratio(5,3);
        Ratio1.GetRatio();
        Ratio2.GetRatio();

        Ratio Result = new Ratio();
        Result = Ratio1 + Ratio2;
        Result.GetRatio();

        Console.Read();

I think event that I didn't repond to your question as well, overloading operators is very useful tool in C#.

:-O Nice job

I've just learned somthin more :)

Thx !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.