|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
C# Property Methods, Constructors, Overload Constructors?
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||
|
4.0 GHz
|
Hey, wondering if you could help with part of my C# program. How do I make a read/write property method, a default constructor, and an Overloaded Constructor? Here are the instructions if this helps:
Create a read/write property method for each of the class variables. Check the values of the arguments for the set methods that update the product number and the on hand quantity. Only update the class variables if the arguments values are greater than zero. Check the values of the arguments for the set methods that update the description and the on hand quantity. Only update the class variables if the arguments values are not blank (null or ""). Create a default constructor for the Product class. Initialize the product number and the on hand quantity to zero. Initialize the description to "no description". Initialize the unit of measure to "no unit of measure". Create an overloaded constructor that takes four arguments - one each for the product number, description, on hand quantity, and unit of measure.
__________________
Future Upgrades:
Jesus is my Savior
|
|||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
AMD Overclocker
|
It sounds like the professor wants you to create a program that has:
A constructor. This will create your object. An overloaded constructor. This will allow you to create an object specifying something about the object. An accessor. This will allow you to get information from the object. A mutator. This will allow you to set some kind of information about the object. For example: Code:
using System;
public class TestClass
{
private string MyColor = "yellow";
public string Color
{
// Default Constructor
Color()
{
}
// Overloaded constructor - sets color from method call
Color(String x)
{
set MyColor = x;
}
// Accessor - gets property of object
get
{
return MyColor;
}
// Mutator - sets property of object
set
{
MyColor = value;
}
}
public static void Main()
{
TestClass c = new TestClass();
Console.WriteLine(c.Color); // get
c.Color = "blue"; // set
Console.WriteLine(c.Color); // get
}
}
Just curious... Are you a compsci student or is the just a class you have to take?
|
|||||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||||
|
4.0 GHz
|
Thanks for the help :] .. I'm just a 16yr old kid learning on my own, interested in programming. I'm just a beginner, but in the future I do want to go into the Computer Science field.
__________________
Future Upgrades:
Jesus is my Savior
|
|||||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
AMD Overclocker
|
Wonderful to hear. Are you starting with C# as your first language?
|
|||||||||||||
|
|
|
|
|
#5 (permalink) | ||||||||||||
|
Security Sleuth
|
this?
Code:
public class Product
{
private int pNumber, quantity;
private string description, unit;
public Product()
{
this.pNumber = this.quantity = 0;
this.description = "no description";
this.unit = "no unit of measure";
}
public Product(int prN, int qtd, string desc, string un)
{
this.pNumber = prN;
this.quantity = qtd;
this.description = desc;
this.unit = un;
}
public int _productNumber
{
get { return pNumber; }
set { if (value > 0) pNumber = value; }
}
public int _quantity
{
get { return quantity; }
set { if (value > 0) quantity = value; }
}
public string _description
{
get { return description; }
set { if (value != null && value != "") description = value; }
}
public string _unit
{
get { return unit; }
set { if (value != null && value != "") unit = value; }
}
}
public Product(int prN, int qtd, string desc, string un) <- Overloaded public int _[???] <- get/set
__________________
CPUz verified OC
|
||||||||||||
|
|
|
|
|
#6 (permalink) | |||||||||||
|
4.0 GHz
|
No, first I explored visual Basic since I heard that was a good place to start. Right now its Java and C# on and off.
__________________
Future Upgrades:
Jesus is my Savior
Last edited by guitar22891 : 10-28-07 at 12:55 PM. |
|||||||||||
|
|
|
|
|
#7 (permalink) | |||||||||||||
|
AMD Overclocker
|
Just as a suggestion. Try and pick a single language for now. And really focus on it.
|
|||||||||||||
|
|
|
|
|
#8 (permalink) | ||||||||||||
|
News Fiend
|
Java and c# are pretty similar. Just choose one to start with then you can break into the other after you understand the basics better.
__________________As for your question "skip-br" seems to have got what the instructions were asking.
|
||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|