Index Outside the Bounds of the Array
Hi everyone,
I'm trying to develop a control that will allow a page developer to use
their own XML documents to populate a page's contents instead of having to
conform to some pre-defined scheme. This sounds weird, but it's mainly to
give them the freedom to develop their own tags, number of elements, etc.
As such, I'm trying to have the size and contents of an array populated
dynamically, as opposed to hard-coded.
In the GetArrayContents method below, I'm trying to read the DataColumns
from a DataTable and write a string value to be read by the ReadSlides
method to populate a jagged array, but I keep getting a "Index Outside the
Bounds of the Array" error. I thought this would be OK, but apparently not.
I wrote the string contents of the GetArrayContents out to the browser, and
the format seems to be correct.
// set a jagged array of images & captions to use by reading data from an
XML file
private string[][] ReadSlides(string filepath)
{
FileStream fs = new
FileStream(filepath,FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs);
DataSet ds = new DataSet();
ds.ReadXml(sr);
fs.Close();
DataTable table = ds.Tables[0];
DataRow[] rows = table.Select();
string[][] contents = new string[rows.Length][];
for(int i=0;i<rows.Length;i++)
{
// THIS IS HOW THE ARRAY SHOULD BE COMING POPULATED, AND
IT'S THE SAME LINE I'M TRYING TO CREATE
// contents[i] = new string[] {
rows[i]["image"].ToString(),rows[i]["alt"].ToString(),rows[i]["title"].ToStr
ing(),rows[i]["caption"].ToString() };
contents[i] = new string[] { GetArrayContents(table) };
}
return contents;
}
// helper method that automates the population of the jagged array
private string GetArrayContents(DataTable table)
{
string arrayContents = string.Empty;
foreach(DataColumn col in table.Columns)
{
arrayContents += "rows[i][\"" + col.ColumnName +
"\"].ToString(),";
}
return arrayContents;
}
Thanks!
Re: Index Outside the Bounds of the Array
You're assigning the string return value of the GetArrayContents method to a
string array; therefore, that array will have only one item. It doesn't
matter that the string created by GetArrayContetns contains embedded quotes
and commas, it's still a single string. Instead, in your GetArrayContents
method, create, populate, and return a string array rather than a string,
and assign it directly. For example, rather than this...
contents[i] = new string[] { GetArrayContents(table) };
....use this:
contents[i] = GetArrayContents(table);
....and return a string array from GetArrayContents.
"Jason Salas" <jason@kuam.com> wrote in message
news:3ea749ba@tnews.web.devx.com...
> Hi everyone,
>
> I'm trying to develop a control that will allow a page developer to use
> their own XML documents to populate a page's contents instead of having to
> conform to some pre-defined scheme. This sounds weird, but it's mainly to
> give them the freedom to develop their own tags, number of elements, etc.
> As such, I'm trying to have the size and contents of an array populated
> dynamically, as opposed to hard-coded.
>
> In the GetArrayContents method below, I'm trying to read the DataColumns
> from a DataTable and write a string value to be read by the ReadSlides
> method to populate a jagged array, but I keep getting a "Index Outside the
> Bounds of the Array" error. I thought this would be OK, but apparently
not.
> I wrote the string contents of the GetArrayContents out to the browser,
and
> the format seems to be correct.
>
> // set a jagged array of images & captions to use by reading data from an
> XML file
> private string[][] ReadSlides(string filepath)
> {
> FileStream fs = new
> FileStream(filepath,FileMode.Open,FileAccess.Read);
> StreamReader sr = new StreamReader(fs);
> DataSet ds = new DataSet();
>
> ds.ReadXml(sr);
> fs.Close();
>
> DataTable table = ds.Tables[0];
> DataRow[] rows = table.Select();
>
> string[][] contents = new string[rows.Length][];
> for(int i=0;i<rows.Length;i++)
> {
> // THIS IS HOW THE ARRAY SHOULD BE COMING POPULATED, AND
> IT'S THE SAME LINE I'M TRYING TO CREATE
> // contents[i] = new string[] {
>
rows[i]["image"].ToString(),rows[i]["alt"].ToString(),rows[i]["title"].ToStr
> ing(),rows[i]["caption"].ToString() };
> contents[i] = new string[] { GetArrayContents(table) };
> }
>
> return contents;
> }
>
> // helper method that automates the population of the jagged array
> private string GetArrayContents(DataTable table)
> {
> string arrayContents = string.Empty;
>
> foreach(DataColumn col in table.Columns)
> {
> arrayContents += "rows[i][\"" + col.ColumnName +
> "\"].ToString(),";
> }
>
> return arrayContents;
> }
>
> Thanks!
>
>
Re: Index Outside the Bounds of the Array
Hi Russell,
Thanks for the tip. You helped out a lot. This actuallty dawned on me last
night right before I went to sleep...about needing to populate the array
some other way. I tried manually saving the line:
string var =
rows[i]["image"].ToString(),rows[i]["alt"].ToString(),rows[i]["title"].ToStr
ing(),rows[i]["caption"].ToString()
.... to a string variable and then putting it in the array placeholder, but
it still got the Index Out of Bounds error.
contents[i] = new string[] { var };
This led me to believe that I'd need to find an alternate way to populate
the jagged array. Here's how I got it to work, if you're interested:
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
for(int i=0;i<rows.Length;i++)
{
contents[i] = GetArrayContents(i,table,rows);
}
private string[] GetArrayContents(int rowNumber,DataTable table,DataRow[]
rows)
{
string[] arrayContents = new string[table.Columns.Count];
int incrementer = 0;
for(int i=0;i<table.Columns.Count;i++)
{
arrayContents.SetValue(rows[rowNumber][i].ToString(),i);
incrementer++;
}
return arrayContents;
}
Thanks again!
Jas
"Russell Jones" <arj1@nospam.northstate.net> wrote in message
news:3ea7ec55$1@tnews.web.devx.com...
> You're assigning the string return value of the GetArrayContents method to
a
> string array; therefore, that array will have only one item. It doesn't
> matter that the string created by GetArrayContetns contains embedded
quotes
> and commas, it's still a single string. Instead, in your GetArrayContents
> method, create, populate, and return a string array rather than a string,
> and assign it directly. For example, rather than this...
>
> contents[i] = new string[] { GetArrayContents(table) };
>
> ...use this:
>
> contents[i] = GetArrayContents(table);
>
> ...and return a string array from GetArrayContents.
>
>
> "Jason Salas" <jason@kuam.com> wrote in message
> news:3ea749ba@tnews.web.devx.com...
> > Hi everyone,
> >
> > I'm trying to develop a control that will allow a page developer to use
> > their own XML documents to populate a page's contents instead of having
to
> > conform to some pre-defined scheme. This sounds weird, but it's mainly
to
> > give them the freedom to develop their own tags, number of elements,
etc.
> > As such, I'm trying to have the size and contents of an array populated
> > dynamically, as opposed to hard-coded.
> >
> > In the GetArrayContents method below, I'm trying to read the DataColumns
> > from a DataTable and write a string value to be read by the ReadSlides
> > method to populate a jagged array, but I keep getting a "Index Outside
the
> > Bounds of the Array" error. I thought this would be OK, but apparently
> not.
> > I wrote the string contents of the GetArrayContents out to the browser,
> and
> > the format seems to be correct.
> >
> > // set a jagged array of images & captions to use by reading data from
an
> > XML file
> > private string[][] ReadSlides(string filepath)
> > {
> > FileStream fs = new
> > FileStream(filepath,FileMode.Open,FileAccess.Read);
> > StreamReader sr = new StreamReader(fs);
> > DataSet ds = new DataSet();
> >
> > ds.ReadXml(sr);
> > fs.Close();
> >
> > DataTable table = ds.Tables[0];
> > DataRow[] rows = table.Select();
> >
> > string[][] contents = new string[rows.Length][];
> > for(int i=0;i<rows.Length;i++)
> > {
> > // THIS IS HOW THE ARRAY SHOULD BE COMING POPULATED, AND
> > IT'S THE SAME LINE I'M TRYING TO CREATE
> > // contents[i] = new string[] {
> >
>
rows[i]["image"].ToString(),rows[i]["alt"].ToString(),rows[i]["title"].ToStr
> > ing(),rows[i]["caption"].ToString() };
> > contents[i] = new string[] { GetArrayContents(table) };
> > }
> >
> > return contents;
> > }
> >
> > // helper method that automates the population of the jagged
array
> > private string GetArrayContents(DataTable table)
> > {
> > string arrayContents = string.Empty;
> >
> > foreach(DataColumn col in table.Columns)
> > {
> > arrayContents += "rows[i][\"" + col.ColumnName +
> > "\"].ToString(),";
> > }
> >
> > return arrayContents;
> > }
> >
> > Thanks!
> >
> >
>
>