473,591 Members | 2,871 Online
Bytes | Software Development & Data Engineering Community
+ 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 2040
>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<Tstruc ture, which is defined like this:

[Serializable, StructLayout(La youtKind.Sequen tial),
TypeDependency( "System.Collect ions.Generic.Nu llableComparer` 1"),
TypeDependency( "System.Collect ions.Generic.Nu llableEqualityC omparer`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 GetValueOrDefau lt();
public T GetValueOrDefau lt(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<Tval ue);
}

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<Tstruc ture, which is defined like this:

[Serializable, StructLayout(La youtKind.Sequen tial),
TypeDependency( "System.Collect ions.Generic.Nu llableComparer` 1"),
TypeDependency( "System.Collect ions.Generic.Nu llableEqualityC omparer`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 GetValueOrDefau lt();
public T GetValueOrDefau lt(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<Tval ue);
}

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.goog legroups.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(re f 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(re f (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.goog legroups.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(re f pos);

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

Position tmp = pos ?? new Position();
InitLocation(re f 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(re f (Position)pos);

So can't you do something like

Position tmp = pos ?? new Position();
InitLocation(re f 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
4849
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 one of these syntax formats to work; // if ( l_flg_01 && l_flg_02 && l_flg_03 ) // Line A
1
5675
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) http://msdn2.microsoft.com/en-us/library/ms366789.aspx however, using their code snippet, I couldn't get it to work:
5
51592
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 DataColumn( "StartDate", typeof( DateTime? ) ) ); dtSearchFromData.Columns.Add( new DataColumn( "EndDate", typeof( System.Nullable<DateTime>) ) ); Any ideas?
15
17848
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 submitted it then is then dealt with as follows: ------------------------------------ Public Sub Update(ByVal sender As Object, ByVal e As System.EventArgs) Handles ConfirmEdit.Click
4
11428
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; Console.WriteLine(myNum.ToString("n0")); //error
0
1550
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 and exposes a web service with nullable data types - like: public class DataItem { DateTime? Timestamp; int? SomeData; }
7
2635
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 following message: "There is no implicit conversion between 'null' and 'int'." A similar if-else construction works just fine.
6
1463
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 can also look at the value of a reference type using the Value property. If HasValue is true, then you are guarantee a non-null value for Value, but if HasValue is false, that is, null has been assigned to the variable, then accessing Value will
6
2356
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 came across nullable types I thought this may provide the answer but sadly not. I can't just pass the variable value if it is null (or nothing) but have to check HasValue first and if not pass Nothing directly. This seems to completely miss what I...
0
7934
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8362
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7992
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6639
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5732
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5400
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3891
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2378
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 we have to send another system
0
1199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.