473,770 Members | 3,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cannot convert from int to ref object

Hello,

probably a beginners question :)

I have this code sample in VB:
if (objResults.Ite m(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).

--
rgds, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #1
15 10697
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 :)
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Hi Wilfried,
Hello,

probably a beginners question :)

I have this code sample in VB:
if (objResults.Ite m(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
Nov 16 '05 #3
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
Nov 16 '05 #4
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.GetE numerator();
while (Result.MoveNex t()) {
if(Result.Curre nt is MapPoint.Locati on) {
Loc = (MapPoint.Locat ion)Result.Curr ent;
if(Loc.StreetAd dress != null)
Console.WriteLi ne(Loc.StreetAd dress.Value);
}
}

rgds, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #5
Wilfried Mestdagh <Wi************ **@discussions. microsoft.com> wrote:
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.GetE numerator();
while (Result.MoveNex t()) {
if(Result.Curre nt is MapPoint.Locati on) {
Loc = (MapPoint.Locat ion)Result.Curr ent;
if(Loc.StreetAd dress != null)
Console.WriteLi ne(Loc.StreetAd dress.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.Locat ion loc in objResults)
{
if (loc.StreetAddr ess != null)
{
Console.WriteLi ne (loc.StreetAddr ess.Value);
}
}

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

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

if (loc != null && loc.StreetAddre ss != null)
{
Console.WriteLi ne (loc.StreetAddr ess.Value);
}
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Hello Jon,
You can use a foreach statement:
Thanks for your reply, this helped me a lot. Is lot more simplier :)
MapPoint.Locati on loc = o as MapPoint.Locati on;
if (loc != null && loc.StreetAddre ss != 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.Locati on loc = o as MapPoint.Locati on;
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 stratenToolStri pMenuItem_Click (object sender, EventArgs
e)
{
MapPoint.Locati on objLoc;
MapPoint.Locati on Loc;
MapPoint.FindRe sults objResults;
MapPoint.Pushpi n PP;
System.Collecti ons.IEnumerator Result;

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

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

objResults =
MP.ActiveMap.Ob jectsFromPoint( MP.ActiveMap.Lo cationToX(objLo c),
MP.ActiveMap.Lo cationToY(objLo c));

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

if(Loc != null && Loc.StreetAddre ss != null)
Console.WriteLi ne("value " + Loc.StreetAddre ss.Value);
else
Console.WriteLi ne("--- not mappoint.locati on object ---");
}
}

thx, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #7
Wilfried Mestdagh <Wi************ **@discussions. microsoft.com> wrote:
You can use a foreach statement:


Thanks for your reply, this helped me a lot. Is lot more simplier :)
MapPoint.Locati on loc = o as MapPoint.Locati on;
if (loc != null && loc.StreetAddre ss != 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 :)


Yes. You could use "if (o is MapPoint.Locati on)" and then cast, but
using "as" is cheaper (because it only needs to do the test once).
foreach (object o in objResults)
{
MapPoint.Locati on loc = o as MapPoint.Locati on;


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.FindRe sults? (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", "FindResult s" etc - much more
readable.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Hello Jon,

Thanks for your reply. This si a URL with the method I use:

http://msdn.microsoft.com/library/de...indResults.asp

rgds, Wilfried
Nov 16 '05 #9
Hello Jon,
"using" statement at the top of your class would allow you to refer to
the classes just as "Location", "FindResult s" 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
Nov 16 '05 #10

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

Similar topics

1
3244
by: Sorisio, Chris | last post by:
Ladies and gentlemen, I've imported some data from a MySQL database into a Python dictionary. I'm attempting to tidy up the date fields, but I'm receiving a 'mx.DateTime.Error: cannot convert value to a time value' error. It's related to glibc returning an error to a pre-1970 date, I think. My question: /how/ do I go through the Python direction I've created to remove the pre-1970 date objects? Ideally, I would be able to iterate...
12
9891
by: Sydex | last post by:
When I compile code I get error C2664: 'Integration::qgaus' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(double)' in this part : double Integration::quad2d(double (*func)(double,double)) { nfunc = func ; return qgaus(f1,x1,x2);//error there
22
27898
by: Christoph Boget | last post by:
I am getting an error (a few among many) for the following lines of code: retval.BrokerName = (( curRow == System.DBNull.Value ) ? SqlString.Null : (string)curRow ); retval.BrokerGroupId = (( curRow == System.DBNull.Value ) ? SqlInt32.Null : (int)curRow ); The errors are (in turn):
3
1641
by: Laura T. | last post by:
The following code is driving me so crazy, that I'm thinking it's a "feature" or a.. ....dare I say it... a BUG. I'm using VS 2003. I've created a new value type (struct) called MyInt. MyInt has all(?) the conversion routines necessary to convert from Int32 and string. (see the listing at the end) When I cast directly from a value of Int32 type, it works:
9
10530
by: Andy Sutorius | last post by:
Hi, I am receiving the error when compiling the project, "cannot implicitly convert type object to string". The error points to this line of code and underlines the dtrRecipient: objMailMessage.To = dtrRecipient; I'm not sure why I am getting this error. Below is the complete code I am working with.
3
4177
by: klynn | last post by:
I defined a session variable as an array using Session = new string; Later, in my code, I need to set it and sort it. I tried Session = "some string"; My error is "Cannot apply indexing with to an expression of type 'object'. Then, later, I try to use it, and sort on it an get 'cannot convert from 'object' to System.array. what am I doing wrong here?
12
10090
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display it. here is the 4 errors. c:\C++\Ch15\Employee.h(29): error C2440: '=' : cannot convert from 'char ' to 'char '
2
15765
by: Christophe | last post by:
class A {} class B {} interface MyInterface { void method(A a); void method(B b); }
7
16709
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be returned from the source. My first attempt was as follows; (please ignore that error handling is not present in this example) public T GetValue<T(string objName) { T results;
2
5194
by: cendter | last post by:
Hi there, I am utterly confused - I have a form that starts an instance from excel and let's the user select a range. I then want to take this range as an array of doubles. I ran into trouble because the 1-based indexing that Excel passes (I think) so after many tries I got: Excel.Range aRange = xlApp.get_Range(textBox2.Text.Split(':'),textBox2.Text.Split(':') );
0
9591
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
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10228
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10057
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
10002
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
9869
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6676
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
5312
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.