|
Lots of the responses here seem to be WAY over thinking things. 3 classes to hold a double value? Really? It doesn't seem like there is going to be any code in any of those 3 classes, so it should just be a single class with an enum indicating what it is. You aren't going to get around type checking all over the place. If you give us more info on what you are actually doing with the rates, we could provide more help. If its just for formatting, well, you would still have a single class and just override the ToString() that switches on the enum and formats with a % or $. And actually, the $ version should use the system formatter which formats currency according to the local settings.
|
|
|
|
|
SledgeHammer01 wrote: so it should just be a single class with an enum
A boolean would be sufficient.
|
|
|
|
|
I agree that a boolean would work fine, but an enumeration would be a lot easier to read. What if by some chance he has his requirements change and needs to keep track of more than two types for the value? This whole thing could go to hell really quick then. Seems like a lot of over engineering going on here
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
|
|
|
|
|
|
OP already explained what he wanted:
Quote: I have a class that contains a double value. This double could represent a dollar value or it could represent a percentage value. Only one or the other, it will never have both. At runtime, I will need to be able to tell what type of value this is. Here is what I came up with, which appears to work, but if there is a better way, I would prefer that.
He wanted a class that holds a double value and a way to determine the type of data. He thought the GetType() solution was clunky.
|
|
|
|
|
|
Collin Jasnoch wrote: That is not exactly what his post states
Looks to me that that is exactly what the OP said.
|
|
|
|
|
I would be more inclined to use the enum over abstract classes and interface for something like this. Unless there is a deeper level of programming that isn't being shown I would stick with simplicity.
This would be my interpetation of the object you are wanting also with a nice ToRateString() method so you can use the rate object to easily display your value in preformatted string.
Rate r = new Rate(ENRateType.Dollar, 5.58d);
public enum ENRateType { Dollar, Percent }
public class Rate
{
public Rate()
{
RateType = ENRateType.Dollar;
Value = 0d;
}
public Rate(ENRateType rateType, double value) : this()
{
RateType = rateType;
Value = value;
}
public ENRateType RateType {get; internal set;}
public double Value { get; internal set; }
public string ToRateString()
{
if (RateType == ENRateType.Dollar) return "$" + Value.ToString("0.00");
return Value.ToString("0.000") + "%";
}
}
|
|
|
|
|
Trak4Net wrote: stick with simplicity
Same here. At least try to keep things as simple as possible.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
|
|
|
|
|
From what I see there are several ways to do this:
1. Use class/interface inheritance
2. Use an enum
3. Use a boolean
It all depends on what you want, If you want good control over the different types of rates you should use class/interface inheritance, if the dollar rate should have different functionality than say a euro rate (convert between rates etc...)
Example:
interface Rate
{
double getValue();
}
class DollarRate : Rate
{
double rawval;
internal double getValue()
{
return rawval * 6;
}
}
class PercetageRate : Rate
{
double rawval;
internal double getValue()
{
return rawval;
}
}
class EuroRate : Rate
{
double rawval;
internal double getValue()
{
return rawval * 9;
}
}
void Main()
{
if(myTransactionLine.Amount is DollarRate)
{
}
else if(myTransactionLine.Amount is PercetageRate)
{
}
}
If you don't need this then just use the enum approach to save time and space.
I also came up with my own approach, I think it's the fastest one that allows for more than 2 rates (IE the bool method)
It works like the enum approach but instead of enums you use constants. (It's faster because constants are converted to a raw value at compile time)
Example:
static class Rates
{
public const byte DOLLAR = 0;
public const byte PERCENTAGE = 1;
public const byte EURO = 2;
}
class Rate
{
double value;
byte type;
internal double Value
{
get { return value; }
}
internal byte Type
{
get { return type; }
}
}
void Main()
{
if(myTransactionLine.Amount.Type == Rates.DOLLAR)
{
}
else if(myTransactionLine.Amount is Rates.PERCENTAGE)
{
}
}
Hope this helps! Sorry for long post...
|
|
|
|
|
As is often the case with newish people, I don't think you've actually asked the right question. What's your real requirement here? From reading the thread, it appears to be that you want a surcharge, either a percentage or a fixed amount, to be applied to a bill. What's the interface to that?
interface ISurcharge {
double GetAmount(Order order);
string Name { get; }
}
... or, if it's just on the amount, you can pass a number instead of an order. My guess, though, is different tax rates may apply to different items so you will eventually need to pass the order. Note the important thing here: the value of the surcharge depends (in general) on the existing order so it needs to be part of the interface. The Name property is for what gets displayed on the bill; you may want a few other little things like that, but this is the core functionality.
Now you can create trivial implementation classes to solve the problem!
class FixedSurcharge : ISurcharge {
private double amount;
public string Name { get; private set; }
public FixedSurcharge(string name, double amount) { Name = name; this.amount = amount; }
public double GetAmount(Order order) { return amount; }
}
class PercentageSurcharge : ISurcharge {
private double percentage;
public string Name { get; private set; }
public PercentageSurcharge (string name, double percentage) { Name = name; this.percentage = percentage; }
public double GetAmount(Order order) { return percentage * 0.01 * order.TotalValue; }
}
... called like:
void placeOrder(Order order){
foreach(OrderItem item in order.Items) { addBillItem(item.Name, item.Value); }
addSurcharge(new PercentageSurcharge("VAT", 20), order);
addSurcharge(new FixedSurcharge("Shipping", 2.50), order);
}
void addSurcharge(ISurcharge surcharge, Order order){
addBillItem(surcharge.Name, surcharge.GetAmount(order));
}
(Actually, on reflection it may be cleaner to make fake order items, with a Value property, by passing the existing Order to a constructor and stashing it in the Surcharge instance. That's a matter of style, I think.)
|
|
|
|
|
I have a DataGridView on a form which is populated through databinding from a table in my database. I've followed, more or less, the process outlined on this MSDN page[^]
I've implemented the databinding both ways so that, by clicking on an "Update" button, the changes that have been made on the DataGridView is stored back into the DB. All works well, but...
Imagine the table used for the databinding being something like this:
USERS
- UserID (INT)
- UserName (VARCHAR)
- UserType (INT)
And here's the problem, that UserType field references the identifier in a separate table like this one:
USERTYPES
- UserType (INT)
- UserTypeDescription (VARCHAR)
Currently, my databinding is done with a simple query like this:
SELECT * FROM USERS;
The problem is that the value displayed in the third column is an integer which means nothing to the end user. If I wasn't interested in two way databinding but only wanted to display the contents of the table in the DataGridView I could easily have overcome that problem by using the following query:
SELECT UserID, UserName, UserTypeDescription FROM USERS INNER JOIN USERTYPES ON USERS.UserType = USERTYPES.UserType;
Unfortunately I need for the end user to be able to change that value and I'd like to put a ComboBox in all the cells of that column which is populated from the USERTYPES table.
Would really appreciate some advice.
***EDIT***
Maybe I should add that I have some experience with adding a ComboBox (rather a DataGridViewComboBoxCell) in a DataGridView but that has always been to DataGridViews that have not been databound to a table.
Here's how I would've done that:
private void myDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
for (int row = 0; row < (sender as DataGridView).Rows.Count; row++)
{
DataGridViewComboBoxCell newCell = new DataGridViewComboBoxCell();
using (SqlCommand sqlCommand = new SqlCommand("SELECT UserType, UserDescription FROM UserTypes", mySQLConnection))
{
using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
{
DataSet ds1 = new DataSet();
DataTable dt1 = new DataTable();
ds1.Tables.Add(dt1);
ds1.Load(sqlDataReader, LoadOption.PreserveChanges, ds1.Tables[0]);
newCell.DataSource = ds1.Tables[0];
newCell.DisplayMember = "GroupName";
newCell.ValueMember = "GroupID";
}
}
newCell.Value = myDataGridView.Rows[row].Cells["UserType"].Value;
myDataGridView.Rows[row].Cells["UserType"].Value = newCell;
}
}
That final line of code generates an ugly exception which I can only assume is because the DataGridView is bound to a table in the DB because in other projects I've used this code successfully.
The exception that I get is:
Unable to cast object of type 'System.Windows.Forms.DataGridViewComboBoxCell' to type 'System.IConvertible'.Couldn't store <DataGridViewComboBoxCell { ColumnIndex=-1, RowIndex=-1 }> in ConnectGroupID Column. Expected type is Int32.
modified 27-Jul-12 8:37am.
|
|
|
|
|
Hello Folks,
If anyone know code to make gridview as rounded corner, Plz help Me.
Thanks
|
|
|
|
|
It would appear that lots of people do[^].
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hi,
Below link will generate round corner Div. you can apply it for your gridview by little modification.
CSS generator to round corner[^]
Thanks
-Amit
Thanks
-Amit Gajjar (MinterProject)
|
|
|
|
|
I have frmSearchResult and showing it from frmMain like this:
is_form_exists = false;
foreach (Form search_form in this.MdiChildren)
{
if ((string)search_form.Tag == "BROWSE_PATIENTS")
{
search_form.Activate();
is_form_exists = true;
break;
}
}
if (is_form_exists == false)
{
frmSearchResult SearchResultForm = new frmSearchResult(public_var);
SearchResultForm.Tag = "BROWSE_PATIENTS";
SearchResultForm.MdiParent = this;
SearchResultForm.Dock = DockStyle.Fill;
SearchResultForm.find_what = "FILE";
SearchResultForm.find_text1 = (sender as TextEdit).Text.Trim();
SearchResultForm.find_text2 = null;
SearchResultForm.Show();
}
and in frmSearcResult_Activate event I am running a coded to populate data.
My problem now, if the form already exist and I am passing vartiable from my frmMain to it then it will not refresh but it will just activate the frmSearcResult form.
How can I solve this?
|
|
|
|
|
Refresh (repopulate) the data right after the Activate, and before the break.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
how?
my code in frmSearcResult is public like this:
public void browse_result()
{
......
}
but when i try to run it after the
search_form.Activate();
search_form.browse_result();
is_form_exists = true;
break;
It doesn't requires it!
|
|
|
|
|
Means you'll probably have to move the initialization-code from the constructor to some public method, so that you can invoke that method on an existing object.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
I have a question.
How can I pass value to find_what, find_text1and find_text2 in this code:
if ((string)search_form.Tag == "BROWSE_PATIENTS")
{
search_form.Activate();
is_form_exists = true;
break;
}
|
|
|
|
|
jrahma wrote: How can I pass value
Add a parameter to the method;
public class SearchResultForm: Form
{
public void SetSomeStuff(string WHAT, string t1, string t2)
{
this.find_what = WHAT;
this.find_text1 = t1;
this.find_text2 = t2;
}
}
..and it goes like this;
SearchResultForm.SetSomeStuff("FILE", (sender as TextEdit).Text.Trim(), null);
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
How can I att-- title explains.
|
|
|
|
|
I don't think that's possible at all[^] with managed code.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Maybe you get me wrong, what I want to do is that realize what "messages" are windows sending, like spy++
|
|
|
|
|
Member 8551910 wrote: Maybe you get me wrong
I didn't
Member 8551910 wrote: what I want to do is that realize what "messages" are windows sending, like spy++
Spy++ is not written in a managed language.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|