mfc >> can not get the index of the array when I use any other type ?

by Allen Maki » Mon, 20 Mar 2006 12:15:45 GMT

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;
}






mfc >> can not get the index of the array when I use any other type ?

by AliR » Tue, 21 Mar 2006 01:00:03 GMT


Might want to try the newsgroup

microsoft.public.dotnet.languages.vc

AliR.









Similar Threads

1. can not get the index of the array when I use any other type (such as

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;
}



2. type promotion for some constructors but not others

3. Getting the underlying Type of an Array Type

Does anyone know how to get the underlying type of an Array type?

Here's a bit of code demonstrating what I'm looking for...

public Type getUnderlyingType( Type arrayType )
{
  if( !arrayType.IsArray )
    throw exception

  //get the underlying type and return it
}

In Java you can call Class.getComponentType()?  Is there something similiar in C#?

thanks,

~harris

4. using DrawToBitmap on WebBrowser--works for some sites, not others - CSharp/C#

5. Enum not working correct in array index?

6. casting to int while using enum to indicate index of array - CSharp/C#

7. Q: Exception when using reference-passed int as array index

Hi all,
maybe you'll see the trick and could help me. I am passing int by
reference and get an exception when using it as an index to passed
array. (Yes I know I can workaround with object encapsulation, but
wish to understand this). Thanks, Jan

[code]

using System;

public class TestPassByRef {
    public void printArray(string[] a) {
        p = new int(0);
        printArray(a, ref p);
    }
    private void printArray(string[] a, ref int p) {
        if (p < 0 || p >= a.Length) return;
        Console.Write("{0}:", p);
        Console.Write("({1}) ", a[p]); // here it breaks .. see output
below
        p++;
        printArray(a, ref p);
    }
}

public class Test {
    static void Main() {
        string[] a;
        TestPassByRef t = new TestPassByRef();
        a = new string[] {"A","B","C","D","E"};
        t.printArray(a);
    }
}

[output]

0:
Unhandled Exception: System.FormatException: Index (zero based) must
be greater than or equal to zero and less than the size of the
argument list.
   at System.Text.StringBuilder.AppendFormat(IFormatProvider provider,
String format, Object[] args)
   at System.String.Format(IFormatProvider provider, String format,
Object[] args)
   at System.IO.TextWriter.Write(String format, Object arg0)
   at System.IO.SyncTextWriter.Write(String format, Object arg0)
   at TestPassByRef.printArray(String[] a, Int32& p)
   at TestPassByRef.printArray(String[] a)
   at Test.Main()

8. Accessing ArrayList index by a key not an index number - CSharp/C#