473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Nullable Types as out Parameter

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
3 7787
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ESPNSTI | last post by:
Hi, Please don't shoot me if the answer is simple, I'm new to .Net and C# :) .. I'm attempting to convert a nullable type to it's "non-nullable" type in a generic way (without knowing what...
12
by: Steven Livingstone | last post by:
I've just blogged some stuff on Nullable types in net 2.0. http://stevenr2.blogspot.com/2006/01/nullable-types-and-null-coalescing.html Question however as to why you can't simply get an implcit...
6
by: Steven Livingstone | last post by:
Bit of advice here folks. I am creating a default constructor that just initializes a new instance of my object and a secon constructor that takes an ID and loads an object with data from the...
0
by: Larry Lard | last post by:
There seems to be something a bit lacking in the way the dataset designer thing deals (or rather doesn't) with nullable fields in VS2005. Maybe it's cos I'm using VB2005 Express (which is variously...
2
by: Joe Bloggs | last post by:
Hi, compiling the following code: public class App { static void Main() { int? x = 5; bool? i = null;
5
by: Niall | last post by:
I'm experience problems very similar to those reported in this thread on dotnet.framework on May 16 at...
8
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
2
by: Andrus | last post by:
My type contains constructor with nullable parameter like public class Iandmed { public Iandmed(DateTime? miskuup) {} } I tried to get its constructor parameter type name using Type t =...
6
by: JDS | last post by:
I want to be able to use effectively a table adaptor query that can take several arguments with several of those arguments possibly null. I have not been able to do this elegantly. When I first...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.