cannot convert from int to ref object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

probably a beginners question :)

I have this code sample in VB:
if (objResults.Item(1).etc
Translate this into C#
if (objResults[1].etc

But compiler complains about the index '1' with: cannot convert from int to
ref object. So what I do wrong here ? (objResults is a returned object from
MapPoint, but that does not matter I think).
 
Hi Wilfried,

I'm not sure what happens here, but VB is often less strict about
conversions where C# needs explicit casts.

What exactly is an objResults? and what does its Item collection return?

Basically, some more code would be nice :)
 
Hi Wilfried,
Hello,

probably a beginners question :)

I have this code sample in VB:
if (objResults.Item(1).etc
Translate this into C#
if (objResults[1].etc

But compiler complains about the index '1' with: cannot convert from int to
ref object. So what I do wrong here ? (objResults is a returned object from
MapPoint, but that does not matter I think).

If VB "Item" needs "ref object" parameter, then you can declare:

object objIndex=1;

.... and pass it by reference as follow:

objResulrs[ref objIndex].etc

HTH

Marcin
 
Hi Marcin and Morton,

Thanks for reply, but dont work. I can access the index in Delphi8 but it
give me other code completitions. So I assume the help and the example are
out of date. Will check that out first.

rgds, Wilfried
 
Hi,

I found out (reading some other likewise questions) how, but dont
understeand wy you can in VB just do objResults.Item(0).etc.. and in C# need
to use new object and GetEnumerator() etc. Can someone explain this to me ?

Result = objResults.GetEnumerator();
while (Result.MoveNext()) {
if(Result.Current is MapPoint.Location) {
Loc = (MapPoint.Location)Result.Current;
if(Loc.StreetAddress != null)
Console.WriteLine(Loc.StreetAddress.Value);
}
}

rgds, Wilfried
http://www.mestdagh.biz
 
Wilfried Mestdagh said:
I found out (reading some other likewise questions) how, but dont
understeand wy you can in VB just do objResults.Item(0).etc.. and in C# need
to use new object and GetEnumerator() etc. Can someone explain this to me ?

Result = objResults.GetEnumerator();
while (Result.MoveNext()) {
if(Result.Current is MapPoint.Location) {
Loc = (MapPoint.Location)Result.Current;
if(Loc.StreetAddress != null)
Console.WriteLine(Loc.StreetAddress.Value);
}
}

It would help if you'd give us a full program. Chances are you can just
use objResults[0] from C#, but as we don't know what objResults are
from your post, that's just a guess.

To iterate through, however, you don't need to call GetEnumerator()
directly in your code. You can use a foreach statement:

foreach (MapPoint.Location loc in objResults)
{
if (loc.StreetAddress != null)
{
Console.WriteLine (loc.StreetAddress.Value);
}
}

That's assuming the results only contain locations. If you want to only
use MapPoint.Location objects, and the result contains others, you'd
use:

foreach (object o in objResults)
{
MapPoint.Location loc = o as MapPoint.Location;

if (loc != null && loc.StreetAddress != null)
{
Console.WriteLine (loc.StreetAddress.Value);
}
}
 
Hello Jon,
You can use a foreach statement:

Thanks for your reply, this helped me a lot. Is lot more simplier :)
MapPoint.Location loc = o as MapPoint.Location;
if (loc != null && loc.StreetAddress != null)

So I see, if o is not a Location object, then the 'o as' returns null. So
not needed to play around with exception blocks here. Nice :)
foreach (object o in objResults)
{
MapPoint.Location loc = o as MapPoint.Location;

I see this programming style often. I'm used to declare al local stack
variables in the beginning of a function. Is this way not setting up stack
frames over and over again in a loop, or is this no matter in NET ?
It would help if you'd give us a full program. Chances are you can just
use objResults[0] from C#, but as we don't know what objResults are
from your post, that's just a guess.

It is only a little experiment with Mappoint in C# to learn the IDE /
programming. I'm not new to programming but very new to NET. This is complete:

private void stratenToolStripMenuItem_Click(object sender, EventArgs
e)
{
MapPoint.Location objLoc;
MapPoint.Location Loc;
MapPoint.FindResults objResults;
MapPoint.Pushpin PP;
System.Collections.IEnumerator Result;

objLoc = MP.ActiveMap.GetLocation(Lat, Lon, Alt);
objLoc.GoTo();

// check if we are on a street
PP = MP.ActiveMap.AddPushpin(objLoc, "Kieken");
PP.Symbol = 1;

objResults =
MP.ActiveMap.ObjectsFromPoint(MP.ActiveMap.LocationToX(objLoc),
MP.ActiveMap.LocationToY(objLoc));

foreach (object o in objResults) {
Loc = o as MapPoint.Location;

if(Loc != null && Loc.StreetAddress != null)
Console.WriteLine("value " + Loc.StreetAddress.Value);
else
Console.WriteLine("--- not mappoint.location object ---");
}
}

thx, Wilfried
http://www.mestdagh.biz
 
Wilfried Mestdagh said:
Thanks for your reply, this helped me a lot. Is lot more simplier :)


So I see, if o is not a Location object, then the 'o as' returns null. So
not needed to play around with exception blocks here. Nice :)

Yes. You could use "if (o is MapPoint.Location)" and then cast, but
using "as" is cheaper (because it only needs to do the test once).
I see this programming style often. I'm used to declare al local stack
variables in the beginning of a function. Is this way not setting up stack
frames over and over again in a loop, or is this no matter in NET ?

No, all local variables are set up in IL code just once when you enter
the method. Declaring at the point of first use makes the point of the
variable clearer, as well as allowing you to use the same name in
multiple places without creating conflicts.
It would help if you'd give us a full program. Chances are you can just
use objResults[0] from C#, but as we don't know what objResults are
from your post, that's just a guess.

It is only a little experiment with Mappoint in C# to learn the IDE /
programming. I'm not new to programming but very new to NET. This is complete:

It's not really complete. It's a complete method, but not a complete
program. Then again, without the MapPoint API it's hard to know exactly
what the types involved do - do you have a reference URL to the API for
MapPoint.FindResults? (If MapPoint is a namespace, btw, putting a
"using" statement at the top of your class would allow you to refer to
the classes just as "Location", "FindResults" etc - much more
readable.)
 
Hello Jon,
"using" statement at the top of your class would allow you to refer to
the classes just as "Location", "FindResults" etc - much more
readable.)

Yes I know, but if I do that with MapPoint the compiler complains about an
ambiguous reference called 'Application'.

rgds, Wilfried
 
Wilfried Mestdagh said:
Yes I know, but if I do that with MapPoint the compiler complains about an
ambiguous reference called 'Application'.

You can resolve that by specifying the one that you want as:

using Application = System.Windows.Forms.Application;

(or MapPoint.Application, depending on which you want the "default" to
be) and then using the full name for any time you want to use the other
one.
 
Hi Jon,
Right. In that case, the equivalent of the VB objResults.Item(0) is
just objResults[0] in C#.

Yes, but that could not compile :( 'cannot convert from int to ref object'.
and changing to:
object o = 1;
objResulst[ref o].
did do a 'invalid keyword ref'

rgds, Wilfried
 
Wilfried Mestdagh said:
Right. In that case, the equivalent of the VB objResults.Item(0) is
just objResults[0] in C#.

Yes, but that could not compile :( 'cannot convert from int to ref object'.
and changing to:
object o = 1;
objResulst[ref o].
did do a 'invalid keyword ref'

Hmm. Odd. Indexers can't have ref or out parameters in C# - I guess
this is just an oddity of how the MapPoint API has been converted to a
..NET API. Nasty though...
 
Hi Jon,
No, all local variables are set up in IL code just once when you enter
the method. Declaring at the point of first use makes the point of the
variable clearer, as well as allowing you to use the same name in
multiple places without creating conflicts.

I understeand about the more cleare readable code. One more question to be
sure:

while (something) {
object o = some_object;
do_something();
}

What does it internally do ? Is it so that there is no overhead in searching
if o is already created in IL ?

I know I maybe it is time have to read some technical things about NET :)

rgds, Wilfried
http://www.mestdagh.biz
 
Wilfried Mestdagh said:
I understeand about the more cleare readable code. One more question to be
sure:

while (something) {
object o = some_object;
do_something();
}

What does it internally do ? Is it so that there is no overhead in searching
if o is already created in IL ?

Not sure what you mean by "searching" here. The stack space for the
variable o will have been allocated on entry to the method, so there's
no need to create a new slot each time through the loop.
I know I maybe it is time have to read some technical things about NET :)

:)
 
Back
Top