By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,052 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,052 IT Pros & Developers. It's quick & easy.

Nullable Types as out Parameter

P: n/a
I'm trying to pass a Nullable type as a parameter to the TryParse()
method (both int and float types) and I'm not having any luck. I've
tried casting as well as passing just the T.Value property; however,
doing this generated a compiler error indicating that it's not possible
to pass a property as an out or ref parameter.

Other than using a temporary (non-nullable) variable, I haven't been
able to come up with a solution.

Example:

int? MyInt;

if (!TryParse("123", out MyInt))
[...]
Any ideas?

Thank you in advance,

--
Sean
Sep 26 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
make an overload of your TryParse Method as the following

private bool TryParse(string s, out int? param)
{
param = 1;
return true;
}

private bool TryParse(string s, out int param)
{
param = 1;
return true;
}
and then use it normally
--
Muhammad Mosa
Software Engineer & Solution Developer
MCT/MCSD.NET
MCTS: .Net 2.0 Web Applications
MCTS: .Net 2.0 Windows Applications
"senfo" wrote:
I'm trying to pass a Nullable type as a parameter to the TryParse()
method (both int and float types) and I'm not having any luck. I've
tried casting as well as passing just the T.Value property; however,
doing this generated a compiler error indicating that it's not possible
to pass a property as an out or ref parameter.

Other than using a temporary (non-nullable) variable, I haven't been
able to come up with a solution.

Example:

int? MyInt;

if (!TryParse("123", out MyInt))
[...]
Any ideas?

Thank you in advance,

--
Sean
Sep 26 '06 #2

P: n/a
senfo <en**********@yahoo.comI-WANT-NO-SPAMwrote:
I'm trying to pass a Nullable type as a parameter to the TryParse()
method (both int and float types) and I'm not having any luck. I've
tried casting as well as passing just the T.Value property; however,
doing this generated a compiler error indicating that it's not possible
to pass a property as an out or ref parameter.

Other than using a temporary (non-nullable) variable, I haven't been
able to come up with a solution.
Well, you could write your own utility method which used the temporary
variable approach if you need to do this a lot, but no, you can't do it
directly. The type of an out/ref parameter must be *exactly* the same
type as the one in the declaration.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 26 '06 #3

P: n/a
Jon Skeet [C# MVP] wrote:
senfo <en**********@yahoo.comI-WANT-NO-SPAMwrote:
>I'm trying to pass a Nullable type as a parameter to the TryParse()
method (both int and float types) and I'm not having any luck. I've
tried casting as well as passing just the T.Value property; however,
doing this generated a compiler error indicating that it's not possible
to pass a property as an out or ref parameter.

Other than using a temporary (non-nullable) variable, I haven't been
able to come up with a solution.

Well, you could write your own utility method which used the temporary
variable approach if you need to do this a lot, but no, you can't do it
directly. The type of an out/ref parameter must be *exactly* the same
type as the one in the declaration.
Interesting. And here I thought I was loosing my mind for not being
able to find a more direct approach.

Thank you very much for your help,

--
Sean
Sep 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.