473,466 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Nullable Values

Is there a way to get the address of the underlying value of a nullable
variable without copying the value? I tried the following, but the
compiler doesn't like it. I need to do this because I am going to
pass the pointer to an unmanaged DLL.

public struct MY_RECT
{
public uint left;
public uint top;
public uint right;
public uint bottom;
}

private void TestMe()
{
MY_RECT? rect = new MY_RECT?(new MY_RECT());
MY_RECT* ptr = &rect.Value;
}

Aug 29 '06 #1
9 2034
>I need to do this because I am going to
pass the pointer to an unmanaged DLL.
So why does the variable have to be nullable to begin with?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 29 '06 #2
I am passing multiple structures in a single function call. Some could
be valued while others are empty. Instead of spliting it into a million
functions to cover all the different possible combinations, I want to
keep it a single, simple function. Passing nullable parameters helps me
achieve it.

Mattias Sjögren wrote:
I need to do this because I am going to
pass the pointer to an unmanaged DLL.

So why does the variable have to be nullable to begin with?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 29 '06 #3
On 29 Aug 2006 16:51:22 -0700, ML****@hotmail.com wrote:
>I am passing multiple structures in a single function call. Some could
be valued while others are empty. Instead of spliting it into a million
functions to cover all the different possible combinations, I want to
keep it a single, simple function. Passing nullable parameters helps me
achieve it.

Mattias Sjögren wrote:
>I need to do this because I am going to
pass the pointer to an unmanaged DLL.

So why does the variable have to be nullable to begin with?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
A nullable type (int?) is just C# syntactic sugar for a wrapper around
the Nullable<Tstructure, which is defined like this:

[Serializable, StructLayout(LayoutKind.Sequential),
TypeDependency("System.Collections.Generic.Nullabl eComparer`1"),
TypeDependency("System.Collections.Generic.Nullabl eEqualityComparer`1")]
public struct Nullable<Twhere T: struct
{
private bool hasValue;
internal T value;
public Nullable(T value);
public bool HasValue { get; }
public T Value { get; }
public T GetValueOrDefault();
public T GetValueOrDefault(T defaultValue);
public override bool Equals(object other);
public override int GetHashCode();
public override string ToString();
public static implicit operator Nullable<T>(T value);
public static explicit operator T(Nullable<Tvalue);
}

So your interop code has to deal with the 2 fields, the hasValue flag
and the value, which obviously can be of different sizes.
--
Philip Daniels
Aug 30 '06 #4
Thanks for the response Philip. But I know this and don't understand
how it answers my question. I am not trying to pass the nullable
variable to the DLL. I want to access the address of the underlying
structure so I can pass that to the unmanaged DLL. Do you know how to
get that address?

Philip Daniels wrote:
On 29 Aug 2006 16:51:22 -0700, ML****@hotmail.com wrote:
I am passing multiple structures in a single function call. Some could
be valued while others are empty. Instead of spliting it into a million
functions to cover all the different possible combinations, I want to
keep it a single, simple function. Passing nullable parameters helps me
achieve it.

Mattias Sjögren wrote:
I need to do this because I am going to
pass the pointer to an unmanaged DLL.

So why does the variable have to be nullable to begin with?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

A nullable type (int?) is just C# syntactic sugar for a wrapper around
the Nullable<Tstructure, which is defined like this:

[Serializable, StructLayout(LayoutKind.Sequential),
TypeDependency("System.Collections.Generic.Nullabl eComparer`1"),
TypeDependency("System.Collections.Generic.Nullabl eEqualityComparer`1")]
public struct Nullable<Twhere T: struct
{
private bool hasValue;
internal T value;
public Nullable(T value);
public bool HasValue { get; }
public T Value { get; }
public T GetValueOrDefault();
public T GetValueOrDefault(T defaultValue);
public override bool Equals(object other);
public override int GetHashCode();
public override string ToString();
public static implicit operator Nullable<T>(T value);
public static explicit operator T(Nullable<Tvalue);
}

So your interop code has to deal with the 2 fields, the hasValue flag
and the value, which obviously can be of different sizes.
--
Philip Daniels
Aug 30 '06 #5
On 30 Aug 2006 05:38:29 -0700, ML****@hotmail.com wrote:
>Thanks for the response Philip. But I know this and don't understand
how it answers my question. I am not trying to pass the nullable
variable to the DLL. I want to access the address of the underlying
structure so I can pass that to the unmanaged DLL. Do you know how to
get that address?
Nope, sorry. Not done much interop. Isn't it just available as a known
offset from the address of the Nullable<>? Presumably the bool has a
known, predictable size (4 bytes?)

Actually it's probably best I stop speculating and you get an answer
from somebody who knows what he's talking about :-)

--
Philip Daniels
Aug 30 '06 #6

<ML****@hotmail.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
Thanks for the response Philip. But I know this and don't understand
how it answers my question. I am not trying to pass the nullable
variable to the DLL. I want to access the address of the underlying
structure so I can pass that to the unmanaged DLL. Do you know how to
get that address?
Pass the structure by reference.

struct Position
{
public int x;
public int y;
}

...
Position pos = new Position();
InitLocation(ref pos);

Willy.
Aug 30 '06 #7
Thanks for the response Willy, but I don't think it solves the problem.
I don't have the structure, I have a "nullable" structure. To make your
example relevant, the following changes are needed. However, it will
not compile because the casting essentially gives you the Value
property of Nullable which can not be passed as a reference.

Position? pos = new Position?(new Position());
InitLocation(ref (Position)pos);

To restate the problem, I get a nullable structure and need to attain
the address of the structure within it.
Willy Denoyette [MVP] wrote:
<ML****@hotmail.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
Thanks for the response Philip. But I know this and don't understand
how it answers my question. I am not trying to pass the nullable
variable to the DLL. I want to access the address of the underlying
structure so I can pass that to the unmanaged DLL. Do you know how to
get that address?
Pass the structure by reference.

struct Position
{
public int x;
public int y;
}

...
Position pos = new Position();
InitLocation(ref pos);

Willy.
Aug 30 '06 #8
>Position? pos = new Position?(new Position());
>InitLocation(ref (Position)pos);
So can't you do something like

Position tmp = pos ?? new Position();
InitLocation(ref tmp);
if (pos.HasValue) pos = tmp;

I.e. keep a non-nullable variable that you pass to the native
function. Initializse it from the nullable and write the changes back
to it if it's not null.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 30 '06 #9
Yes, but as I said in my initial post, I wanted to find a way to do
this without copying the data. I am not looking for other ways to do
this because I think I know the alternatives. It just seems that if the
nullable structure contains my structure, I should have the ability to
access it's address.

My function receives a nullable structure. If it is null, I pass a null
via interop to an unmanaged DLL function. If the structure is not null,
I pass a pointer to the structure. If I am going to do this many times
and with large structures, it would be much more efficient to pass the
address of the original than to make a copy, pass its address, then
copy the results back to the original.

I appreciate all the help everyone is trying to provide, but I must say
again that my question is a simple one:
Is it possible to attain the address of the underlying structure in a
nullable? If yes, how?

Mattias Sjögren wrote:
Position? pos = new Position?(new Position());
InitLocation(ref (Position)pos);

So can't you do something like

Position tmp = pos ?? new Position();
InitLocation(ref tmp);
if (pos.HasValue) pos = tmp;

I.e. keep a non-nullable variable that you pass to the native
function. Initializse it from the nullable and write the changes back
to it if it's not null.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 31 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked...
1
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide)...
5
by: GG | last post by:
I am trying to add a nullable datetime column to a datatable fails. I am getting exception DataSet does not support System.Nullable<>. None of these works dtSearchFromData.Columns.Add( new...
15
by: scparker | last post by:
I have yet to find a satisfactory solution to this problem. It involves VB.NET 2.0 and datetime issues. I have a form that asks for a Date to be submitted in dd/mm/yyyy format. When this is...
4
by: J Miro | last post by:
When I use ToString method to format the value of a nullable numeric variable, I get "No overload for method 'ToString' takes '1' arguments" error message. Example: Int32? myNum = 12345; ...
0
by: nightwatch77 | last post by:
Hello, I have recently stumbled upon a problem with serialization of nullable value types in .Net 2.0 and would appreciate any explanation of what's happening. My application is hosted in IIS...
7
by: =?Utf-8?B?bWloa2VsdHQ=?= | last post by:
Just one question, why wouldn't the following lines compile: decimal? decVar = 5; int? intVar = decVar == null ? null : Convert.ToInt32(decVar.GetValueOrDefault()); The compiler shows the...
6
by: Tony Johansson | last post by:
Hello! I'm reading in a book called Visual C# 2005. In the chapter about Generics there ia a section about Nullable types. Here is the text that isn't complete true I think. It says: "You...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.