arrays and parse enumerators

  • Thread starter Thread starter tony collier
  • Start date Start date
T

tony collier

I have

.....

enum day {monday, tuesday, wednesday};

myArray[2,2]=5;

i=(int)Enum.Parse(typeof(day), wednesday);

Console.Writeline(i);

Console.Writeline(myArray[2,2]);


outputs:

2
5

as expected. If i try



Console.Writeline(myArray[i,i]);


i get index outside bound of array error. Any ideas anyone?
 
Had to correct a couple of syntax errors, this compiles and works;

Other than some "" missing in the Enum.Parse call and Console.WriteLine not
having a capital "L", I couldn't see any errors.

Josh
Microsoft.com Tools

//
public class Tester
{
enum day {monday, tuesday, wednesday};
[STAThread]
static void Main(string[] args)
{

int[,] myArray = new int[3,3];

myArray[2,2]=5;

int i=(int)Enum.Parse(typeof(day), "wednesday");

Console.WriteLine(i);

Console.WriteLine(myArray[2,2]);

Console.WriteLine(myArray[i,i]);

}
}
//
 
tony collier said:
I have

....

enum day {monday, tuesday, wednesday};

myArray[2,2]=5;

i=(int)Enum.Parse(typeof(day), wednesday);

Console.Writeline(i);
Console.Writeline(myArray[2,2]);

outputs:

2
5

as expected. If i try

Console.Writeline(myArray[i,i]);

i get index outside bound of array error. Any ideas anyone?

Could you show a short but complete program which demonstrates the
problem?
 
tony collier said:
I have

....

enum day {monday, tuesday, wednesday};

myArray[2,2]=5;

i=(int)Enum.Parse(typeof(day), wednesday);

Console.Writeline(i);
Console.Writeline(myArray[2,2]);

outputs:

2
5

as expected. If i try

Console.Writeline(myArray[i,i]);

i get index outside bound of array error. Any ideas anyone?

Could you show a short but complete program which demonstrates the
problem?
asp.net based project>>>


in global.asax :>

......
void Session_Start(Object Sender, EventArgs e)
{

string [,] pricetable=new string [5,12];

Session["pricetable"]=pricetable;
}

............



in page_load


enum bookstores

{
Amazon,
Blackwells,
CoopBookshop,
CountryBookshop,
HistoryBookshopcom,
StudentBookWorldcom,
Swotbooks,
TescoBookStore,
TheBookPlace,
WHSmithOnline,
WHSmithOnlineSD,
ComputerManuals,
ComputerBooks
};

StringBuilder RawHtml=new StringBuilder();

RawHtml.Append( load of screenscraped data );

StringBuilder Bookstore=new StringBuilder();

string [,] pricetable= (string[,])Session["pricetable"];

string price="";

int storeno;



//grab bookstore data between > and < html tags that will correspond
to one of the enum bookstore elements

startindex=RawHtml.ToString().IndexOf(">",startindex)+1;

endindex=RawHtml.ToString).IndexOf"<",startindex);

Bookstore.Append(RawHtml.ToString().Substring(startindex, endindex-
startindex));


// set storeno equal to integer corresponding to bookstore element

storeno=(int)Enum.Parse(typeof(bookstores),Bookstore.ToString());


/* cut out more data from RawHtml and store in pricetable array which is
indexed with a direct correlation between stores and their enumerated
values*/


startindex=RawHtml.ToString().IndexOf("egen>",startindex)+5;

endindex=RawHtml.ToString().IndexOf("<",startindex);

price=RawHtml.ToString().Substring(startindex, endindex-startindex);

pricetable[storeno]=price;



and this is where i get the error. if i do a response.write for storeno
it returns an integer correlating to enum bookstore values and if i do a
response.write for pricetable[0 upto 10 or whatever] this works fine
also.
 
asp.net based project>>>

It's hard to get all that going - please convert it to a simple console
app which basically does nothing apart from showing the bug.

However, I do note that you've got new string[5,12] despite the fact
that there are 13 stores. Also, I'm not convinced that

pricetable[storeno]=price;

is going to work when pricetable is a 2D array...
 
asp.net based project>>>

It's hard to get all that going - please convert it to a simple console
app which basically does nothing apart from showing the bug.

However, I do note that you've got new string[5,12] despite the fact
that there are 13 stores. Also, I'm not convinced that

pricetable[storeno]=price;

is going to work when pricetable is a 2D array...

spot on jon. The whole thing was wrapped in a loop which gave me the
second index for pricetable which i omitted from the example and as you
correctly identified the problem was a really dumb one where i should have
set the pricetable array to [5,13].

I can't stress how impressed i am with all your help. thankyou very much.
 
Back
Top