|
Never post your email address in any forum, unless you really like spam! If anyone replies to you, you will receive an email to let you know.
I've removed them from your message.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
|
So im practicing some C# basic concepts and was writing some code to simply calculate the income of a person after asking them to input their name, PayRate and Income and then displaying the income of that same person before and after taxes. However, after the user inputs their hours-per-week the program seems to jump past asking for their PayRate and displays their weekly income before and after taxes, so if someone could point out what i did wrong itd help me out, Thanks! Also the Console.ReadLine() and the console.Read() at the end of the program is to simply keep the command window open so i can check my work since it seems to want to close immediately after the code has been run instead of allowing me a chance to analyze the output so if someone could help me with that too it would be greatly appreciated thanks again.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearningCs
{
class Program
{
static double CalcWeeklyIncome(double hours, double PayRate)
{
double income;
income = hours * PayRate;
return income;
}
static double CalcIncomeAfterTaxes(double income)
{
const double TaxRate = .2333;
income = income - (TaxRate * income);
return income;
}
static void Main(string[] args)
{
double hours, PayRate, Income;
string name;
Console.WriteLine("Please Input your Name: ");
name = Console.ReadLine();
Console.WriteLine("Please input your hours-per-week: ");
hours = Console.Read();
Console.WriteLine("Please input your Hourly Pay Rate: ");
PayRate = Console.Read();
Income = CalcWeeklyIncome(hours, PayRate);
Console.WriteLine(name + "'s income before taxes is " + Income);
CalcIncomeAfterTaxes(Income);
Console.WriteLine("The Income After taxes for " + name + " Is " + Income);
Console.ReadLine();
Console.Read();
}
}
}
|
|
|
|
|
Console.Read returns the character code[^] of the next character from the input. For example, if the user types 3, it will return the number 51 , because that is the ASCII code for the number 3.
If the user types 37, then your code will set hours to 51 (the ASCII code for 3), and PayRate to 55 (the ASCII code for 7). When you reach the second call to Console.Read , there is already a character in the buffer, so it doesn't need to wait for the user.
This is obviously not what you want. Instead, you should use Console.ReadLine to read the entire input until the user presses the Enter key.
You can then use Double.TryParse Method[^] to attempt to convert the string to a number:
string input;
Console.WriteLine("Please input your hours-per-week: ");
input = Console.ReadLine();
while (!double.TryParse(input, out hours))
{
Console.WriteLine("Please enter a valid number: ");
input = Console.ReadLine();
}
You'll then need to repeat that for the hourly rate.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
i think u should use Console.readline for hours-per-week and pay rate , and then convert it to int it will look like this:
name = Console.ReadLine()
Convert.ToInt32(Console.ReadLine());
|
|
|
|
|
Thanks so much guys I used the Convert.ToDouble method to keep the cents part of the value after taxes, I really appreciate the help, I would never have thought of that to be honest and learned a few new concepts thanks to your responses. Thanks Again!
|
|
|
|
|
Convert.ToDouble will throw an exception if the user enters something that can't be converted to a number. Since you don't handle any exceptions, your program will crash.
Use Double.TryParse instead. That way, your program won't crash if the user enters gibberish instead of a number.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
how to detect the event when you move a folder inside another with FileSystemWatcher in c # halgun exemplification of treating this event
|
|
|
|
|
There are lots of examples online of how to use FileSystemWatcher. Have you gone through any of the examples?
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
|
I'm trying to select all users with a roleId of 4. How can I do that? Here's my JSON string:
{
"?xml" : {
"@version" : "1.0",
"@encoding" : "UTF-8"
},
"DataFeed" : {
"@FeedName" : "AdminData",
"People" : [{
"id" : "63",
"active" : "1",
"firstName" : "Joe",
"lastName" : "Schmoe",
"roleIds" : {
"int" : "4"
}
} , {
"id" : "65",
"active" : "1",
"firstName" : "Steve",
"lastName" : "Jobs",
"roleIds" : {
"int" : ["4", "16", "25", "20", "21", "22", "17", "23", "18"]
}
} , {
"id" : "66",
"active" : "1",
"firstName" : "Bill",
"lastName" : "Gates",
"roleIds" : {
"int" : ["3", "16", "25", "20"]
}
}
]
}
}
Here's the query that I'm using:
JObject jsonFeed = JObject.Parse(jsonText);
from people in jsonFeed.SelectTokens("DataFeed.People").SelectMany(i => i.ObjectsOrSelf())
where (int)people["active"] == 1 && (int)people["roleIds.int"] == 4
select new PeopleClass
{
Id = (int)people["id"],
ResAnFName = (string)people["firstName"],
ResAnLName = (string)people["lastName"]
}
I'm getting the following error on (int)people["roleIds.int"] == 4 :
ArgumentNullException: Value cannot be null.<br />
Parameter name: value
In the end, my results should be: Joe Schmoe & Steve Jobs , only.
What am I doing wrong?
modified 21-Sep-16 12:13pm.
|
|
|
|
|
As you said, the value can be an actual integer or an array, when it's an array it attempts to cast the array as an integer and it returns null value, which causes the error. What needs to happen here, you need to check first if it's an array and then search in the array
var roleFour = (from people in json.SelectTokens("DataFeed.People")
.SelectMany(i => i)
let ids = people["roleIds.int"]
where (int) people["active"] == 1 &&
(ids.Type == JTokenType.Array) ?
((int[]) ids.ToObject(typeof(int[]))).Any(k => k == 4) :
(int) ids == 4
select new {
Id = (int) people["id"],
ResAnFName = (string) people["firstName"],
ResAnLName = (string) people["lastName"]
});
|
|
|
|
|
I'm getting the following error:
NullReferenceException: Object reference not set to an instance of an object.
|
|
|
|
|
Can you change the Json source to force the "int" property to an array, so that your first value would be '["1"]'? That would simplify things considerably, especially since your cast is wrong for 2 of the supplied data elements (id 65 and 66 should be (int[])people["roleIds.int"].Contains(4) not (int)people["roleIds.int"] == 4).
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
Sorry. I don't have any control over that. Yes, it makes more sense if it was always an array, but that's what I get from the web service feed.
|
|
|
|
|
You see the attached image file, this very same combobox control when you hover on the number 1 click on the arrow down 1 the same menubar menu, hover in the 2 and 3 show up as the number 1 button, if in the area No. 2 is clicked, the button will be displayed on the No. 1 as we manipulate click items in the combobox, if in the number 3 is clicked will open 1 form, particularly in the region of 2 to add or delete items will be added or removed as combobox manipulation, you know DevExpress controls are in the name of anything? [IMG]http://imagizer.imageshack.com/img923/6672/I3JgLe.jpg[/IMG]
|
|
|
|
|
I've looked at the image, and I can't figure out what you are asking here.
Are you using Windows Forms, and your goal is to add some special behavior to interaction with the standard ComboBox ?
If you are using DevXPress, they have good support forums, I hear.
Please clarify.
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
What is your question?
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
I have this sample FTP code that works fine:
public class SFTP
{
private string _address = "";
private string _userName = "";
private string _password = "";
public SFTP(string address, string userName, string password)
{
_address = address;
_userName = userName;
_password = password;
}
public void DownloadFile(string localFile, string remoteFile)
{
var ftpConnection = GetConnection();
ftpConnection.DownloadFile(localFile, remoteFile);
}
public FTPFile[] GetFileList()
{
var ftpConnection = GetConnection();
FTPFile[] fileDetails = ftpConnection.GetFileInfos();
return fileDetails;
}
public void UploadFile(string localFile, string remoteFile)
{
var ftpConnection = GetConnection();
ftpConnection.UploadFile(localFile, remoteFile);
}
private FTPConnection GetConnection()
{
FTPConnection ftpConnection = new FTPConnection();
ftpConnection.ServerAddress = _address;
ftpConnection.UserName = _userName;
ftpConnection.Password = _password;
ftpConnection.ConnectMode = FTPConnectMode.ACTIVE;
ftpConnection.BytesTransferred += FtpConnection_BytesTransferred;
try
{
ftpConnection.Connect();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
throw;
}
return ftpConnection;
}
private void FtpConnection_BytesTransferred(object sender, BytesTransferredEventArgs e)
{
Console.WriteLine("{0}: {1}", e.RemoteFile, e.ByteCount);
}
}
Now I want to be able to upload or download multiple files at once and show progress for each. Basically this code needs to run seperetly for each file being uploaded or downloaded. What's the right way to implement this?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 20-Sep-16 16:18pm.
|
|
|
|
|
Confused? Why don't you simply do
public void UploadFile(string[] localFiles, string[] remoteFiles)
{
var ftpConnection = GetConnection();
for(int i=0; i<localFiles.Count; i++)
{
ftpConnection.UploadFile(localFiles[i], remoteFiles[i]);
}
}
|
|
|
|
|
The files need to upload/download simultaneously, not one at a time
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Well, then replace for by Parallel.ForEach . First get your filenames into a Dictionary<string, string> , then do
Parallel.ForEach(dictionary, keyValuePair =>
{
ftpConnection.UploadFile(keyValuePair.Key, keyValuePair.Value);
});
|
|
|
|
|
Good idea.. Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
You might also need to consider the server's limit on the number of simultaneous transfers. If you're lucky, your excess transfers will just queue, but I wouldn't count on it.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|