|
Also, for simple timer methods, I like to use anonymous delegates. Like this:
myTimer.Tick += delegate { Invalidate(); };
The invalidate is just the sample code inside the anonymous delegate, you can put anything there.
When you do this, any variables created inside the method that defnes the delegate are in scope, but I like it more because it defines what the timer does at the point that I create it.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hello, I am a beginning C# programming student at a university and I'm having trouble making a previous program of mine modular. We wrote a program that calculated an employee's net pay, and now we have to make it modular by adding 3 methods. I've done just as the examples are in our notes and all I get is an error saying invalid namespace. Here is my original code:
<pre>
using System;
using System.Collections.Generic;
using System.Text;
namespace Program5
{
class Program
{
const double DEPINS = 25.50;
const double TAX = .23;
static void Main()
{
int dep;
double grossPay, hourRate, hourWorked, deduct, netPay, depIns, tax;
Console.Write("Hours Worked: ");
hourWorked = Double.Parse(Console.ReadLine());
Console.Write("Hourly Rate: ");
hourRate = Double.Parse(Console.ReadLine());
Console.Write("Dependents: ");
dep = Int32.Parse(Console.ReadLine());
if (hourWorked >= 40)
grossPay = Math.Round(1.5 * hourRate * hourWorked, 2);
else
grossPay = Math.Round(hourRate * hourWorked, 2);
depIns = dep * DEPINS;
tax = grossPay * TAX;
deduct = Math.Round(depIns + tax, 2);
netPay = grossPay - deduct;
Console.WriteLine("");
Console.WriteLine("Hours Worked: {0,9:n}", hourWorked);
Console.WriteLine("Hourly Rate: {0,9:n}", hourRate);
Console.WriteLine("Dependents: {0,9}", dep);
Console.WriteLine("Gross Pay: {0,9:n}", grossPay);
Console.WriteLine("Deductions: {0,9:n}", deduct);
Console.WriteLine(" ---------");
Console.WriteLine("Net Pay: {0,9:c}", netPay);
}
}
}
</pre>
Thanks!
|
|
|
|
|
If I cut and paste your example code it compiles fine. What are you using to compile the code? Does it say which namespace is invalid? Or a line number?
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Yeah, it compiles fine because I copy and pasted the original coding without what I put in when I tried to make it modular. What I tried to do looked something like this:
<pre>
using System;
using System.Collections.Generic;
using System.Text;
namespace Program5
{
class Program
{
const double DEPINS = 25.50;
const double TAX = .23;
static void GetData()
{
int dep;
double grossPay, hourRate, hourWorked, deduct, netPay, depIns, tax;
Console.Write("Hours Worked: ");
hourWorked = Double.Parse(Console.ReadLine());
Console.Write("Hourly Rate: ");
hourRate = Double.Parse(Console.ReadLine());
Console.Write("Dependents: ");
dep = Int32.Parse(Console.ReadLine());
}
static void CalcNet()
{
if (hourWorked >= 40)
grossPay = Math.Round(1.5 * hourRate * hourWorked, 2);
else
grossPay = Math.Round(hourRate * hourWorked, 2);
depIns = dep * DEPINS;
tax = grossPay * TAX;
deduct = Math.Round(depIns + tax, 2);
netPay = grossPay - deduct;
}
static void DspData()
{
Console.WriteLine("");
Console.WriteLine("Hours Worked: {0,9:n}", hourWorked);
Console.WriteLine("Hourly Rate: {0,9:n}", hourRate);
Console.WriteLine("Dependents: {0,9}", dep);
Console.WriteLine("Gross Pay: {0,9:n}", grossPay);
Console.WriteLine("Deductions: {0,9:n}", deduct);
Console.WriteLine(" ---------");
Console.WriteLine("Net Pay: {0,9:c}", netPay);
}
static void Main ()
{
GetData();
CalcNet();
DspData();
}
</pre>
So what I did is I set it up exactly like it was set up in an example program from my notes which I copy and pasted and compiled and that worked just fine, but when I do it with this program it says my namespace is invalid and that I need a semi-colon after the static void's which I know is incorrect. So I have no idea what is going on.
|
|
|
|
|
in the above code you should get a ton of errors and not just invalid namespace.
you have defined variables like hourWorked, hourRate etc in the function "GetData()" and try to access it from other functions. those variables will have scope in only GetData() function and hence are not accessible in other functions.
so, either you can pass the variables as arguments to the functions or do not make your functions static.
hope this helps.
regards
|
|
|
|
|
Okay, I fixed the defined variables...but now I get a bunch of errors saying invalid "{"'s and "}"'s after the methods and stuff plus "expected class, delegate, enum, interface, or struct" for the voids. And this is what I've got for coding:
using System;
using System.Collections.Generic;
using System.Text;
namespace Program5
{
class Program
{
const double DEPINS = 25.50;
const double TAX = .23;
int dep;
double grossPay, hourRate, hourWorked, deduct, netPay, depIns, tax;
static void getData()
{
Console.Write("Hours Worked: ");
hourWorked = Double.Parse(Console.ReadLine());
Console.Write("Hourly Rate: ");
hourRate = Double.Parse(Console.ReadLine());
Console.Write("Dependents: ");
dep = Int32.Parse(Console.ReadLine());
}
static void calcNet()
{
if (hourWorked >= 40)
grossPay = Math.Round(1.5 * hourRate * hourWorked, 2);
else
grossPay = Math.Round(hourRate * hourWorked, 2);
depIns = dep * DEPINS;
tax = grossPay * TAX;
deduct = Math.Round(depIns + tax, 2);
netPay = grossPay - deduct;
}
static void dspData();
{
Console.WriteLine("");
Console.WriteLine("Hours Worked: {0,9:n}", hourWorked);
Console.WriteLine("Hourly Rate: {0,9:n}", hourRate);
Console.WriteLine("Dependents: {0,9}", dep);
Console.WriteLine("Gross Pay: {0,9:n}", grossPay);
Console.WriteLine("Deductions: {0,9:n}", deduct);
Console.WriteLine(" ---------");
Console.WriteLine("Net Pay: {0,9:c}", netPay);
}
static void Main();
{
getData()
calcNet()
dspData()
}
}
}
|
|
|
|
|
this still has problems.
murtle3 wrote: static void dspData();
remove ';' after the function name.
murtle3 wrote: static void Main();
{
getData()
calcNet()
dspData()
}
remove ';' after Main() and add ';' after all the function calls.
after all this is done you should note that you are trying to access the class variables from static method. this is not possible.
a static method can access static variables. what you should do is make the variables static (not a good way of programming), but it should help you run the program.
instead of:
murtle3 wrote: int dep;
double grossPay, hourRate, hourWorked, deduct, netPay, depIns, tax;
try the following:
static int dep;
static double grossPay, hourRate, hourWorked, deduct, netPay, depIns, tax;
hope this helps.
regards
|
|
|
|
|
Alright, I got it to work. Thanks a lot guys for your help!
|
|
|
|
|
You should use tryparse to read lnes, in case someone enters text instead of a number. You have not pasted the whole program, b/c there's nothing modular about this, it's all one method.
murtle3 wrote: nt dep;
double grossPay, hourRate, hourWorked, deduct, netPay, depIns, tax;
This is bad practice, declare variables as you give them values, not in a bunch at the top. If your teacher tells you to do it this way, tell him that C required this, but C# does not, and doing it only increases the scope of your variables, making debugging harder.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi,
I have a RichTextBox with some information about colors in the text stored as Rtf. Now I'd like to remove for example first 1000 chars from the text without loosing information about colors in rest of the text. Is it possible?
Thanks
Pawel
|
|
|
|
|
You should be able to use the Select[^] method to select the text you want to remove, then set the SelectedText[^] property to String.Empty (or "").
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Thanks. Works fine.
Pawel
|
|
|
|
|
You can set the path to the defragger under the following registry key:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\MyComputer\DefragPath
The default value specifies the path and usually defaults to %systemroot%\system32\dfrg.msc %c:
You can see how Windows registers it's defragger in the file C:\Windows\Inf\dfrg.inf
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Hi,
Thanks Tom, much appreciated.
Andrew.
|
|
|
|
|
Can someone help me understand why sr is being viewed as a local variable to the if statement in the finally clause.
the exact error I'm receiving is: "Use of unassigned local variable 'sr'"
As I said, everything is fine until the finally clause.
Thanks for the help!
ArrayList files;
StreamReader sr;
try {
sr = new StreamReader(args[0]);
do {
files.Add(sr.ReadLine());
} while (sr.Peek() > 0);
}
catch (Exception ex) {
MessageBox.Show(ApplaunchErr + "reading the .cfg file!\n" + AppClosing, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
finally {
if(!(sr == null))
sr.Close();
}
"In the middle of difficulty lies opportunity." Albert Einstein
|
|
|
|
|
You need to explicitly set it to null and create a new instance of the ArrayList like so:
ArrayList files = new ArrayList();
StreamReader sr = null;
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Payrok wrote: Can someone help me understand why sr is being viewed as a local variable to the if statement in the finally clause.
It's not.
Payrok wrote: the exact error I'm receiving is: "Use of unassigned local variable 'sr'"
That's because the code preceding that statement doesn't guarantee that the variable has been assigned a value. If the creation of the StreamReader causes an exception, the code will exit the try block before any value has been assigned to the variable.
---
"Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things."
-- Douglas Adams
|
|
|
|
|
I'm not going to comment on the problem because you have already received two perfectly good answers. I do have a question though. Why do you not use if (sr != null) instead of if (!(sr == null)) ? The first is more readable.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Thanks for bringing that to my attention. There's a good reason for it actually, and it's because I do a lot more VB.Net development than C#. I'm more used to having to do stupid things like: if Not sr is nothing then ... end if
So it affects the way I structure things in C#. Thanks for the replies, I never realized I had to set an object to null like that to keep it in scope.
"In the middle of difficulty lies opportunity." Albert Einstein
|
|
|
|
|
The variable is not out of scope, it just was not initialized.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
It's not really keeping it in scope. It's just that the scoping of the compiler means that it can't really do an operation in a block on a variable if it thinks that it hasn't been initialised. I've lost count of the number of times I've been kicked in the butt by this one.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hi,
Is it possible, my means of registry entries etc to launch the default Defragmenter in Windows. For example, users of OO Defrag can set this as the default, and can be launched as the default instead of the Windows defragmenter.
Thanks, Andrew.
|
|
|
|
|
Hi again... always fighting with my installation...
Now everything works fine... I only have 1 other little detail: I'd like to let the end user choose whether install or not SQL Server Express.
If I put it in the prerequisites and the users cancel the install of SQL Server. also my app doesn't get installed...
Is there a way? I googled around but seems like it's still not possible...
Life is not short... the problem is only how you organize yourself
|
|
|
|
|
Hi all,
I'm working on client-server application
While sending data from server through Socket class,
my client calls the callback function correctly but the problem rises when it gets hang at Client.Receive(buffer[])
in debug mode, when i repost message from server to client, it resumes the code from Client.Receive
what should be the reason, any idea?
thanks
Determination and faith are the only keys !
|
|
|
|
|
nayabsiddiqi wrote: but the problem rises when it gets hang at Client.Receive(buffer[])
nayabsiddiqi wrote: what should be the reason, any idea?
If you are making a blocking call then it is working perfectly.
|
|
|
|
|