this the sample code
class Class12
{
public static string last_name;
public static string first_name;
public static double gross_pay;
public void adjustPay(double gross_pay)
{
//adjust the pay by multiplying it by a factor of 1.2
gross_pay = gross_pay * 1.2;
Console.WriteLine("The adjusted pay is {0} for {1} {2}", gross_pay,first_name,last_name);
}
static void Main()
{
Console.Write("Enter the first name : ");
first_name = Console.ReadLine();
Console.Write("Enter the last name : ");
last_name = Console.ReadLine();
Console.Write("Enter the raw pay : ");
gross_pay = Convert.ToDouble(Console.ReadLine());
Class12 obj = new Class12();
obj.adjustPay(gross_pay);
Console.WriteLine("Press any key to continue . . .");
Console.ReadKey();
}
}
The issue here is code works cool but i want the same code implemented using get & set block i tried so like but issue is ITS SHOWING ME 3 errors i.e you already have first_name , last_name ,gross_salary ..
I am confused donno what to do ?? any ideas
MY CODE : USING GET & SET
The issue here is code works cool but i want the same code implemented using get & set block i tried so like but issue is ITS SHOWING ME 3 errors i.e you already have first_name , last_name ,gross_salary ..
I am confused donno what to do ?? any ideas
MY CODE : USING GET & SET
class Class12
{
string last_name;
string first_name;
double gross_pay;
public double gross_pay
{
get
{
return gross_pay;
}
set
{
value = gross_pay;
}
}
public string last_name
{
get
{
return last_name;
}
set
{
value = last_name;
}
}
public string first_name
{
get
{
return first_name;
}
set
{
value = first_name;
}
}
public Class12(string first_name, string last_name, double gross_pay)
{
this.first_name = first_name;
this.last_name = last_name;
this.gross_pay = gross_pay;
}
public void adjustPay()
{
//adjust the pay by multiplying it by a factor of 1.2
gross_pay = gross_pay * 1.2;
Console.WriteLine("The adjusted pay is {0} for {1} {2}", gross_pay,first_name,last_name);
}
static void Main()
{
Class12 obj = new Class12(first_name, last_name, gross_pay);
Console.Write("Enter the first name ");
obj.first_name = Console.ReadLine();
Console.Write("Enter the last name ");
obj.last_name = Console.ReadLine();
Console.Write("Enter the raw pay ");
obj.gross_pay = Convert.ToDouble(Console.ReadLine());
obj.adjustPay();
Console.WriteLine("Press any key to continue . . .");
Console.ReadKey();
}
}
Cheers TY
Cheers TY

VulpesPosted Nov 14, 2013, 7:21 AM
set
VulpesPosted Nov 14, 2013, 8:54 AM
Prior to this, the fields have their default values which is null for strings and zero for doubles.
SUNIL GUTTAPosted Nov 14, 2013, 8:20 AM
Just a small query .. Well in C# Every non static data member show be initialized with object right .. @ basic rule .
In the above code you declared 3 non static variable i.e
string _last_name;
But we haven't initialized with obj ????????
Am i missing something here .. help me
SUNIL GUTTAPosted Nov 14, 2013, 1:26 AM
I made the adjustments .. please look into it . code now working properly but i am not confident enough . . so please check it matter of seconds for you :)
class Class12
{
static string _last_name;
static string _first_name;
static double _gross_pay;
public double gross_pay
{
get
{
return _gross_pay;
}
set
{
value = _gross_pay;
}
}
public string last_name
{
get
{
return _last_name;
}
set
{
value = _last_name;
}
}
public string first_name
{
get
{
return _first_name;
}
set
{
value = _first_name;
}
}
public Class12(string _first_name, string _last_name, double _gross_pay)
{
this.first_name = _first_name;
this.last_name = _last_name;
this.gross_pay = _gross_pay;
}
public void adjustPay(double _gross_pay)
{
//adjust the pay by multiplying it by a factor of 1.2
_gross_pay = _gross_pay * 1.2;
Console.WriteLine("The adjusted pay is {0} for {1} {2}", _gross_pay,_first_name,_last_name);
}
static void Main(string [] args)
{
Class12 obj = new Class12(_first_name,_last_name,_gross_pay);
Console.Write("Enter the first name ");
_first_name = Console.ReadLine();
Console.Write("Enter the last name ");
_last_name = Console.ReadLine();
Console.Write("Enter the raw pay ");
_gross_pay = Convert.ToDouble(Console.ReadLine());
obj.adjustPay(_gross_pay);
Console.WriteLine("Press any key to continue . . .");
Console.ReadKey();
}
}
Actually under constructor i am not sure it can be like this
this.name = _name ???? but i feel it should be like this._name=_name
Well in above declaration i declared the 3 variables with static just becoz to avoid non-static initialization ..
Please :) check the above code in this comment and give perfect touch up with style . I like the variable declaration non-static
Thank you
VulpesPosted Nov 13, 2013, 5:05 PM
One convention is to start the property name with an upper case letter but the field with a lower case letter.
So you could call the property First_name but the backing field first_name.
Another popular convention is to proceed as above but to prefix the field name with an underscore i.e. _first_name.
However, the latter convention is more commonly used in combination with 'camel' casing. So FirstName for the property and _firstName for the backing field.