how to make an array of lists.

  • Thread starter Thread starter jleslie48
  • Start date Start date
J

jleslie48

ok, so I have a list and I use it fine here is its declaration:

public List<faPair> points_list_pc = new List<faPair>(); /* points
list as a percent of range */

it works so well I now need 5 of them but rather than have 5 variables
for example

public List<faPair> points_list_pc0 = new List<faPair>(); /* points
list as a percent of range */
public List<faPair> points_list_pc1 = new List<faPair>(); /* points
list as a percent of range */
public List<faPair> points_list_pc2 = new List<faPair>(); /* points
list as a percent of range */
public List<faPair> points_list_pc3 = new List<faPair>(); /* points
list as a percent of range */
public List<faPair> points_list_pc4 = new List<faPair>(); /* points
list as a percent of range */

I want to have an indexed array of the object. something like this
although this is the wrong syntax;


public List<faPair> points_list_pc[5] = new List<faPair>(); /* points
list as a percent of range */

so where I have references to something like:

points_list_pc.Clear();

will become

points_list_pc[0].Clear();

can something like this be done?
 
ok, so I have a list and I use it fine here is its declaration:

public List<faPair> points_list_pc = new List<faPair>();  /* points
list as a percent of range */

it works so well I now need 5 of them but rather than have 5 variables
for example

public List<faPair> points_list_pc0 = new List<faPair>();  /* points
list as a percent of range */
public List<faPair> points_list_pc1 = new List<faPair>();  /* points
list as a percent of range */
public List<faPair> points_list_pc2 = new List<faPair>();  /* points
list as a percent of range */
public List<faPair> points_list_pc3 = new List<faPair>();  /* points
list as a percent of range */
public List<faPair> points_list_pc4 = new List<faPair>();  /* points
list as a percent of range */

I want to have an indexed array of the object. something like this
although this is the wrong syntax;

public List<faPair> points_list_pc[5] = new List<faPair>();  /* points
list as a percent of range */

so where I have references to something like:

points_list_pc.Clear();

will become

points_list_pc[0].Clear();

can something like this be done?

ok syntax wise this is good:

public int AF_INDX = 0;
public int CJ_INDX = 1;

public List<faPair>[] points_list_pc;


but this statement in the constructor:

points_list_pc[AF_INDX] = new List<faPair>();

generates a runtime error:

System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=WindowsFormsApplication01
StackTrace:
at WindowsFormsApplication01.Form1..ctor() in C:\jon
\visualstudio2010\csharp
\WindowsFormsApplication01\WindowsFormsApplication01\Form1.cs:line 117
at WindowsFormsApplication01.Program.Main() in C:\jon
\visualstudio2010\csharp
\WindowsFormsApplication01\WindowsFormsApplication01\Program.cs:line
18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean
checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext, String[] activationCustomData)
at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext)
at System.Activator.CreateInstance(ActivationContext
activationContext)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
 
ok, so I have a list and I use it fine here is its declaration:
public List<faPair> points_list_pc = new List<faPair>();  /* points
list as a percent of range */
it works so well I now need 5 of them but rather than have 5 variables
for example
public List<faPair> points_list_pc0 = new List<faPair>();  /* points
list as a percent of range */
public List<faPair> points_list_pc1 = new List<faPair>();  /* points
list as a percent of range */
public List<faPair> points_list_pc2 = new List<faPair>();  /* points
list as a percent of range */
public List<faPair> points_list_pc3 = new List<faPair>();  /* points
list as a percent of range */
public List<faPair> points_list_pc4 = new List<faPair>();  /* points
list as a percent of range */
I want to have an indexed array of the object. something like this
although this is the wrong syntax;
public List<faPair> points_list_pc[5] = new List<faPair>();  /* points
list as a percent of range */
so where I have references to something like:
points_list_pc.Clear();

will become
points_list_pc[0].Clear();

can something like this be done?

ok syntax wise this is good:

 public  int AF_INDX = 0;
 public  int CJ_INDX = 1;

public List<faPair>[] points_list_pc;

but this statement in the constructor:

points_list_pc[AF_INDX] = new List<faPair>();

generates a runtime error:

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=WindowsFormsApplication01
  StackTrace:
       at WindowsFormsApplication01.Form1..ctor() in C:\jon
\visualstudio2010\csharp
\WindowsFormsApplication01\WindowsFormsApplication01\Form1.cs:line 117
       at WindowsFormsApplication01.Program.Main() in C:\jon
\visualstudio2010\csharp
\WindowsFormsApplication01\WindowsFormsApplication01\Program.cs:line
18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
       at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean
checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContex t
activationContext, String[] activationCustomData)
       at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContex t
activationContext)
       at System.Activator.CreateInstance(ActivationContext
activationContext)
       at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone( )
       at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
       at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:




Never mind. I got it:


public List<faPair>[] points_list_pc = new List<faPair>[2]; /* points
list as a percent of range */

public Form1()
{
InitializeComponent();

plotpt_pairs = new List<faPair>();
points_list_pc[AF_INDX] = new List<faPair>();
points_list_pc[CJ_INDX] = new List<faPair>();
 
ok syntax wise this is good:

public int AF_INDX = 0;
public int CJ_INDX = 1;

public List<faPair>[] points_list_pc;


but this statement in the constructor:

points_list_pc[AF_INDX] = new List<faPair>();

generates a runtime error:

You need:
List<faPair>[] points_list_pc = new List<int>[10];

then

points_list_pc[AF_INDX] = new List<faPair>();

(I chose 10 arbitrarily, it can be whatever you need)

I suspect there may be a better way though but we'll have to wait for an
expert.
 
public List<faPair>[] points_list_pc = new List<faPair>[2]; /* points
list as a percent of range */

public Form1()
{
InitializeComponent();

plotpt_pairs = new List<faPair>();
points_list_pc[AF_INDX] = new List<faPair>();
points_list_pc[CJ_INDX] = new List<faPair>();

A few small comments not directly related to the problem:
* type names are supposed to start with uppercase letter
according to standard naming convention
* given that that those index constants are not really
forced to be sequential, then a Dictionary instead of
an array may be more safe - and maybe make the key an
enum instead of int's

Arne
 
public List<faPair>[] points_list_pc = new List<faPair>[2];  /* points
list as a percent of range */
         public Form1()
         {
             InitializeComponent();
             plotpt_pairs = new List<faPair>();
             points_list_pc[AF_INDX] = new List<faPair>();
             points_list_pc[CJ_INDX] = new List<faPair>();

A few small comments not directly related to the problem:
* type names are supposed to start with uppercase letter
   according to standard naming convention
* given that that those index constants are not really
   forced to be sequential, then a Dictionary instead of
   an array may be more safe - and maybe make the key an
   enum instead of int's

Arne

thanks all for your input.

Ame, I was trying to get an enum to work as an index, but It only
added to the confusion.

I was looking for the C# equivalent to of a #define AF_INDX 0 and
quickly realized that was no longer available.

whats this "dictionary" thing, and how would I get the enum to work?
I'm going to try it out right now anyway...
 
public List<faPair>[] points_list_pc = new List<faPair>[2]; /* points
list as a percent of range */
public Form1()
{
InitializeComponent();
plotpt_pairs = new List<faPair>();
points_list_pc[AF_INDX] = new List<faPair>();
points_list_pc[CJ_INDX] = new List<faPair>();

A few small comments not directly related to the problem:
* type names are supposed to start with uppercase letter
according to standard naming convention
* given that that those index constants are not really
forced to be sequential, then a Dictionary instead of
an array may be more safe - and maybe make the key an
enum instead of int's

thanks all for your input.

Ame, I was trying to get an enum to work as an index, but It only
added to the confusion.

I was looking for the C# equivalent to of a #define AF_INDX 0 and
quickly realized that was no longer available.

whats this "dictionary" thing, and how would I get the enum to work?
I'm going to try it out right now anyway...

Using enum instead of int makes more sense when you are
using Dictionary instead of array.

public enum Indx { A,B,C }
....
Dictionary<Indx,List<FaPair>> data = new Dictionary<Indx,List<FaPair>>();

Arne
 
On 01-12-2010 17:57, jleslie48 wrote:
public List<faPair>[] points_list_pc = new List<faPair>[2];  /* points
list as a percent of range */
          public Form1()
          {
              InitializeComponent();
              plotpt_pairs = new List<faPair>();
              points_list_pc[AF_INDX] = new List<faPair>();
              points_list_pc[CJ_INDX] = new List<faPair>();
A few small comments not directly related to the problem:
* type names are supposed to start with uppercase letter
    according to standard naming convention
* given that that those index constants are not really
    forced to be sequential, then a Dictionary instead of
    an array may be more safe - and maybe make the key an
    enum instead of int's
thanks all for your input.
Ame, I was trying to get an enum to work as an index, but It only
added to the confusion.
I was looking for the C# equivalent to of a #define AF_INDX 0 and
quickly realized that was no longer available.
whats this "dictionary" thing, and how would I get the enum to work?
I'm going to try it out right now anyway...

Using enum instead of int makes more sense when you are
using Dictionary instead of array.

public enum Indx { A,B,C }
...
Dictionary<Indx,List<FaPair>> data = new Dictionary<Indx,List<FaPair>>();

Arne

ok,

so this works:

public enum indx {a, c };

public List<faPair>[] points_list_pc = new List<faPair>[7];

points_list_pc[(int)indx.a] = new List<faPair>();

but it sure is ugly.
 
On 01-12-2010 17:57, jleslie48 wrote:
public List<faPair>[] points_list_pc = new List<faPair>[2]; /* points
list as a percent of range */
public Form1()
{
InitializeComponent();
plotpt_pairs = new List<faPair>();
points_list_pc[AF_INDX] = new List<faPair>();
points_list_pc[CJ_INDX] = new List<faPair>();
A few small comments not directly related to the problem:
* type names are supposed to start with uppercase letter
according to standard naming convention
* given that that those index constants are not really
forced to be sequential, then a Dictionary instead of
an array may be more safe - and maybe make the key an
enum instead of int's
thanks all for your input.
Ame, I was trying to get an enum to work as an index, but It only
added to the confusion.
I was looking for the C# equivalent to of a #define AF_INDX 0 and
quickly realized that was no longer available.
whats this "dictionary" thing, and how would I get the enum to work?
I'm going to try it out right now anyway...

Using enum instead of int makes more sense when you are
using Dictionary instead of array.

public enum Indx { A,B,C }
...
Dictionary<Indx,List<FaPair>> data = new Dictionary<Indx,List<FaPair>>();

ok,

so this works:

public enum indx {a, c };

public List<faPair>[] points_list_pc = new List<faPair>[7];

points_list_pc[(int)indx.a] = new List<faPair>();

but it sure is ugly.

It is. And it does not provide any value.

But it looks a lot better with Dictionary.

Arne
 
On 02-12-2010 08:20, jleslie48 wrote:
On 01-12-2010 17:57, jleslie48 wrote:
public List<faPair>[] points_list_pc = new List<faPair>[2];  /*points
list as a percent of range */
           public Form1()
           {
               InitializeComponent();
               plotpt_pairs = new List<faPair>();
               points_list_pc[AF_INDX] = new List<faPair>();
               points_list_pc[CJ_INDX] = new List<faPair>();
A few small comments not directly related to the problem:
* type names are supposed to start with uppercase letter
     according to standard naming convention
* given that that those index constants are not really
     forced to be sequential, then a Dictionary instead of
     an array may be more safe - and maybe make the key an
     enum instead of int's
thanks all for your input.
Ame, I was trying to get an enum to work as an index, but It only
added to the confusion.
I was looking for the C# equivalent to of a #define AF_INDX 0 and
quickly realized that was no longer available.
whats this "dictionary" thing, and how would I get the enum to work?
I'm going to try it out right now anyway...
Using enum instead of int makes more sense when you are
using Dictionary instead of array.
public enum Indx { A,B,C }
...
Dictionary<Indx,List<FaPair>>  data = new Dictionary<Indx,List<FaPair>>();

so this works:
public enum indx {a, c };
public List<faPair>[] points_list_pc = new List<faPair>[7];
points_list_pc[(int)indx.a] = new List<faPair>();
but it sure is ugly.

It is. And it does not provide any value.

But it looks a lot better with Dictionary.

Arne

Right so now onto dictionary which helps a bit:

public Dictionary <indx,List<faPair>> points_list_pc = new Dictionary
<indx,List<faPair>>();

public Form1()
{
points_list_pc[indx.a] = new List<faPair>();
points_list_pc[indx.c] = new List<faPair>();


so now I have two issues, 1) how do I use the value of the enum, say
for example, I want do do a foreach loop:

foreach ( enum curr_enum in indx) {
foobar(point_list_pc[curr_enum);
}//for

// is that it???

and 2) in a different class things got very ugly:
public class PlotPanel : System.Windows.Forms.Panel
{...

protected override void OnPaint(PaintEventArgs pe)
{...

if
( myform1ptr.points_list_pc[WindowsFormsApplication01.Form1.indx.a].Count
0 ) { //then something to graph


I should note that myform1ptr is WindowsFormsApplication01.Form1 and
the compiler wouldn't accept:

if ( myform1ptr.points_list_pc[myform1ptr.indx.a].Count >0 ) { //then
something to graph


mind you I would love to have a complete global indx.a so it would
look like:

if ( myform1ptr.points_list_pc[indx.a].Count >0 ) { //then something
to graph

no matter where I am in the program.

3) how can I send in a value for the index? for example:

foobar2 ( indx.a);


void foobar2 ( ????? current_index) {

....

points_list_pc[current_index]....


Thanks again Ame for this discussion.
 
Right so now onto dictionary which helps a bit:

public Dictionary<indx,List<faPair>> points_list_pc = new Dictionary
<indx,List<faPair>>();

public Form1()
{
points_list_pc[indx.a] = new List<faPair>();
points_list_pc[indx.c] = new List<faPair>();


so now I have two issues, 1) how do I use the value of the enum, say
for example, I want do do a foreach loop:

foreach ( enum curr_enum in indx) {
foobar(point_list_pc[curr_enum);
}//for

// is that it???

I don't think C# supports this syntax. Try:

foreach(Indx ix in Enum.GetValues(typeof(Indx))
{
}
and 2) in a different class things got very ugly:
public class PlotPanel : System.Windows.Forms.Panel
{...

protected override void OnPaint(PaintEventArgs pe)
{...

if
( myform1ptr.points_list_pc[WindowsFormsApplication01.Form1.indx.a].Count
0 ) { //then something to graph


I should note that myform1ptr is WindowsFormsApplication01.Form1 and
the compiler wouldn't accept:

if ( myform1ptr.points_list_pc[myform1ptr.indx.a].Count>0 ) { //then
something to graph


mind you I would love to have a complete global indx.a so it would
look like:

if ( myform1ptr.points_list_pc[indx.a].Count>0 ) { //then something
to graph

no matter where I am in the program.

Just make Indx er top level type and import the namespace
similar to other types.
3) how can I send in a value for the index? for example:

foobar2 ( indx.a);


void foobar2 ( ????? current_index) {
Indx

(or whatever you decided to call the enum)
...

points_list_pc[current_index]....

Arne
 
Right so now onto dictionary which helps a bit:
public Dictionary<indx,List<faPair>>  points_list_pc = new Dictionary
<indx,List<faPair>>();
public Form1()
{
   points_list_pc[indx.a] = new List<faPair>();
   points_list_pc[indx.c] = new List<faPair>();
so now I have two issues, 1) how do I use the value of the enum, say
for example,    I want do do a foreach loop:
foreach (  enum curr_enum in indx) {
     foobar(point_list_pc[curr_enum);
     }//for
// is that it???

I don't think C# supports this syntax. Try:

foreach(Indx ix in Enum.GetValues(typeof(Indx))
{









}
and 2) in a different class things got very ugly:
public class PlotPanel : System.Windows.Forms.Panel
          {...
protected override void OnPaint(PaintEventArgs pe)
             {...
if
( myform1ptr.points_list_pc[WindowsFormsApplication01.Form1.indx.a].Count
0 ) { //then something to graph
I should note that myform1ptr is WindowsFormsApplication01.Form1  and
the compiler wouldn't accept:
if ( myform1ptr.points_list_pc[myform1ptr.indx.a].Count>0 ) { //then
something to graph
mind you I would love to have a complete global indx.a so it would
look like:
if ( myform1ptr.points_list_pc[indx.a].Count>0 ) { //then something
to graph
no matter where I am in the program.

Just make Indx er top level type and import the namespace
similar to other types.
3) how can I send in a value for the index?   for example:
    foobar2 ( indx.a);
void foobar2 ( ?????   current_index) {

                  Indx

(or whatever you decided to call the enum)


  points_list_pc[current_index]....

Arne

Excellent!!!


heres the solutions so far:

public enum indx {a, c };
public Dictionary <indx,List<faPair>> points_list_pc
= new Dictionary <indx,List<faPair>>();

public Form1()
{

foreach ( indx counter_index in
Enum.GetValues(typeof(indx)) ) {
points_list_pc[counter_index] = new List<faPair>();
}//for



WindowsFormsApplication01.Form1.indx data_index;

data_index = WindowsFormsApplication01.Form1.indx.a;

if ( myform1ptr.points_list_pc[data_index].Count >0 ) {


what took me by surprise is how indx takes on the role of both a
variable and a type. for example both of these lines are
good code:

indx new_enum_variable; //1
new_enum_variable = indx.c; //2
data_count = points_list_pc[indx.a].Count; //3
data_count = points_list_pc[new_enum_variable].Count; //4


in line //1 above, indx takes on the role of a type.in //2 and //3 it
is used like a variable.
in //4 new_enum_variable is used as a variable with the value index.c
 
But it looks a lot better with Dictionary.
Right so now onto dictionary which helps a bit:
public Dictionary<indx,List<faPair>> points_list_pc = new Dictionary
<indx,List<faPair>>();
public Form1()
{
points_list_pc[indx.a] = new List<faPair>();
points_list_pc[indx.c] = new List<faPair>();
so now I have two issues, 1) how do I use the value of the enum, say
for example, I want do do a foreach loop:
foreach ( enum curr_enum in indx) {
foobar(point_list_pc[curr_enum);
}//for
// is that it???

I don't think C# supports this syntax. Try:

foreach(Indx ix in Enum.GetValues(typeof(Indx))
{









}
and 2) in a different class things got very ugly:
public class PlotPanel : System.Windows.Forms.Panel
{...
protected override void OnPaint(PaintEventArgs pe)
{...
if
( myform1ptr.points_list_pc[WindowsFormsApplication01.Form1.indx.a].Count
0 ) { //then something to graph
I should note that myform1ptr is WindowsFormsApplication01.Form1 and
the compiler wouldn't accept:
if ( myform1ptr.points_list_pc[myform1ptr.indx.a].Count>0 ) { //then
something to graph
mind you I would love to have a complete global indx.a so it would
look like:
if ( myform1ptr.points_list_pc[indx.a].Count>0 ) { //then something
to graph
no matter where I am in the program.

Just make Indx er top level type and import the namespace
similar to other types.
3) how can I send in a value for the index? for example:
foobar2 ( indx.a);
void foobar2 ( ????? current_index) {

Indx

(or whatever you decided to call the enum)


points_list_pc[current_index]....

Arne

Excellent!!!


heres the solutions so far:

public enum indx {a, c };
public Dictionary<indx,List<faPair>> points_list_pc
= new Dictionary<indx,List<faPair>>();

public Form1()
{

foreach ( indx counter_index in
Enum.GetValues(typeof(indx)) ) {
points_list_pc[counter_index] = new List<faPair>();
}//for



WindowsFormsApplication01.Form1.indx data_index;

data_index = WindowsFormsApplication01.Form1.indx.a;

if ( myform1ptr.points_list_pc[data_index].Count>0 ) {


what took me by surprise is how indx takes on the role of both a
variable and a type. for example both of these lines are
good code:

indx new_enum_variable; //1
new_enum_variable = indx.c; //2
data_count = points_list_pc[indx.a].Count; //3
data_count = points_list_pc[new_enum_variable].Count; //4


in line //1 above, indx takes on the role of a type.in //2 and //3 it
is used like a variable.
in //4 new_enum_variable is used as a variable with the value index.c

You could also compare //2 and //3 with a type with static fields!

Arne
 
On 02-12-2010 11:46, jleslie48 wrote:
But it looks a lot better with Dictionary.
Right so now onto dictionary which helps a bit:
public Dictionary<indx,List<faPair>>    points_list_pc = new Dictionary
<indx,List<faPair>>();
public Form1()
{
    points_list_pc[indx.a] = new List<faPair>();
    points_list_pc[indx.c] = new List<faPair>();
so now I have two issues, 1) how do I use the value of the enum, say
for example,    I want do do a foreach loop:
foreach (  enum curr_enum in indx) {
      foobar(point_list_pc[curr_enum);
      }//for
// is that it???
I don't think C# supports this syntax. Try:
foreach(Indx ix in Enum.GetValues(typeof(Indx))
{
}
and 2) in a different class things got very ugly:
public class PlotPanel : System.Windows.Forms.Panel
           {...
protected override void OnPaint(PaintEventArgs pe)
              {...
if
( myform1ptr.points_list_pc[WindowsFormsApplication01.Form1.indx.a].Count
0 ) { //then something to graph
I should note that myform1ptr is WindowsFormsApplication01.Form1  and
the compiler wouldn't accept:
if ( myform1ptr.points_list_pc[myform1ptr.indx.a].Count>0 ) { //then
something to graph
mind you I would love to have a complete global indx.a so it would
look like:
if ( myform1ptr.points_list_pc[indx.a].Count>0 ) { //then something
to graph
no matter where I am in the program.
Just make Indx er top level type and import the namespace
similar to other types.
3) how can I send in a value for the index?   for example:
     foobar2 ( indx.a);
void foobar2 ( ?????   current_index) {
                   Indx
(or whatever you decided to call the enum)
...
   points_list_pc[current_index]....
Arne
Excellent!!!

heres the solutions so far:
         public enum indx {a, c };
         public Dictionary<indx,List<faPair>>  points_list_pc
               = new Dictionary<indx,List<faPair>>();
         public Form1()
         {
             foreach ( indx counter_index in
Enum.GetValues(typeof(indx)) ) {
                points_list_pc[counter_index] = new List<faPair>();
                }//for
WindowsFormsApplication01.Form1.indx data_index;
data_index = WindowsFormsApplication01.Form1.indx.a;
if ( myform1ptr.points_list_pc[data_index].Count>0 ) {
what took me by surprise is how indx takes on the role of both a
variable and a type. for example both of these lines are
good code:
  indx new_enum_variable;  //1
  new_enum_variable = indx.c; //2
  data_count = points_list_pc[indx.a].Count; //3
  data_count = points_list_pc[new_enum_variable].Count; //4
in line //1 above,  indx takes on the role of a type.in //2 and //3 it
is used like a variable.
in //4 new_enum_variable is used as a variable with the value index.c

You could also compare //2 and //3 with a type with static fields!

Arne
you mean like this:

if ( new_enum_variable == indx.a) {


Hey, thanks for all your help with this.

- Jon
 
On 02-12-2010 11:46, jleslie48 wrote:
But it looks a lot better with Dictionary.
Right so now onto dictionary which helps a bit:
public Dictionary<indx,List<faPair>> points_list_pc = new Dictionary
<indx,List<faPair>>();
public Form1()
{
points_list_pc[indx.a] = new List<faPair>();
points_list_pc[indx.c] = new List<faPair>();
so now I have two issues, 1) how do I use the value of the enum, say
for example, I want do do a foreach loop:
foreach ( enum curr_enum in indx) {
foobar(point_list_pc[curr_enum);
}//for
// is that it???
I don't think C# supports this syntax. Try:
foreach(Indx ix in Enum.GetValues(typeof(Indx))
{
}
and 2) in a different class things got very ugly:
public class PlotPanel : System.Windows.Forms.Panel
{...
protected override void OnPaint(PaintEventArgs pe)
{...
if
( myform1ptr.points_list_pc[WindowsFormsApplication01.Form1.indx.a].Count
0 ) { //then something to graph
I should note that myform1ptr is WindowsFormsApplication01.Form1 and
the compiler wouldn't accept:
if ( myform1ptr.points_list_pc[myform1ptr.indx.a].Count>0 ) { //then
something to graph
mind you I would love to have a complete global indx.a so it would
look like:
if ( myform1ptr.points_list_pc[indx.a].Count>0 ) { //then something
to graph
no matter where I am in the program.
Just make Indx er top level type and import the namespace
similar to other types.
3) how can I send in a value for the index? for example:
foobar2 ( indx.a);
void foobar2 ( ????? current_index) {

(or whatever you decided to call the enum)
points_list_pc[current_index]....
Excellent!!!

heres the solutions so far:
public enum indx {a, c };
public Dictionary<indx,List<faPair>> points_list_pc
= new Dictionary<indx,List<faPair>>();
public Form1()
{
foreach ( indx counter_index in
Enum.GetValues(typeof(indx)) ) {
points_list_pc[counter_index] = new List<faPair>();
}//for
WindowsFormsApplication01.Form1.indx data_index;
data_index = WindowsFormsApplication01.Form1.indx.a;
if ( myform1ptr.points_list_pc[data_index].Count>0 ) {
what took me by surprise is how indx takes on the role of both a
variable and a type. for example both of these lines are
good code:
indx new_enum_variable; //1
new_enum_variable = indx.c; //2
data_count = points_list_pc[indx.a].Count; //3
data_count = points_list_pc[new_enum_variable].Count; //4
in line //1 above, indx takes on the role of a type.in //2 and //3 it
is used like a variable.
in //4 new_enum_variable is used as a variable with the value index.c

You could also compare //2 and //3 with a type with static fields!
you mean like this:

if ( new_enum_variable == indx.a) {

No.

Compare the syntax.

Arne
 
On 02-12-2010 16:21, jleslie48 wrote:
On 02-12-2010 11:46, jleslie48 wrote:
But it looks a lot better with Dictionary.
Right so now onto dictionary which helps a bit:
public Dictionary<indx,List<faPair>>      points_list_pc = new Dictionary
<indx,List<faPair>>();
public Form1()
{
     points_list_pc[indx.a] = new List<faPair>();
     points_list_pc[indx.c] = new List<faPair>();
so now I have two issues, 1) how do I use the value of the enum, say
for example,    I want do do a foreach loop:
foreach (  enum curr_enum in indx) {
       foobar(point_list_pc[curr_enum);
       }//for
// is that it???
I don't think C# supports this syntax. Try:
foreach(Indx ix in Enum.GetValues(typeof(Indx))
{
}
and 2) in a different class things got very ugly:
public class PlotPanel : System.Windows.Forms.Panel
            {...
protected override void OnPaint(PaintEventArgs pe)
               {...
if
( myform1ptr.points_list_pc[WindowsFormsApplication01.Form1.indx.a]..Count
0 ) { //then something to graph
I should note that myform1ptr is WindowsFormsApplication01.Form1  and
the compiler wouldn't accept:
if ( myform1ptr.points_list_pc[myform1ptr.indx.a].Count>0 ) { //then
something to graph
mind you I would love to have a complete global indx.a so it would
look like:
if ( myform1ptr.points_list_pc[indx.a].Count>0 ) { //then something
to graph
no matter where I am in the program.
Just make Indx er top level type and import the namespace
similar to other types.
3) how can I send in a value for the index?   for example:
      foobar2 ( indx.a);
void foobar2 ( ?????   current_index) {
                    Indx
(or whatever you decided to call the enum)
...
    points_list_pc[current_index]....
Arne
Excellent!!!
heres the solutions so far:
          public enum indx {a, c };
          public Dictionary<indx,List<faPair>>    points_list_pc
                = new Dictionary<indx,List<faPair>>();
          public Form1()
          {
              foreach ( indx counter_index in
Enum.GetValues(typeof(indx)) ) {
                 points_list_pc[counter_index] = new List<faPair>();
                 }//for
WindowsFormsApplication01.Form1.indx data_index;
data_index = WindowsFormsApplication01.Form1.indx.a;
if ( myform1ptr.points_list_pc[data_index].Count>0 ) {
what took me by surprise is how indx takes on the role of both a
variable and a type. for example both of these lines are
good code:
   indx new_enum_variable;  //1
   new_enum_variable = indx.c; //2
   data_count = points_list_pc[indx.a].Count; //3
   data_count = points_list_pc[new_enum_variable].Count; //4
in line //1 above,  indx takes on the role of a type.in //2 and //3it
is used like a variable.
in //4 new_enum_variable is used as a variable with the value index.c
You could also compare //2 and //3 with a type with static fields!
you mean like this:
if ( new_enum_variable == indx.a) {

No.

Compare the syntax.

Arne

I don't get what you are referring to.
 
On 02-12-2010 16:21, jleslie48 wrote:
what took me by surprise is how indx takes on the role of both a
variable and a type. for example both of these lines are
good code:
indx new_enum_variable; //1
new_enum_variable = indx.c; //2
data_count = points_list_pc[indx.a].Count; //3
data_count = points_list_pc[new_enum_variable].Count; //4
in line //1 above, indx takes on the role of a type.in //2 and //3 it
is used like a variable.
in //4 new_enum_variable is used as a variable with the value index.c
You could also compare //2 and //3 with a type with static fields!
you mean like this:
if ( new_enum_variable == indx.a) {

No.

Compare the syntax.

I don't get what you are referring to.

See this example:

using System;

namespace E
{
public enum Indx { A, B, C }
public class Data
{
public static Data A = new Data('A');
public static Data B = new Data('B');
public static Data C = new Data('C');
private char c;
public Data(char c)
{
this.c = c;
}
public override string ToString()
{
return c.ToString();
}
}
public class Program
{
public static void Main(string[] args)
{
Indx ve;
ve = Indx.B;
Console.WriteLine(Indx.B);
Console.WriteLine(ve);
Data vc;
vc = Data.B;
Console.WriteLine(Data.B);
Console.WriteLine(vc);
Console.ReadKey();
}
}
}

Arne
 
List<Point> CRZXPoints = new List<Point>();
List<Point>[] BSPoints = new List<Point>[50]; // allow 50
......
CatmullRom crs = new CatmullRom(ZXPoints, nDivs); // list creator

CRZXPoints = crs.CRPoints(); // return list from method
......
BSPoints[splineCount] = CRZXPoints; // place latest list in list array

splineCount++; // update count
.....................
Echo
foreach (Point pt in CRZXPoints)
{
sWriter.WriteLine(" X {0,8:F2} {1,8:F2}",pt.X, pt.Y);
}
ok, so I have a list and I use it fine here is its declaration:

public List<faPair> points_list_pc = new List<faPair>(); /* points
list as a percent of range */

it works so well I now need 5 of them but rather than have 5 variables
for example

public List<faPair> points_list_pc0 = new List<faPair>(); /* points
list as a percent of range */
public List<faPair> points_list_pc1 = new List<faPair>(); /* points
list as a percent of range */
public List<faPair> points_list_pc2 = new List<faPair>(); /* points
list as a percent of range */
public List<faPair> points_list_pc3 = new List<faPair>(); /* points
list as a percent of range */
public List<faPair> points_list_pc4 = new List<faPair>(); /* points
list as a percent of range */

I want to have an indexed array of the object. something like this
although this is the wrong syntax;


public List<faPair> points_list_pc[5] = new List<faPair>(); /* points
list as a percent of range */

so where I have references to something like:

points_list_pc.Clear();

will become

points_list_pc[0].Clear();

can something like this be done?
On Wednesday, December 01, 2010 4:37 PM jleslie48 wrote:
s

ok syntax wise this is good:

public int AF_INDX =3D 0;
public int CJ_INDX =3D 1;

public List<faPair>[] points_list_pc;


but this statement in the constructor:

points_list_pc[AF_INDX] =3D new List<faPair>();

generates a runtime error:

System.NullReferenceException was unhandled
Message=3DObject reference not set to an instance of an object.
Source=3DWindowsFormsApplication01
StackTrace:
at WindowsFormsApplication01.Form1..ctor() in C:\jon
\visualstudio2010\csharp
\WindowsFormsApplication01\WindowsFormsApplication01\Form1.cs:line 117
at WindowsFormsApplication01.Program.Main() in C:\jon
\visualstudio2010\csharp
\WindowsFormsApplication01\WindowsFormsApplication01\Program.cs:line
18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean
checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContex=
t
activationContext, String[] activationCustomData)
at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContex=
t
activationContext)
at System.Activator.CreateInstance(ActivationContext
activationContext)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone(=
)
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
On Wednesday, December 01, 2010 5:57 PM jleslie48 wrote:
s
s
s
s
s
nts
mbly,
bly,
()
ex t
ex t
e( )
t




Never mind. I got it:


public List<faPair>[] points_list_pc =3D new List<faPair>[2]; /* points
list as a percent of range */

public Form1()
{
InitializeComponent();

plotpt_pairs =3D new List<faPair>();
points_list_pc[AF_INDX] =3D new List<faPair>();
points_list_pc[CJ_INDX] =3D new List<faPair>();
On Wednesday, December 01, 2010 10:59 PM Jeff Gaines wrote:
On 01/12/2010 in message
jleslie48 wrote:


You need:
List<faPair>[] points_list_pc = new List<int>[10];

then

points_list_pc[AF_INDX] = new List<faPair>();

(I chose 10 arbitrarily, it can be whatever you need)

I suspect there may be a better way though but we will have to wait for an
expert.

--
Jeff Gaines Wiltshire UK
Greater love hath no man than this, that he lay down his friends for his
life.
(Jeremy Thorpe, 1962)
On Thursday, December 02, 2010 11:15 AM jleslie48 wrote:
oints
ir>();
ir>();
);

ok,

so this works:

public enum indx {a, c };

public List<faPair>[] points_list_pc =3D new List<faPair>[7];

points_list_pc[(int)indx.a] =3D new List<faPair>();

but it sure is ugly.
On Thursday, December 02, 2010 11:46 AM jleslie48 wrote:
points
<faPair>();
<faPair>();
air>>();

Right so now onto dictionary which helps a bit:

public Dictionary <indx,List<faPair>> points_list_pc =3D new Dictionary
<indx,List<faPair>>();

public Form1()
{
points_list_pc[indx.a] =3D new List<faPair>();
points_list_pc[indx.c] =3D new List<faPair>();


so now I have two issues, 1) how do I use the value of the enum, say
for example, I want do do a foreach loop:

foreach ( enum curr_enum in indx) {
foobar(point_list_pc[curr_enum);
}//for

// is that it???

and 2) in a different class things got very ugly:
public class PlotPanel : System.Windows.Forms.Panel
{...

protected override void OnPaint(PaintEventArgs pe)
{...

if
( myform1ptr.points_list_pc[WindowsFormsApplication01.Form1.indx.a].Count


I should note that myform1ptr is WindowsFormsApplication01.Form1 and
the compiler would not accept:

if ( myform1ptr.points_list_pc[myform1ptr.indx.a].Count >0 ) { //then
something to graph


mind you I would love to have a complete global indx.a so it would
look like:

if ( myform1ptr.points_list_pc[indx.a].Count >0 ) { //then something
to graph

no matter where I am in the program.

3) how can I send in a value for the index? for example:

foobar2 ( indx.a);


void foobar2 ( ????? current_index) {

...

points_list_pc[current_index]....


Thanks again Ame for this discussion.
On Thursday, December 02, 2010 4:21 PM jleslie48 wrote:
ry
nt

Excellent!!!


heres the solutions so far:

public enum indx {a, c };
public Dictionary <indx,List<faPair>> points_list_pc
=3D new Dictionary <indx,List<faPair>>();

public Form1()
{

foreach ( indx counter_index in
Enum.GetValues(typeof(indx)) ) {
points_list_pc[counter_index] =3D new List<faPair>();
}//for



WindowsFormsApplication01.Form1.indx data_index;

data_index =3D WindowsFormsApplication01.Form1.indx.a;

if ( myform1ptr.points_list_pc[data_index].Count >0 ) {


what took me by surprise is how indx takes on the role of both a
variable and a type. for example both of these lines are
good code:

indx new_enum_variable; //1
new_enum_variable =3D indx.c; //2
data_count =3D points_list_pc[indx.a].Count; //3
data_count =3D points_list_pc[new_enum_variable].Count; //4


in line //1 above, indx takes on the role of a type.in //2 and //3 it
is used like a variable.
in //4 new_enum_variable is used as a variable with the value index.c
34, jleslie48 wrote:

See this example:

using System;

namespace E
{
public enum Indx { A, B, C }
public class Data
{
public static Data A = new Data('A');
public static Data B = new Data('B');
public static Data C = new Data('C');
private char c;
public Data(char c)
{
this.c = c;
}
public override string ToString()
{
return c.ToString();
}
}
public class Program
{
public static void Main(string[] args)
{
Indx ve;
ve = Indx.B;
Console.WriteLine(Indx.B);
Console.WriteLine(ve);
Data vc;
vc = Data.B;
Console.WriteLine(Data.B);
Console.WriteLine(vc);
Console.ReadKey();
}
}
}

Arne
 
Back
Top