|
When I started learning about C#, I noticed the sample programs would use the funky argument-substitution notation inside Console.WriteLine , similar to printf in C:
int a = 17;
string b = "something";
Console.WriteLine("a = {0} and b = {1}", a, b);
When I finally got around to writing some code, I decided to stick with the Java approach of simply contatenating everything, which to me seems easier to read and less error prone:
Console.WriteLine("a = " + a + " and b = " + b);
I'd like to get some opinions on the pros and cons of each approach.
Why does Microsoft seem to promote the first technique? The thing is, you look at other WriteLine s, such as Debug.WriteLine and Trace.WriteLine and they don't take a variable number of arguments, so you can't use the argument-substitution technique there.
Thanks,
Alvaro
When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com
|
|
|
|
|
I usually use the second one
Now a days... computers are so fast it doesn't matter how you do it, as long as it works. That's why I use the easier way.
But sometimes it helps to know how to use the first one, because in the first method it is easier to read the code, and modify it later.
|
|
|
|
|
Alvaro Mendez wrote:
I'd like to get some opinions on the pros and cons of each approach.
By using the argument substitution you can add additional formatting to the WriteLine call (such as pad numbers or strings) which you may or may not be able to do the other way, depending on how the ToString method is written for each variable's type.
Another advantage is if you are localizing your application you only have one string to replace instead of 2 for your example.
As you point out the con is that you aren't placing the variable right there so you need to reference back and forth to get the whole picture.
Alvaro Mendez wrote:
as Debug.WriteLine and Trace.WriteLine
A shame too, I find myself having to do: string.Format inside of the WriteLine method
James
"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation
|
|
|
|
|
I prefer the first method since it makes the code far more readable. I'm not too sure as to whether one is more efficient than the other, it'd be interesting to see though.
I tend to use String.Format("{0}: {1}") etc. when using it with a method that doesn't directly support it.
--
Paul
"Put the key of despair into the lock of apathy. Turn the knob of mediocrity slowly and open the gates of despondency - welcome to a day in the average office."
- David Brent, from "The Office"
MS Messenger: paul@oobaloo.co.uk
|
|
|
|
|
The first way (using String.Format essentially) is a little more useful i think under a scenario like the following
Console.WriteLine("a = {0} and a = {0} and b = {1}", a, b);
vs
Console.WriteLine("a = " + a + " and a = " + a + " and b = " + b);
|
|
|
|
|
Thought I'll add me bit as well. I like former approach more, because you can do this:
Console.Write("Matches for '{1,1}': {0,-10}\t{2,10:f3}ms\t", count, b, Avg(contains));
I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02
|
|
|
|
|
How can I add or subtract value from DateTime,like this:
mydatetime = DateTime.Now – SixHour
Or
Mydatetime = DateTime.Now - SixHour and Half Minutes
Mazy
"And the carpet needs a haircut, and the spotlight looks like a prison break
And the telephone's out of cigarettes, and the balcony is on the make
And the piano has been drinking, the piano has been drinking...not me...not me-Tom Waits
|
|
|
|
|
See the AddHours method[^] or the Subtraction operator[^]. Both will do nicely.
And don't forget to visit the TimeSpan [^]object.
Try:
DateTime nm6 = DateTime.Now - new TimeSpan (6, 0, 0);
α.γεεκ Fortune passes everywhere. Duke Leto Atreides
|
|
|
|
|
Thanks.
Mazy
"And the carpet needs a haircut, and the spotlight looks like a prison break
And the telephone's out of cigarettes, and the balcony is on the make
And the piano has been drinking, the piano has been drinking...not me...not me-Tom Waits
|
|
|
|
|
I save files from my application with an extention. I have two problem now:
1.I want when user double click on that file my application open and show the proper datas from that file.
2.When user save files ,it saves with my application icon,now its save with unknown window icon.
Mazy
"And the carpet needs a haircut, and the spotlight looks like a prison break
And the telephone's out of cigarettes, and the balcony is on the make
And the piano has been drinking, the piano has been drinking...not me...not me-Tom Waits
|
|
|
|
|
Mazdak wrote:
2.When user save files ,it saves with my application icon,now its save with unknown window icon.
There is quite a bit involved to do this using .net.
Take a look at this usenet post on microsoft.public.dotnet.languages.csharp. It should get you rolling.
Hey don't worry, I can handle it. I took something. I can see things no one else can see. Why are you dressed like that?
- Jack Burton
|
|
|
|
|
Thanks.
Well I found first one.I get it from command line.
Mazy
"And the carpet needs a haircut, and the spotlight looks like a prison break
And the telephone's out of cigarettes, and the balcony is on the make
And the piano has been drinking, the piano has been drinking...not me...not me-Tom Waits
|
|
|
|
|
I am writing a utility to check the health of an application and I would like to check if a particular Windows service is installed and if it is whether it is running or not.
I have used the System.ServiceProcess.ServiceController class to see if a service is running or not (and this works fine) but I can't find a way to trap if the service is actually installed.
I could interate through the ServiceController[] using GetServices but this seems to be a bit "clunky" particularly becauseI want to check the existence of a number of services as the utility loads.
Is there a class/method that will do this for me?
Anyone got any ideas on how to do it?
Thanks for your help
Andy
|
|
|
|
|
I don't know if I understand your problem.When you can get list of services,check the name of them if your proper names are there so they are installed,whats the problem with that?
Mazy
"And the carpet needs a haircut, and the spotlight looks like a prison break
And the telephone's out of cigarettes, and the balcony is on the make
And the piano has been drinking, the piano has been drinking...not me...not me-Tom Waits
|
|
|
|
|
Hi Mazy,
Thanks for the reply.
It just seems to be a lot of work when I would have expected an exception to be returned by ServiceController to say that the service is not installed.
If you use VBA and the windows API "advapi32.dll" the QueryServiceStatus method returns an error that shows if the service is installed - surely C# has an equivalent - maybe not.
Andy
|
|
|
|
|
Well,When I want to do that I just look up in ServiceController[] which used GetServices().I look just check each ServiceName property for ServiceController[] and if its there so it is installed.Its not that muck hard or caused any expetion.And I can get the status of each service by Status prperty.
Anyway,You can use QueryServiceStatus in C# too.You can use it like the way you use other win32 functions in C# but thats very hard and boring.You can write a DLL with MC++(I GUESS you can do that with VB.NET too) and do this jon in it and then it is REALLY easy to reuse it in C#.If you want to do it in this way I can help you more.
I don't think you have any other choices,there is nothing like that in .Net framework as much as I know.
HTH
Mazy
"And the carpet needs a haircut, and the spotlight looks like a prison break
And the telephone's out of cigarettes, and the balcony is on the make
And the piano has been drinking, the piano has been drinking...not me...not me-Tom Waits
|
|
|
|
|
Mazy,
Many thanks for your advice.
You have confirmed my thoughts, it just seems odd that when someone has written a class that gets a service status that one of the status conditions would not be "its not installed".
I am going to opt for using ServiceConroller[].
Regards
Andy
|
|
|
|
|
I completely agree with you. Worse there doesn't seem to be any native .NET way of getting all the information you could normally get using QueryServiceStatus() that is returned in the SERVICE_STATUS structure. .NET needs to replace the ServiceStatus enum with a class.
|
|
|
|
|
This works for me..
public bool IsInstalled()
{
try
{
string str;
ServiceController sc = new ServiceController(myServiceName);
// accessing some property will throw an exception if not installed.
str = sc.ServiceName;
}
catch (Exception)
{
return false;
}
return true;
}
|
|
|
|
|
I've written a tray based application and overridden the OnClosing where I iconify the application instead of closing it. However, when I'm trying to shut down or restart my computer the program wont shut down and the computer wont reboot or shutdown until I exit the program. I know that WIndows sends the message WM_ENDSESSION when the system goes down, but how do I handle this from my hidden application in the traybar? Or is there another simple solution of this problem 
|
|
|
|
|
Is that the only method (OnClosing) you wrote an override for?
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
I've overridden OnLoad, OnResize, Dispose (that's default?) and OnClosing.
|
|
|
|
|
I hate to say it, but that's just weird. I have a similar app except minimize (rather than close) puts it in the tray and it doesn't seem to have any problem at all. So there may be something about the OnClosing method that I'm not aware of. Does your code look like this?:
protected void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
return;
}
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
Is this a normal app with a tray icon, or a system service, because surely the app would shut down normally as any windows form app would. The only thing I can think of is that you havn't disposed the NotifyIcon in the Dispose Override.
If its a system service then I can't help, never written one in C#.
"If you just say porn then you get all manner of chaff and low grade stuff." - Paul Watson, Lounge 25 Mar 03 "But a fresh install - it's like having clean sheets" - C. Maunder Lounge 3 Mar '03
Jonathan 'nonny' Newman
Homepage [www.nonny.com] [^]
|
|
|
|
|
Somewhere on CP there is an article you can use in the Closing event to determine why you are being closed. Use that to handle the case where windows is shutting down to exit your program.
or
pahlsson wrote:
I know that WIndows sends the message WM_ENDSESSION
Your application is still running so it should still get the WM_ENDSESSION message. You'll need to lookup the real value of WM_ENDSESSION, then override WndProc to handle it.
protected override void WndProc(ref Message m)
{
const int WM_ENDSESSION = 0x0016;
switch(m.Msg)
{
case WM_ENDSESSION:
bool sessionEnding = (bool) m.WParam;
m.Result = 0;
break;
default:
base.WndProc(ref m);
}
} However, MSDN says "The application need not call the DestroyWindow or PostQuitMessage function when the session is ending." so I don't think this is a suitable addition without my first suggestion implemented.
James
"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation
|
|
|
|