In the following HousePlant object's public method having three HousePlant's prameters
HousePlant x = new HousePlant();
x.SetName("Philodendron");
x.SetPrice(29.99);
x.SetValue(true);
In the following HousePlant constructor having three HousePlant parameters
HousePlant x = new HousePlant("Philodendron", 29.99, true);
I wish to know both are same or different.
Loading
Posted Dec 27, 2011, 8:58 AM
VulpesPosted Dec 27, 2011, 8:15 AM
A read-only property has only a 'get'.
A write-only property has only a 'set', but is rarely used.
Posted Dec 27, 2011, 8:00 AM
VulpesPosted Dec 27, 2011, 4:16 AM
Posted Dec 26, 2011, 2:46 PM
HousePlant x = new HousePlant("Philodendron", 29.99, true);
using System;
namespace ConsoleApplication1
{
class DisplayHousePlants
{
static void Main(string[] args)
{
HousePlant x = new HousePlant("Philodendron", 29.99, true);
Console.WriteLine("Name({0}), Price(${1}), Value({2})", x.GetName(), x.GetPrice(), x.GetValue());
Console.ReadKey();
}
}
}
class HousePlant
{
string name;
double price;
bool value;
public HousePlant(string name, double price, bool value)
{
this.name = name;
this.price = price;
this.value = value;
}
public string GetName()
{
return this.name;
}
public double GetPrice()
{
return this.price;
}
public bool GetValue()
{
return this.value;
}
}
//Name(Philodendron), Price($29.99), Value(True)
VulpesPosted Dec 26, 2011, 1:25 PM
Posted Dec 26, 2011, 1:22 PM
using System;
namespace ConsoleApplication1
{
class DisplayHousePlants
{
static void Main(string[] args)
{
HousePlant x = new HousePlant();
x.SetName("Philodendron");
x.SetPrice(29.99);
x.SetValue(true);
Console.WriteLine("Name({0}), Price(${1}), Value({2})", x.GetName(), x.GetPrice(), x.GetValue());
Console.ReadKey();
}
}
}
class HousePlant
{
string name;
double price;
bool value;
public string GetName()
{
return this.name;
}
public void SetName(string name)
{
this.name = name;
}
public double GetPrice()
{
return this.price;
}
public void SetPrice(double price)
{
this.price = price;
}
public bool GetValue()
{
return this.value;
}
public void SetValue(bool value)
{
this.value = value;
}
}
Chintan RathodPosted Dec 26, 2011, 1:21 PM
As we know that constructor is called while object is instantiated. So while you are passing arguments to initialize its attributes. It will not be changed if there is no method inside. Constructor is called once.
And as methods implemented, while instantiating, it has no initial values to store in attributes. So if you have method, you can change value of attributes multiple of time as you need. while this is not possible in constructor.
Thanks.
VulpesPosted Dec 26, 2011, 1:18 PM
The code in the constructor may look like this:
public HousePlant(string name, decimal price, bool value)
{
}