|
What is the question (it is certainly not a line of emojis)?
|
|
|
|
|
Can this code fragment be serialized Sir@Richard MacCutchan?
|
|
|
|
|
That makes no sense. Please explain in detail what problem you are trying to solve. Perhaps the word serialize means something different to you.
|
|
|
|
|
The process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file is Serialization in C# right? i dont have an idea how to do it though...
|
|
|
|
|
|
even i was confused i already did that unfortunately i failed to link it with that.. in the articles its always showing a class that fragment is out of nowhere my prof is not around i don't know what to do.. any suggestions? anyway thanks for answering
|
|
|
|
|
Follow the link I gave you, there is a reference to the MSDN documentation and also a CodeProject article.
|
|
|
|
|
I have followed WPF TabControl: Turning Off Tab Virtualization[^] to be able to have tabs that don't recreate their contents each time.
Recently I found out that if you close a tab for example the content is not destroyed!
For example if you add a browser with a video in it the video will continue to play.
The content seems to be stored in a property, I can't figure out how to remove it for good.
This is done with data binding and a DataTemplateSelector.
Any help would be greatly appreciated.
|
|
|
|
|
You should ask this on the article forum. The author will be notified of questions.
|
|
|
|
|
I asked, but I don't see much activity there recently. So I know that the content is stored in a property and I would just like a generic wpf answer how to remove content (UI controls) that are stored in a property.
|
|
|
|
|
I need to edit code to add in C# code for a method called readInteger which has the following signature:
int readInteger ( string prompt )
but I don't know how; can anyone tell me the code I would use?
thanks
|
|
|
|
|
There is a "ReadLine" method on the Console class. ReadInteger can be found on various reader-classes, but could also be from some other class.
Given that it needs a string as a prompt, I would guess that it is a bit o' homework where you display the prompt on the console, ask for a valid integer, provide feedback if it isn't, and then return the result.
If it is homework then please copy the entire question.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
The program presently performs the number reading for the item price inside the main loop. Improve the structure of the program by writing the C# code for a method called readInteger which has the following signature:
int readInteger ( string prompt )
Modify the program to use this method.
class PriceCalc { static void Main(string[] args)
{
float costPrice = 0.0f;
float profit;
float tax; float totalCost = 0.0f;
float totalProfit = 0.0f;
float totalTax = 0.0f;
bool finished = false;
float salePrice;
string priceString;
string nameString; float taxRate = 0.2f;
while (!finished) {
Console.Write("Enter the name of the product : ");
nameString = Console.ReadLine(); Console.WriteLine("Product is " + nameString);
if (nameString.Length != 0) {
finished = true;
} else
{ bool dataOK = false;
while (!dataOK)
{
Console.Write("Enter price of the product : ");
priceString = Console.ReadLine(); try {
costPrice = float.ParsepriceString); dataOK =true; catch
{
Console.WriteLine("That is not a valid number");
Console.WriteLine("Enter a number, e.g. 15.5");
}
}
if (costPrice < 20.0f) {
profit = costPrice * 0.2f;
} else { if (costPrice < 40.0f) {
profit = costPrice * 0.15f; // } else
{
profit = costPrice * 0.1f;
}
}
tax = (costPrice + profit) * taxRate;
totalTax = totalTax + tax; totalCost = totalCost + costPrice; totalProfit = totalProfit + profit;
salePrice = costPrice + profit + tax;
|
|
|
|
|
Ask and ye receive
The indentation is screwed up by the browser due to the formatting. I've cleaned it up and added it below in pre-tags;
using System;
class PriceCalc
{
static void Main(string[] args)
{
float costPrice = 0.0f;
float profit;
float tax;
float totalCost = 0.0f;
float totalProfit = 0.0f;
float totalTax = 0.0f;
bool finished = false;
float salePrice;
string priceString;
string nameString;
float taxRate = 0.2f;
while (!finished)
{
Console.Write("Enter the name of the product : ");
nameString = Console.ReadLine();
Console.WriteLine("Product is " + nameString);
if (nameString.Length != 0)
{
finished = true;
}
else
{
bool dataOK = false;
while (!dataOK)
{
Console.Write("Enter price of the product : ");
priceString = Console.ReadLine();
try
{
costPrice = float.Parse(priceString);
dataOK = true;
}
catch
{
Console.WriteLine("That is not a valid number");
Console.WriteLine("Enter a number, e.g. 15.5");
}
}
if (costPrice < 20.0f)
{
profit = costPrice * 0.2f;
}
else
{
if (costPrice < 40.0f)
{
profit = costPrice * 0.15f;
}
else
{
profit = costPrice * 0.1f;
}
}
tax = (costPrice + profit) * taxRate;
totalTax = totalTax + tax;
totalCost = totalCost + costPrice;
totalProfit = totalProfit + profit;
salePrice = costPrice + profit + tax;
}
}
Console.ReadKey();
}
}
At this moment, your entire program is a single method, and it would make it harder to reuse parts. If you would write a larger application like this, you'd end up with a lot of repeating and hard to maintain code.
The line "static void Main(string[] args)" is an example of a methods signature. It states the methods argument(s) and its name. Adding such a method to the code would give a different structure;
class PriceCalc
{
static int readInteger (string prompt)
{
Console.WriteLine(prompt);
priceString = Console.ReadLine();
return priceString;
}
static void Main(string[] args)
{
float costPrice = 0.0f;
float profit;
..and so on
You'd then be able to call this new method from your main-method like below;
Console.Write("Enter price of the product : ");
priceString = Console.ReadLine();
priceString = readInteger("Enter price of the product : "); Now you have a new design-decision to make; will you implement the error-handling in the new method, or would you keep it in the main-method?
Some additional notes;
--edit;
Nearly forgot an important part, without which the code will not execute; you need to add the word "static" to the methods signature like below, or it won't work (updated the example above also);
static int readInteger (string prompt) If you are not allowed to change the signature, then you'd additionally need to create a class, and create an object of that type before using it.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
thank you so much! that's so helpful!!!
|
|
|
|
|
You're welcome, and thanks
|
|
|
|
|
Eddy,
This doesn't quite make sense.
You have a method whose signature says it is returning int , but you are returning a string !
I think you need to capture more of the original code into the method:
(Including your note about int vs. float )
static float readInteger (string prompt)
{
do
{
Console.Write(prompt);
float value;
if (float.TryParse(Console.ReadLine(), out value))
return value;
Console.WriteLine("That is not a valid number");
Console.WriteLine("Enter a number, e.g. 15.5");
} while (true);
}
And the usage would be like:
if (nameString.Length != 0)
{
finished = true;
}
else
{
costPrice = readInteger("Enter price of the product : ");
if (costPrice < 20.0f)
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed."
- G.K. Chesterton
|
|
|
|
|
Matt T Heffron wrote: You have a method whose signature says it is returning int , but you
are returning a string ! Should result in a compiler-error on the return statement; I did not want to add the code to do the conversion.
I would still recommend using integers, not floats. You cannot pay half a cent AFAIK, and it would eliminate rounding errors
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Matt T Heffron wrote: You have a method whose signature says it is returning int , but you
are returning a string ! Should result in a compiler-error on the return statement; I did not want to add the code to do the conversion.
I would still recommend using integers, not floats. You cannot pay half a cent AFAIK, and it would eliminate rounding errors
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: I would still recommend using integers, not floats. You cannot pay half a cent AFAIK, and it would eliminate rounding errors
Of course, this is exactly what the decimal data type is for!
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed."
- G.K. Chesterton
|
|
|
|
|
Matt T Heffron wrote: Of course, this is exactly what the decimal data type is for! "Exactly"? To quote MSDN*;
Compared to floating-point types, the decimal type has more precision and a smaller range, which makes it appropriate for financial and monetary calculations. Treating the items as integers in monetary applications prevents a lot of common errors, like wrong boxing/unboxing, rounding errors, wrong conversions and/or picking an inappropriate datatype. If you count whole items, there is no rounding, and cents only come in whole-variants.
*) MSDN agrees, I don't. Show me 0.000783678 cents and I might change my mind
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: Show me 0.000783678 cents and I might change my mind
The last time I filled up my gas tank it was $3.499/gallon (349.9 cents/gallon)
And, consider why 25cents is called "two-bits": Bit (money)[^] ...
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed."
- G.K. Chesterton
|
|
|
|
|
Matt T Heffron wrote: 349.9 cents/gallon Which means you get a cent discount if you buy ten; does not mean that a tenth of a cent exists.
That is confusing physical objects (stuff) with claims (I will be getting a cent discount).
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
In the US, the 1/10th of a cent unit is still legally defined and used in some contexts.
Mill (currency)[^]
In any case, the point is that accurate financial arithmetic in units of less than a cent is still a requirement in some cases and using only integer cents can still lead to difficulties.
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed."
- G.K. Chesterton
|
|
|
|
|
Deserves 5 for effort - quiet day over there!
Never underestimate the power of human stupidity
RAH
|
|
|
|