Thursday, July 30, 2009

I got an error that cannot find symbol why iam getting this error?

class AddSub{


int a=Integer.parseInt(args[0]);


int b=Integer.parseInt(args[1]);


int add(){


int c=a+b;


System.out.println(c);


return c;


}


int sub(){


int d=a-b;


System.out.println(d);


return d;


}


public static void main(String[] args){


AddSub as=new AddSub();


as.add();


as.sub();


}


}

I got an error that cannot find symbol why iam getting this error?
Your main method has an argument... args[]...


this argument is not known by the AddSub class unless you pass it as argument to one of the class methods or through direct assignment to a static class member.
Reply:Your code has many bugs. Below I list a few and a possible fix.





1) You are mixing VB.NET and C# syntax and keywords ("Integer" is a keyword in VB.NET: "Int32" or "int" is a keyword in C#)


2) Your variable name "as" is a keyword and cannot be used as a variable name


3) "System.out" should be "System.Console.Out"


4) You are not passing "args[]" to your inner class.


5) etc. etc.





Below is some code that takes what you are trying to do and makes it work in C#.





using System;


using System.Collections.Generic;


using System.Text;





namespace ConsoleApplication1


{


class Program


{


public static void Main(String[] args)


{


AddSub as1 = new AddSub(args);


as1.add();


as1.sub();


}


}





class AddSub


{


int a;


int b;


public AddSub(String[] args)


{


a = Int32.Parse(args[0]);


b = Int32.Parse(args[1]);


}





internal int add()


{


int c=a+b;


System.Console.Out.WriteLine( c);


return c;


}





internal int sub()


{


int d=a-b;


System.Console.Out.WriteLine( d);


return d;


}


}


}


No comments:

Post a Comment