Solved

Another C# Array of objects question

Posted on 2006-10-29
8
306 Views
Last Modified: 2008-02-01

I'm having trouble accessing array list members contained within a class.  First here is the class:

using System;
using System.Collections;

namespace Inhallation
{
      /// <summary>
      /// Summary description for clsChamberInfo.
      /// </summary>
      public class clsChamberInfo
      {

            
            public int ChamberID;
            public bool Active;
            public string ChamberTitle;
            public ArrayList arAlarmInfo = new ArrayList();
            public ArrayList arDataCapture = new ArrayList();
            public ArrayList arLastData = new ArrayList();
            public ArrayList arNewData = new ArrayList();



            public clsChamberInfo()
            {
                  //
                  // TODO: Add constructor logic here
                  //
            }

            public clsChamberInfo(int chamberid, bool active, string chambertitle, ArrayList [] aralarms)
            {
                  this.ChamberID = chamberid;
                  this.Active = active;
                  this.ChamberTitle = chambertitle;
                        
            }

            public void AddAlarm( string alarmname,string uom,int alarmid,decimal lowvalue,decimal highvalue,bool active,int recordid,decimal highwarning,decimal lowwarning, decimal x2, decimal x, decimal constant, int linear, int equationtype, int calculationtype, int displaydecimals, bool activewarning)
            {
                  this.arAlarmInfo.Add( new clsExposureAlarmInfo(alarmname,uom,alarmid,lowvalue,highvalue,active,recordid,highwarning,lowwarning,x2,x,constant,linear,  equationtype, calculationtype, displaydecimals,  activewarning));
            }

            public void AddData( DateTime runtime, int runnum, int startnum, int positionid, int alarmid, decimal parmvalue, int studyid, int sessionid, int exposurerun, bool inexposure)
            {
                  this.arNewData.Add( new clsExposureData(runtime,runnum,startnum,positionid, alarmid,parmvalue, studyid, sessionid, exposurerun));
            }

            public void CopyData()
            {
                  this.arLastData.Clear();
                  this.arLastData.Add(arNewData);

            }

            public void AppendToDataCapture()
            {
                  this.arDataCapture.Add(arNewData);

            }

            public void ClearNewData()
            {
                  this.CopyData();
                  this.arNewData.Clear();
            }



            public override string ToString()
            {
                  return this.ChamberTitle;
            }
      }
}


From my main program, I want to access the members of the arNewData ArrayList.   Here is a function that I am stuck on :

            private void PlotData()
            {
                  for( int i = 0; i<arChamberInfo.Count; i++)
                  {
                        switch(i)
                        {
                              case 0:
                                    waveformGraph1.PlotYAppend( ((clsChamberInfo) arChamberInfo[i]).<-------Stuck here
                        }
                  }
                  waveformGraph1.PlotYAppend(newPos,2);

            }

arChamberInfo is an ArrayList of clsChamberInfo objects.  I want to get to the arChamberInfo[i].arNewData members, like arChamberInfo[i].arNewData[0].  Thanks for your quick help.  I'll be checking back frequently in you need more clarification.  Thanks.
0
Comment
Question by:jvalescu
  • 4
  • 3
8 Comments
 

Author Comment

by:jvalescu
ID: 17831515
By the way, the ArrayList arNewData is an array of this class, clsExposureData:

using System;
using System.Collections;


namespace Inhallation
{
      /// <summary>
      /// Summary description for clsExposureData.
      /// </summary>
      public class clsExposureData
      {
            DateTime RunTime = DateTime.Now;
            int RunNum = 0;
            int StartNum = 0;
            int PositionID = 0;
            int AlarmID = 0;
            decimal ParmValue = 0;
            int StudyID = 0;
            int SessionID = 0;

            public clsExposureData()
            {
                  //
                  // TODO: Add constructor logic here
                  //
            }

            public clsExposureData( DateTime runtime, int runnum, int startnum, int positionid, int alarmid, decimal parmvalue, int studyid, int sessionid, int exposurerun)
            {
                  this.RunTime = runtime;
                  this.RunNum = runnum;
                  this.StartNum = startnum;
                  this.PositionID = positionid;
                  this.AlarmID = alarmid;
                  this.ParmValue = parmvalue;
                  this.StudyID = studyid;
                  this.SessionID = sessionid;

            }
      }
}
0
 
LVL 7

Expert Comment

by:whatsit2002
ID: 17831525
Please let me know if you have you tried the following.

 waveformGraph1.PlotYAppend(((clsChamberInfo) arChamberInfo[i]).arNewData[0]);

If that works and your question is about how to loop through the second collection, try this:

               for( int i = 0; i<arChamberInfo.Count; i++)
               {
                    switch(i)
                    {
                         case 0:
                              ArrayList al = ((clsChamberInfo) arChamberInfo[i]).arNewData;
                              for (int j=0; j<al.Count; j++)
                              {
                                 waveformGraph1.PlotYAppend(al[j]);
                              }
                    }
               }
               waveformGraph1.PlotYAppend(newPos,2);


Thanks.

Jason
0
 

Author Comment

by:jvalescu
ID: 17831655
Jason,

   Thanks for the quick response.  The line, waveformGraph1.PlotYAppend( ((clsChamberInfo) arChamberInfo[i]).arNewData[0]);, compiles fine, but doesn't work.  PlotYAppend is expecting a double for its parameter.  Second, arNewData[0] would be pointing to the first element of that array of clsExposure Data objects.  I'm interested in getting to the clsExposureData.parmvalue property.  Also, when I tried the loop you made, al[j] does not give me access to clsExposureData properties.  Am I confusing things even more?  I'll be standing by.................

0
 
LVL 7

Expert Comment

by:whatsit2002
ID: 17831710
I see. So you need to do some type casting, it looks like. I presume you are using .NET 1.1 (visual studio 2003) and not 2.0 (visual studio 2005), right? If you were using .NET 2.0, you could simply use a generic list which would make this much, much simpler.

However, on the assumption that you must use ArrayLists, try this, please:

for(int i = 0; i< arChamberInfo.Count; i++)
{
   clsChamberInfo ci = (clsChamberInfo)arChamberInfo[i];
   for (int j = 0; j < ci.Count; j++)
   {
      clsExposureData ed = (clsExposureData)ci.arNewData[j];
      decimal val = ed.parmvalue;
      waveformGraph1.PlotYAppend(val);
   }
}

Also, please make sure that your clsExposureData class has a public property called "paramvalue" because otherwise, you will get a compile error.

Thanks.

Jason
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 100 total points
ID: 17831733
Try that.
If you need to pass a ParmValue to you method you need to :

1. In the clsExposureData class make ParmValue a public property. Really the better way to do that is (unlike your approach)

        private double parmval;

        public double ParmValue
        {
            get{return parmval;}
            set{parmval = value;}
        }

2. Argument to pass: ((clsChamberInfo) arChamberInfo[i]).arNewData[0].ParmValue
0
 

Author Comment

by:jvalescu
ID: 17833551
OK, based on what you've both told me, I've modified my clsExposureData to make each variable a property.  However, I still cannot address it directly.  The class is listed at the bottom of this message.  From my main program, I am trying to get the value, ParmValue, but once I get to here:

decimal pv = ((clsChamberInfo) arChamberInfo[i]).arNewData[0]. <-----  

None of the properties are showing up.  Let me know what else I can do.  Also, Jason, you mentioned "if you must use Array Lists", is there a better way?  If needed, I'll create another so you can get more points.

Thanks.
0
 

Author Comment

by:jvalescu
ID: 17834284
Forgot to list the update class:

using System;
using System.Collections;


namespace Inhallation
{
      /// <summary>
      /// Summary description for clsExposureData.
      /// </summary>
      public class clsExposureData
      {
            private DateTime runtime = DateTime.Now;
            private int runnum = 0;
            private int startnum = 0;
            private int positionid = 0;
            private int alarmid = 0;
            decimal parmvalue = 0;
            private int studyid = 0;
            private int sessionid = 0;

            public clsExposureData()
            {
                  //
                  // TODO: Add constructor logic here
                  //
            }

            
            public DateTime RunTime
            {
                  get{return runtime;}
                  set{runtime=value;}
            }

            public int RunNum
            {
                  get{return runnum;}
                  set{runnum = value;}
            }
            
            public int StartNum
            {
                  get{return startnum;}
                  set{startnum = value;}
            }

            public int PositionID
            {
                  get{return positionid;}
                  set{positionid = value;}
            }

            public int AlarmID
            {
                  get{return alarmid;}
                  set{alarmid = value;}
            }

            public decimal ParmValue
            {
                  get{return parmvalue;}
                  set{parmvalue = value;}
            }

            public int StudyID
            {
                  get{return studyid;}
                  set{studyid = value;}
            }

            public int SessionID
            {
                  get{return sessionid;}
                  set{sessionid = value;}
            }

      }
}
0
 
LVL 7

Assisted Solution

by:whatsit2002
whatsit2002 earned 400 total points
ID: 17834663
Try this:
decimal pv = ((clsExposureData)((clsChamberInfo) arChamberInfo[i]).arNewData[0]).ParmValue;

Also, my remark about array lists was tied to the availability of generic lists in .NET 2.0. In 2.0, you can create a strongly typed list of your objects like so:

List<clsExposureData> myList = new List<clsExposureData>();
myList.Add(new clsExposureData());

Then when you want to access them, you don't have to cast anything from the base type "object" like you do with an array list. You can do things like:

decimal val = myList[0].ParmValue;

Thanks.

Jason
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now