|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
C# - Throwing an ArgumentOutOfRangeException
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||
|
4.0 GHz
|
Hey, working on a C# programming project and part of it is to:
Quote:
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace ProductMod
{
public abstract class Product
{
private int productNum;
private float quantity;
private string description, unitOfMeasure;
private const string DEF_DESC = "no description";
private const string DEF_UM = "no unit of measure";
public Product()
{
productNum = 0;
quantity = 0;
description = DEF_DESC;
unitOfMeasure = DEF_UM;
}// default constructor for each value
public Product(int prN, float qtd, string desc, string un)
{
productNum = prN;
quantity = qtd;
if (desc != null && desc != "") // property method
description = desc;
else
description = DEF_DESC;
if (desc != null && un != "")// property method
unitOfMeasure = un;
else
unitOfMeasure = DEF_UM;
}//overload constructor for each variable
public abstract double calcStdCost();
// abstract calcStdCost method
public override string ToString()
{
return
productNum + " " +
quantity + " " +
description + " " +
unitOfMeasure;
}// public method tostring that overides ToString method of Object class
public int ProductNumber
{
get
{
return productNum;
}
set
{
if (value > 0)
productNum = value;
// only update if value is greater than 0
}
}
public float HandOnQuantity
{
get
{
return quantity;
}
set
{
if (value > 0) quantity = value;
// only update if value is greater than 0
}
}
public string Description
{
get
{
return description;
}
set
{
if (value != null && value != "") description = value;
// only update if not blank
}
}
public string UnitOfMeasure
{
get
{
return unitOfMeasure;
}
set
{
if (value != null && value != "") unitOfMeasure = value;
// only update if not blank
}
}
}
}
__________________
Future Upgrades:
Jesus is my Savior
|
||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
Bifford
![]() |
I would use an if statement and then "throw" a specific error depending on the situation. You might even create a private void that throws the error (since it will be reused).
__________________
Helpful Posts (Hopefully )Overclocker's Calculator Photo Editing - B&W w/Color Accents
|
|||||||||||||
|
|
|
|
#3 (permalink) | |||||||||||
|
4.0 GHz
|
hmm.. can you give me an example for a?
__________________
Future Upgrades:
Jesus is my Savior
|
|||||||||||
|
|
|
|
|
#4 (permalink) | ||||||||||||
|
Programmer
|
He means use the if statement to check for your error inside a try/catch. Then if the if statement is true then do something allong the line of
Code:
throw new ArgumentOutOfRangeException("Your error statement");
__________________
From American Dad: I touched her hand... her hand touched her boob. By the transitive property, I got some boob! Algebra's awesome!
|
||||||||||||
|
|
|
|
#5 (permalink) | ||||||||||||
|
Programmer
|
U plugged that all in and I got no exceptions... were you using it in another method? Just the declarations like that shouldn't cause an error...
__________________
"If there is a god, I hope he has a good excuse" Woody Allen
|
||||||||||||
|
|
|
|
|
#6 (permalink) | |||||||||||||
|
Programmer
|
Quote:
Also, I haven't had time to check the code I put above but it should work. If not you have to do something very similar and you can find it by googling "c# force throw error" or something like that
__________________
From American Dad: I touched her hand... her hand touched her boob. By the transitive property, I got some boob! Algebra's awesome!
|
|||||||||||||
|
|
|
|
#7 (permalink) | |||||||||||
|
4.0 GHz
|
Where would I throw the exception though?
Could someone give me an example? Help?
__________________
Future Upgrades:
Jesus is my Savior
|
|||||||||||
|
|
|
|
|
#8 (permalink) | ||||||||||||
|
IDDQD
|
Here's a quick example. It doesn't really apply to your code, and it also probably isn't correct for syntax (I only code Java/C), but you get the idea of how they work.
Code:
public int checkProductStock(int prN)
{
int[] product = {12345,21314,12312};
if (prN > -1 && prN < 3)
return product[prN];
else
throw new ArgumentOutOfRangeException();
}
Code:
try{
qtyRemaining = checkProductStock(prodNum);
}catch(ArgumentOutOfRangeException() ex){
//do whatever task you want to react to the invalid number
}
__________________
|
||||||||||||
|
|
|
|
#9 (permalink) | |||||||||||
|
4.0 GHz
|
THanks!! This makes a lot more sense. Now for my code should I throw the exception in the constructor, overload constructor, or the get / set??
This is what I'm thinking: Code:
public int ProductNumber
{
get
{
return productNum;
}
set
{
if (value <= 0)
throw new ArgumentOutOfRangeException
("Argument for Product Number is 0 or Less");
else
productNum = value;
}
}
__________________
Future Upgrades:
Jesus is my Savior
Last edited by guitar22891 : 11-18-07 at 06:05 PM. |
|||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|