473,804 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

arrays and parse enumerators

I have

.....

enum day {monday, tuesday, wednesday};

myArray[2,2]=5;

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

Console.Writeli ne(i);

Console.Writeli ne(myArray[2,2]);
outputs:

2
5

as expected. If i try

Console.Writeli ne(myArray[i,i]);
i get index outside bound of array error. Any ideas anyone?
Nov 15 '05 #1
5 1487
Had to correct a couple of syntax errors, this compiles and works;

Other than some "" missing in the Enum.Parse call and Console.WriteLi ne 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.Par se(typeof(day), "wednesday" );

Console.WriteLi ne(i);

Console.WriteLi ne(myArray[2,2]);

Console.WriteLi ne(myArray[i,i]);

}
}
//
"tony collier" <me*****@hotmai l.com> wrote in message
news:Xn******** *************** ********@140.99 .99.130...
I have

....

enum day {monday, tuesday, wednesday};

myArray[2,2]=5;

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

Console.Writeli ne(i);

Console.Writeli ne(myArray[2,2]);
outputs:

2
5

as expected. If i try

Console.Writeli ne(myArray[i,i]);
i get index outside bound of array error. Any ideas anyone?

Nov 15 '05 #2
tony collier <me*****@hotmai l.com> wrote:
I have

....

enum day {monday, tuesday, wednesday};

myArray[2,2]=5;

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

Console.Writeli ne(i);
Console.Writeli ne(myArray[2,2]);

outputs:

2
5

as expected. If i try

Console.Writeli ne(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?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote in
news:MP******** *************** *@msnews.micros oft.com:
tony collier <me*****@hotmai l.com> wrote:
I have

....

enum day {monday, tuesday, wednesday};

myArray[2,2]=5;

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

Console.Writeli ne(i);
Console.Writeli ne(myArray[2,2]);

outputs:

2
5

as expected. If i try

Console.Writeli ne(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(O bject Sender, EventArgs e)
{

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

Session["pricetable "]=pricetable;
}

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

in page_load
enum bookstores

{
Amazon,
Blackwells,
CoopBookshop,
CountryBookshop ,
HistoryBookshop com,
StudentBookWorl dcom,
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=RawH tml.ToString(). IndexOf(">",sta rtindex)+1;

endindex=RawHtm l.ToString).Ind exOf"<",startin dex);

Bookstore.Appen d(RawHtml.ToStr ing().Substring (startindex, endindex-
startindex));
// set storeno equal to integer corresponding to bookstore element

storeno=(int)En um.Parse(typeof (bookstores),Bo okstore.ToStrin g());
/* 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=RawH tml.ToString(). IndexOf("egen>" ,startindex)+5;

endindex=RawHtm l.ToString().In dexOf("<",start index);

price=RawHtml.T oString().Subst ring(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.
Nov 15 '05 #4
tony collier <me*****@hotmai l.com> wrote:
<snip>
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...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote in
news:MP******** *************** *@msnews.micros oft.com:
tony collier <me*****@hotmai l.com> wrote:
<snip>
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.
Nov 15 '05 #6

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

Similar topics

13
1588
by: Kim Jensen | last post by:
Hi there I currently have a .txt file (lets call it jimharper.txt) that holds a number of arrays like for instance: $name = "Jim Harper" $relatives = "Joan Harper (wife)" $relatives = "Beth Harper (daughter)"
35
3687
by: Troll | last post by:
Hi, I need to write a script which reads some data and reports the findings. Just to give you an idea the structure is similar to the following. Data input example: HEADING 1 ********** ColumnA ColumnB ColumnC ColumnD ColumnE
6
1816
by: Matt Taylor | last post by:
I'm trying to write an x86 assembler in C++ for use in a debugger. What I'd like do is to use template specialization to prevent invalid combinations from compiling. Thus one could not accidentally add a 16-bit register and an 8-bit register since there is no encoding for this on the x86 architecture. My trouble has stemmed from the fact that enumerators are only integers and can be freely cast into other enumerators, and I was using them...
6
2025
by: Tim Davis | last post by:
I am currently writing a class which I would like to make "enumerable" by inheriting from IEnumerable. The documentation says that the IEnumerator.MoveNext method should throw an exception if the object being enumerated has changed since the enumerator was created. Does anyone have any tips on how best to implement this? I can think of 2 approaches, neither of which seem very satisfactory. These are as follows: 1. Take a copy of the...
0
1010
by: jg | last post by:
just for those who have to deal with legacy application that supports COM, OCX, and WINAPI(_stadcall) - c-style?, I worked out the COM interface for function out array parameters. For example ' Caller proto: ' Int32 lSz, lsz2 ' String lstrOut ' Int32 lPos
0
1101
by: Frank | last post by:
Hi, I use rpy on linux to call R functions. Works fine up to the following problem: How to parse arrays (no vectors, that means 2-dimensional) to R without much effort? The following code solves the problem (in two different ways). However, it seems to me that there might be a way to do it more efficiently.
1
1499
muaddubby
by: muaddubby | last post by:
Hello all and happy new year. I've seen several posts floating around asking about string enumerators in C#, and generally speaking, they're not supported. I've come up with a way around it that lets you create a class which behaves exactly like the regular enumerators (it offers all the functionality a regular enumerator has), and you can store any type of string in it. The full article can be found here:...
1
1305
nitindel
by: nitindel | last post by:
Hi All, May i have an in depth article of enumerators in C#..that explains the Enumerators in C# very precisely.... Thanks, Nitin
3
2472
by: erbrose | last post by:
Hello all! Newbie here, I've been tasked with a fairly intensive project here and my perl skills are still at a minimum so this post may eventually turn into a long one, but I am only going to ask the immediate problems I am having first and try to figure the rest out myself. Basically I need to compare the contents of a directory, with the contents of a series of 'csv' files, when those two match it will trigger some moving of the contents of...
0
9714
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
9594
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
10599
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
10346
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
10347
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
10090
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
6863
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.