-
Accessing jagged array values
Hi everyone,
I'm developing a custom control in which I sift through the contents of an
XML document and populate a jagged array (an array with a variable number of
elements, but always has 2 values for each element). Even though I'm only
populating an Array with a key/value pair, I can't use a Hashtable or
SortedList object because of the erratic sorting inhernent in both data
types...I need it to display information as entered into the array.
I'm trying to loop through the array, but I keep getting the error message:
"Cannot apply indexing with [] to an expression of type 'System.Array'" when
trying to access the values with my code. It looks cut-and-dry (or so I
thought), but it won't work.
Here's my code:
private Array GetFiles(string fileProperty)
{
FileStream fs = new
FileStream(fileProperty,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();
// POPULATE THE ARRAY HERE
string[][] arrSubmissions = new string[rows.Length][];
for(int i=0;i<rows.Length;i++)
{
arrSubmissions[i] = new string[2] {
rows[i]["link"].ToString(),rows[i]["title"].ToString() };
}
return arrSubmissions;
}
protected override void Render(HtmlTextWriter writer)
{
// CALL THE PRIVATE HELPED METHOD
Array sl = GetFiles(DataFile);
for(int i=0;i<sl.Length;i++)
{
for(int j=0;j<sl[i].Length;j++)
{
// THIS LINE CAUSES THE ERROR
writer.Write("{0}",sl[i][j].ToString());
}
}
}
Any ideas?
Thanks!
Jas
-
Re: Accessing jagged array values
Use the Array.GetValue method.
"Jason Salas" <jason@kuam.com> wrote in message
news:3ea23602@tnews.web.devx.com...
> Hi everyone,
>
> I'm developing a custom control in which I sift through the contents of an
> XML document and populate a jagged array (an array with a variable number
of
> elements, but always has 2 values for each element). Even though I'm
only
> populating an Array with a key/value pair, I can't use a Hashtable or
> SortedList object because of the erratic sorting inhernent in both data
> types...I need it to display information as entered into the array.
>
> I'm trying to loop through the array, but I keep getting the error
message:
> "Cannot apply indexing with [] to an expression of type 'System.Array'"
when
> trying to access the values with my code. It looks cut-and-dry (or so I
> thought), but it won't work.
>
> Here's my code:
>
> private Array GetFiles(string fileProperty)
> {
> FileStream fs = new
> FileStream(fileProperty,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();
>
> // POPULATE THE ARRAY HERE
> string[][] arrSubmissions = new string[rows.Length][];
> for(int i=0;i<rows.Length;i++)
> {
> arrSubmissions[i] = new string[2] {
> rows[i]["link"].ToString(),rows[i]["title"].ToString() };
> }
> return arrSubmissions;
> }
>
> protected override void Render(HtmlTextWriter writer)
> {
> // CALL THE PRIVATE HELPED METHOD
> Array sl = GetFiles(DataFile);
> for(int i=0;i<sl.Length;i++)
> {
> for(int j=0;j<sl[i].Length;j++)
> {
> // THIS LINE CAUSES THE ERROR
> writer.Write("{0}",sl[i][j].ToString());
> }
> }
> }
>
> Any ideas?
>
> Thanks!
> Jas
>
>
-
Re: Accessing jagged array values
Hi Russell,
I actually was able to figure out my problem...I changed the return data
type to string[][] and it worked out:
using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Collections;
using System.Data;
namespace kuam.utilities.customcontrols
{
public class ContentLinker : Control
{
private string _filename;
public string DataFile
{
get { return Context.Server.MapPath(_filename); }
set { _filename = value; }
}
private string[][] GetFiles(string fileProperty)
{
FileStream fs = new
FileStream(fileProperty,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[][] arrSubmissions = new string[rows.Length][];
for(int i=0;i<rows.Length;i++)
{
arrSubmissions[i] = new string[2] {
rows[i]["link"].ToString(),rows[i]["title"].ToString() };
}
return arrSubmissions;
}
protected override void Render(HtmlTextWriter writer)
{
string[][] arrSubmissions = GetFiles(DataFile);
for(int i=0;i<arrSubmissions.Length;i++)
{
// this needs to skip every other element in the jagged
array (j=j+2), otherwise each entry is printed twice
for(int j=0;j<arrSubmissions[i].Length;j=j+2)
{
writer.Write("\n\t\t<li><a target=\"_blank\"
href=\"{0}\">{1}</a></li>",arrSubmissions[i][0].ToString(),arrSubmissions[i]
[1].ToString());
}
}
}
}
}
....that C# class reads from this file structure:
<?xml version="1.0" encoding="utf-8" ?>
<communitycommentary-submissions>
<article>
<link>rstoraciitenet-0413301.asp</link>
<title>Don't Blame Unions for Economic Downturn</title>
</article>
<article>
<link>rstoraciitenet-0412301.asp</link>
<title>The Ghost of Korean Air Flight 801</title>
</article>
<article>
<link>rstoraciitenet-0406301.asp</link>
<title>We All Killed Jesus Christ</title>
</article>
<article>
<link>rstoraciitenet-03310301.asp</link>
<title>What Price Healtcare?</title>
</article>
<article>
<link>fredgarfanhasu.com-03250301.asp</link>
<title>Worried about Guam's Future</title>
</article>
</communitycommentary-submissions>
Jas
"Russell Jones" <arj1@nospam.northstate.net> wrote in message
news:3ea2be17@tnews.web.devx.com...
> Use the Array.GetValue method.
>
> "Jason Salas" <jason@kuam.com> wrote in message
> news:3ea23602@tnews.web.devx.com...
> > Hi everyone,
> >
> > I'm developing a custom control in which I sift through the contents of
an
> > XML document and populate a jagged array (an array with a variable
number
> of
> > elements, but always has 2 values for each element). Even though I'm
> only
> > populating an Array with a key/value pair, I can't use a Hashtable or
> > SortedList object because of the erratic sorting inhernent in both data
> > types...I need it to display information as entered into the array.
> >
> > I'm trying to loop through the array, but I keep getting the error
> message:
> > "Cannot apply indexing with [] to an expression of type 'System.Array'"
> when
> > trying to access the values with my code. It looks cut-and-dry (or so I
> > thought), but it won't work.
> >
> > Here's my code:
> >
> > private Array GetFiles(string fileProperty)
> > {
> > FileStream fs = new
> > FileStream(fileProperty,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();
> >
> > // POPULATE THE ARRAY HERE
> > string[][] arrSubmissions = new string[rows.Length][];
> > for(int i=0;i<rows.Length;i++)
> > {
> > arrSubmissions[i] = new string[2] {
> > rows[i]["link"].ToString(),rows[i]["title"].ToString() };
> > }
> > return arrSubmissions;
> > }
> >
> > protected override void Render(HtmlTextWriter writer)
> > {
> > // CALL THE PRIVATE HELPED METHOD
> > Array sl = GetFiles(DataFile);
> > for(int i=0;i<sl.Length;i++)
> > {
> > for(int j=0;j<sl[i].Length;j++)
> > {
> > // THIS LINE CAUSES THE ERROR
> > writer.Write("{0}",sl[i][j].ToString());
> > }
> > }
> > }
> >
> > Any ideas?
> >
> > Thanks!
> > Jas
> >
> >
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|