Why can I get the index of the item of the array when I use string*, but
can not get the index of the array when I use any other type (such as
Int32)?
This code will compile perfectly, but if I replace String* with Int32, the
code would not compile?
The code using String* type:
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
int _tmain()
{
String* Items[] = { S"Dog", S"Cat", S"Elephant", S"Gerbil", S"Dog",
S"Horse", S"Pig", S"Cat" };
String* Item = S"Elephant";
int pos = Array::IndexOf(Items, Item);
Console::WriteLine(S"Index of Item in Items is {0}", __box(pos));
return 0;
}
The code using Int32
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
int _tmain()
{
Int32 Items [] = {1,3,5,7,4,9,6};
Int32 Item = 7;
int pos = Array::IndexOf(Items, Item);
Console::WriteLine(S"Index of Item in Items is {0}", __box(pos));
return 0;
} 1 1482
> The code using Int32 #include "stdafx.h"
#using <mscorlib.dll>
using namespace System; using namespace System::Collections;
int _tmain() { Int32 Items [] = {1,3,5,7,4,9,6}; Int32 Item = 7; int pos = Array::IndexOf(Items, Item); Console::WriteLine(S"Index of Item in Items is {0}", __box(pos));
return 0; }
Hi,
This does not compile on my machine.
However, when I change this
int pos = Array::IndexOf(Items, Item);
to this:
int pos = Array::IndexOf(Items, __box(Item));
it compiles cleanly and it also prints the correct index (3).
IndexOf expects an object reference so you have to box the int, just like
you did for the writeline.
--
Kind regards,
Bruno van Dooren br**********************@hotmail.com
Remove only "_nos_pam" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: lawrence |
last post by:
I've waited 6 weeks for an answer to my other question and still no
luck, so let me rephrase the question. I know I can do this:
<form method="post" action="$self">
<input type="text"...
|
by: Randell D. |
last post by:
Folks,
I can program fairly comfortably in PHP and can, for the most part using
these skills and others that I've picked up over the years manage to
read/understand most code in Javascript... so...
|
by: phancey |
last post by:
I am trying to invoke a web service method dynamically. I have created
a generic function that takes a method name, string of parameters and
calls the web method using System.Reflection:
...
|
by: VK |
last post by:
Whatever you wanted to know about it but always were affraid to ask.
<http://www.geocities.com/schools_ring/ArrayAndHash.html>
|
by: Venkat |
last post by:
Hi All,
Can someone tell me how do we convert a character array to string.
I know the other way.
For eg:-
String strvar = "hello";
printf("%s",strvar.c_str());
regards,
|
by: Craig Buchanan |
last post by:
what is the fastest way to remove a value from a string array? something
like:
dim x as string() = {"A","B","C","D"}
'remove C
x.Clear(x, x.IndexOf(x, "C"), 1)
Questions:
|
by: John |
last post by:
Hi
Is there a way to create an array of strings of subscript of type index too?
Like;
x("Element A")="Value for element A"
Thanks
Regards
|
by: drako |
last post by:
Hi,
I'm a bit stumped as I am getting a "Notice: Array to String
Conversion" error when trying to do something that on the surface
should be a very simple task - create an array, and write a set...
|
by: Lonifasiko |
last post by:
Hi,
Just want to replace character at index 1 of a string with another
character. Just want to replace character at that position. I thought
Replace method would be overloaded with an index...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
| |