|
C++ allows to use default values for parameters e.g.
void MyFun(int nParam1, nDefault = 0);
Is it possible in C#? If yes, how to do it?
|
|
|
|
|
void MyFun(int nParam1)
{
MyFun(nParam1, 0);
}
void MyFun(int nParam1, int nDefault)
{
}
In other words, No...
|
|
|
|
|
Clever
|
|
|
|
|
Default parameters are not inherently supported in C#. You need to define an overloaded version of the method that doesn't require the parameter (that defaults it) and call the version that contains all the parameters. Before you say it, I agree that it sucks
Cheers,
Tom Archer
Author, Inside C#
A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.
|
|
|
|
|
How could they leave out something so trivial to impliment, and so useful ? Templates I can understand given that Microsoft are yet to release a worthwhile implimentation of templates on any platform, but this just blows.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
|
|
|
|
|
we've had many talks with Eric Gunnerson about this on the aspfriends.com email lists.
you may disagree with it...
but the reason they didn't implement it was because they favored ( by a small margin ) overall simplicity against More Features©
however, we have been fairly successfull at giving good arguments for it... and they are still considering it for v2.
|
|
|
|
|
lol, beat me to it
James
Simplicity Rules!
|
|
|
|
|
The more I use C#, the more it treats me like an idiot. It's clear to me they went for simplicity, for the benefit of people coming over from VB, I suspect.
Thanks for the insight.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
|
|
|
|
|
This is actually not the case. According to the C# Product Manager it had more to do with versioning than anything.
Cheers,
Tom Archer
Author, Inside C#
Author, Visual C++.NET Bible
A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af
|
|
|
|
|
|
It's a percieved issue with distributed libraries ? Given that people are talking about doing the same thing via two functions, the hard coded values are still there, we're just finding ways around the limitation in C#.
But of course, I should be telling Joe this
Thanks for the info. BTW, Amazon shipping my copy of Inside C# on the weekend, so the delay was not that bad. Soon I will actually start learning me some C# instead of flailing around blindly. The Petzold book is helpful when I have a UI issue, but the other book I have, I have quickly outgrown.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
|
|
|
|
|
Christian Graus wrote:
It's a percieved issue with distributed libraries ? Given that people are talking about doing the same thing via two functions, the hard coded values are still there, we're just finding ways around the limitation in C#.
Agreed. There were many situations where the reason for omitting something from C# that most C++ developers would consider important was due to versioning
Christian Graus wrote:
But of course, I should be telling Joe this
I'm sure he's heard it
Christian Graus wrote:
BTW, Amazon shipping my copy of Inside C# on the weekend, so the delay was not that bad
Very cool! I'll be here for any questions.
Cheers,
Tom Archer
Author, Inside C#
Author, Visual C++.NET Bible
A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af
|
|
|
|
|
|
Here is what Eric Gunnerson had to say about the subject on the aspngcs list.
[The discussion was regarding optional parameters in VB, but they are essentially the same thing]
I'd like to try to share a bit of the language designer perspective to this.
When we look at features, the question that we try to answer is:
Is the utility that this feature adds worth the additional complexity in the language?
When we did this for C#, there were a number of cases where the tradeoff isn't clear-cut. Our default decision is not to add a feature, because simplicity is also a feature. To put this another way, C# is already more complex than we'd like it to be, so additional features have to be really useful to meet the cut.
Then later,
I appreciate the feedback. I'm not sure you have to persuade me - I'm pretty much on the fence on this one. You'd really have to persuade Anders...
I can give you a bit more perspective on this issue.
In C++, default parameters don't version well, because the default value ends up in the client code rather than your library. I think that's a pretty serious drawback, and I don't think it's the right approach for C#.
One other thing we've considered doing was having the compiler take a definition like:
public void (string s1, string s2 = null, string s3 = null)
{
}
and generate the other two overloads.
IIRC, we're planning on discussing this so more, but I don't know what the outcome will be.
If anyone wants I can e-mail them the thread (titled "Optional Parameters in C#" for those on the list).
James
Simplicity Rules!
|
|
|
|
|
James T. Johnson wrote:
To put this another way, C# is already more complex than we'd like it to be,
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
|
|
|
|
|
|
I just spoke with Joe Nalewabau (Product Manager for C#) and asked him why default params were not included in the language. I figured you might want to hear the "inside story":
[Joe Nalewabau] - Default parameters were not added due to the versioning issues associated with them. Essentially when you add default parameters the compiler essentially burns the default value in to the callers code when the code is compiled. This means that if you change the default values all clients must be recompiled to take advantage of the new value -
rather than have the class library itself decide what the default values should be.
Hope this helps.
Cheers,
Tom Archer
Author, Inside C#
Author, Visual C++.NET Bible
A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af
|
|
|
|
|
Thank you for information. This is quite reasonable (but sometimes default parameters are very usefull
Tomiga
|
|
|
|
|
I'm with you. I'd rather have the default params. As Christian stated, people are just going to inject work-arounds to the lack of support for default params that are cause the same problems that resulted in their omission to begin with
Cheers,
Tom Archer
Author - Inside C#, Visual C++.NET Bible
A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af
|
|
|
|
|
Hello All,
I'm having some problems reading values off a remote machine's registy. When I run the following code I get a System.NullReferenceException all the time. Any Ideas why ? Thanks in advance...
-Koby
using System;
using Microsoft.Win32;
namespace RegExlorer
{
class RegExplorer
{
public static void Main()
{
RegistryKey remoteKey;
object myObj;
// try to open HKEY_CURRENT_USER hive on remote machine
remoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.CurrentUser, "beast4-009");
// try to open (HKEY_CURRENT_USER\)Volatile Enviornment" hive on remote machine
remoteKey.OpenSubKey("Volatile Environment");
// get value
myObj = remoteKey.GetValue("USERDNSDOMAIN");
// print the result out to the screen
Console.WriteLine(myObj.ToString());
// wait for a key stroke
Console.ReadLine();
}
}
}
|
|
|
|
|
There are actually two separate problems here.
1) You're using the RegistryKey from the OpenRemoteBaseKey call to get the value of the subkey when the OpenSubKey returned the RegistryKey you need to use.
2) You need to specify permissions (using the RegistryPermission class) to work with the remote registry.
Here's a quick example I just typed up and ran on my home network. Obviously, you'll need to change your machine name:
using System;
using Microsoft.Win32;
using System.Security.Permissions;
namespace RegExlorer
{
class RegExplorer
{
public static void Main()
{
try
{
RegistryPermission f = new RegistryPermission(
RegistryPermissionAccess.Read,
"\\cosette\\HKEY_CURRENT_USER\\Volatile Environment\\0");
RegistryKey remoteUserHive;
RegistryKey volatileEnvKey;
object myObj;
remoteUserHive = RegistryKey.OpenRemoteBaseKey(RegistryHive.CurrentUser, "COSETTE");
if (remoteUserHive != null)
{
volatileEnvKey = remoteUserHive.OpenSubKey("Volatile Environment");
if (volatileEnvKey != null)
{
myObj = volatileEnvKey.GetValue("LOGONSERVER");
Console.WriteLine(myObj.ToString());
}
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}
}
Cheers,
Tom Archer
Author, Inside C#
A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.
|
|
|
|
|
Further testing seems to indicate that the permissions is not needed and that your only problem was not using the RegistryKey returned from the GetSubKey method. You might want to play around with this, but this code works and should get you pointed in the right direction.
Cheers,
Tom Archer
Author, Inside C#
A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.
|
|
|
|
|
Thanks for the swift reply Tom the code that you have provided me worked.
-Koby
P.S.: I just ordered a copy of your book
|
|
|
|
|
Very cool on both issues
Cheers,
Tom Archer
Author, Inside C#
A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.
|
|
|
|
|
I want to convert a colored bitmap to a Grayscaled one...
so we wonder if there is function doing this in C#....
Thanks in advance..
|
|
|
|
|