C++ to C# and Nullable

  • Thread starter Thread starter Steven
  • Start date Start date
S

Steven

In C++ you can have an optional param by doing something like this:

int GetIndex(POINT pt, RECTANGLE* pRect)
{
....
if(pRect != NULL)
{
pRect->left = 0;
...
}
return Index;
}

RECTANGLE rect;
int nIndex = GetIndex(pt, &rect);
nIndex = GetIndex(pt, NULL);

How do you do that in C#? or can you?

int GetIndex(Point pt, ref Nullable Rectangle rect)
doesn't quite work. Is there a way to do this? I won't
always need the extra piece of information.

Thanks,

Steven
 
The function compiles, but how do I pass null?
Do I have to make

int GetIndex(Point pt, ref Nullable<Rectangle> rect)
{
}

Nullable<Rectangle> rect;
GetIndex(pt, ref rect); //CS0165: Use of unassigned local variable

Is there ANYWAY to do GetIndex(pt, null)?
 
In C++ you can have an optional param by doing something like this:

int GetIndex(POINT pt, RECTANGLE* pRect)
{
....
if(pRect != NULL)
{
pRect->left = 0;
...
}
return Index;
}

RECTANGLE rect;
int nIndex = GetIndex(pt, &rect);
nIndex = GetIndex(pt, NULL);

How do you do that in C#? or can you?

int GetIndex(Point pt, ref Nullable Rectangle rect)
doesn't quite work. Is there a way to do this? I won't
always need the extra piece of information.

Thanks,

Steven

Make an overload like this:

int GetIndex(Point pt)
{
....
return Index;
}

You call it with:

nIndex = GetIndex(pt);
 
Yeah, I had thought about that, but it doesn't seem very efficient.
For one, the code is lengthy, and if anything changes I don't want
to have to change it in two places, and it seems inefficient to make
an overload just to create some storage to pass it.

int GetIndex(Point pt)
{
Rectangle rect = new Rectangle();
return GetIndex(pt, ref rect);
}

It would just seem so much more efficient if there were a way for
me to pass a null value. I can do something like above if C#
deson't provide a way to do, what seems like to me, the right
way t do it. Maybe this is just the trade off for .NET/C# vs C++;
slower, more function calls, extra memory in exchange for
rapid development.
 
Steven presented the following explanation :
The function compiles, but how do I pass null?
Do I have to make

int GetIndex(Point pt, ref Nullable<Rectangle> rect)
{
}

Nullable<Rectangle> rect;
GetIndex(pt, ref rect); //CS0165: Use of unassigned local variable

Is there ANYWAY to do GetIndex(pt, null)?

Rectangle apparently is a struct and you want to change some property
of it (if not null).

To change a "real" Rectangle, you will need the "ref" keyword. So you
would have to call is as:
Rectangle? dummy = null;
int something = GetIndex(pt, ref dummy);

An alternative would be to use an overload to hide this "messy" code:

public int GetIndex(Point pt)
{
Rectangle? dummy = null;
return GetIndex(pt, ref dummy);
}

so you can just call it as
GetIndex(pt);

while the important part of the GetIndex method is *not* duplicated.

Hans Kesting
 
Yeah, I had thought about that, but it doesn't seem very efficient.
For one, the code is lengthy, and if anything changes I don't want
to have to change it in two places, and it seems inefficient to make
an overload just to create some storage to pass it.

Seems rather elegant to me. The length of the code shouldn't matter. Most
overloads of this type will not change when the "main" function changes.
 
Steven,
Do it the other way around - GetIndex(Point, Rect) do whatever to the
Rect and then call GetIndex(Point).
Bob
 
Back
Top