Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to create an array of objects?

How to create an array of objects?

Scheduled Pinned Locked Moved C#
graphicsdata-structurestutorialquestionworkspace
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aghosh Babu
    wrote on last edited by
    #1

    i am developing an sms gateway application. I have a configuration text file which i have to read from the application and configure the connected modems according to it. After configuring the modems i have to save the configured modem as an object to an array/arraylist. So next time whenver i want the modem to do something i call the object from the array.

    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Text;
    using System.IO;
    using System.Windows.Forms;
    using System.IO.Ports;
    using System.Threading;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;

    namespace SMSTest
    {
    class SMSModem
    {

        public ArrayList PortNameContainer = new ArrayList();
        public ArrayList BaudRateContainer = new ArrayList();
        public ArrayList DataBitContainer = new ArrayList();
        public ArrayList PhoneNumberContainer = new ArrayList();
        public ArrayList SMSCetreNoContainer = new ArrayList();
    
        SerialPort serialPort1 = new SerialPort();
        private string config\_file = "C:/config.txt";
       // public string PhoneNumber;
        private string ServiceCentreNo;
        public int NumberOfLines = 0;
    
    
        public bool ModemConfig()
        {
            try
            {
                string line;
                TextReader tr = new StreamReader(config\_file);
                System.IO.StreamReader objReader;
                objReader = new System.IO.StreamReader(config\_file);
    
    
                while ((line = tr.ReadLine()) != null)
                {
                    NumberOfLines++;
                }
    
    
                string\[\] ListLines = new string\[NumberOfLines\];
    
                for (int i = 0; i < NumberOfLines; i++)
                {
                    ListLines\[i\] = objReader.ReadLine();
                    string\[\] values = ListLines\[i\].Split(new char\[\] { '$', '#' });
                    string\[\] PortSplit = values\[0\].Split(new char\[\] { ':', '#' });
                    string PortName = PortSplit\[1\];
    
                    //\*\*\*\*\*\*\*\*Reading Baud Rate\*\*\*\*\*\*\*\*\*
                    string\[\] BaudSplit = values\[1\].Split(new char\[\] { ':', '#' });
                    int BaudRate = Convert.ToInt32(BaudSplit\[1\]);
    
                    //\*\*\*\*\*\*READING DATA BITS\*\*\*\*\*\*\*\*\*\*
                    string\[\] BitsSplit = values\[2\].Split(new char\[\] { ':', '#' });
                    int DataBits = Convert.ToInt32(BitsSplit\[1\]);
    
                    //\*\*\*\*\*\*\*\*REA
    
    N 1 Reply Last reply
    0
    • A Aghosh Babu

      i am developing an sms gateway application. I have a configuration text file which i have to read from the application and configure the connected modems according to it. After configuring the modems i have to save the configured modem as an object to an array/arraylist. So next time whenver i want the modem to do something i call the object from the array.

      using System;
      using System.Collections.Generic;
      using System.Collections;
      using System.Text;
      using System.IO;
      using System.Windows.Forms;
      using System.IO.Ports;
      using System.Threading;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;

      namespace SMSTest
      {
      class SMSModem
      {

          public ArrayList PortNameContainer = new ArrayList();
          public ArrayList BaudRateContainer = new ArrayList();
          public ArrayList DataBitContainer = new ArrayList();
          public ArrayList PhoneNumberContainer = new ArrayList();
          public ArrayList SMSCetreNoContainer = new ArrayList();
      
          SerialPort serialPort1 = new SerialPort();
          private string config\_file = "C:/config.txt";
         // public string PhoneNumber;
          private string ServiceCentreNo;
          public int NumberOfLines = 0;
      
      
          public bool ModemConfig()
          {
              try
              {
                  string line;
                  TextReader tr = new StreamReader(config\_file);
                  System.IO.StreamReader objReader;
                  objReader = new System.IO.StreamReader(config\_file);
      
      
                  while ((line = tr.ReadLine()) != null)
                  {
                      NumberOfLines++;
                  }
      
      
                  string\[\] ListLines = new string\[NumberOfLines\];
      
                  for (int i = 0; i < NumberOfLines; i++)
                  {
                      ListLines\[i\] = objReader.ReadLine();
                      string\[\] values = ListLines\[i\].Split(new char\[\] { '$', '#' });
                      string\[\] PortSplit = values\[0\].Split(new char\[\] { ':', '#' });
                      string PortName = PortSplit\[1\];
      
                      //\*\*\*\*\*\*\*\*Reading Baud Rate\*\*\*\*\*\*\*\*\*
                      string\[\] BaudSplit = values\[1\].Split(new char\[\] { ':', '#' });
                      int BaudRate = Convert.ToInt32(BaudSplit\[1\]);
      
                      //\*\*\*\*\*\*READING DATA BITS\*\*\*\*\*\*\*\*\*\*
                      string\[\] BitsSplit = values\[2\].Split(new char\[\] { ':', '#' });
                      int DataBits = Convert.ToInt32(BitsSplit\[1\]);
      
                      //\*\*\*\*\*\*\*\*REA
      
      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Your post is broken and hard to read. Please edit it and make it readable.

      Aghosh Babu wrote:

      i want to create an array of modem objects

      Create a class, say Modem with all required properties and put it in a List<Modem> other than arraylist.

      Navaneeth How to use google | Ask smart questions

      L 1 Reply Last reply
      0
      • N N a v a n e e t h

        Your post is broken and hard to read. Please edit it and make it readable.

        Aghosh Babu wrote:

        i want to create an array of modem objects

        Create a class, say Modem with all required properties and put it in a List<Modem> other than arraylist.

        Navaneeth How to use google | Ask smart questions

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        +5 for the attempt to read. WTF you going to audit 300 lines of someone else's code???

        N 1 Reply Last reply
        0
        • L Lost User

          +5 for the attempt to read. WTF you going to audit 300 lines of someone else's code???

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          :)

          Navaneeth How to use google | Ask smart questions

          A 1 Reply Last reply
          0
          • N N a v a n e e t h

            :)

            Navaneeth How to use google | Ask smart questions

            A Offline
            A Offline
            Aghosh Babu
            wrote on last edited by
            #5

            :confused::confused:

            A 1 Reply Last reply
            0
            • A Aghosh Babu

              :confused::confused:

              A Offline
              A Offline
              Aghosh Babu
              wrote on last edited by
              #6

              pls tell me how to create an array of objects in general? and how to call the objects later??

              N 1 Reply Last reply
              0
              • A Aghosh Babu

                pls tell me how to create an array of objects in general? and how to call the objects later??

                N Offline
                N Offline
                N a v a n e e t h
                wrote on last edited by
                #7

                Aghosh Babu wrote:

                pls tell me how to create an array of objects

                object[] objArray = {new object(),new object()};

                You access it with an indexer. Like

                object obj1 = objArray[0]

                . These all are basics and looks like you need a C# book. :)

                Navaneeth How to use google | Ask smart questions

                A 1 Reply Last reply
                0
                • N N a v a n e e t h

                  Aghosh Babu wrote:

                  pls tell me how to create an array of objects

                  object[] objArray = {new object(),new object()};

                  You access it with an indexer. Like

                  object obj1 = objArray[0]

                  . These all are basics and looks like you need a C# book. :)

                  Navaneeth How to use google | Ask smart questions

                  A Offline
                  A Offline
                  Aghosh Babu
                  wrote on last edited by
                  #8

                  I am not an expert.. this is my univ project! ;P

                  J 1 Reply Last reply
                  0
                  • A Aghosh Babu

                    I am not an expert.. this is my univ project! ;P

                    J Offline
                    J Offline
                    J4amieC
                    wrote on last edited by
                    #9

                    Aghosh Babu wrote:

                    this is my univ project!

                    Then you've learnt nothing, choose another career path.

                    A L 2 Replies Last reply
                    0
                    • J J4amieC

                      Aghosh Babu wrote:

                      this is my univ project!

                      Then you've learnt nothing, choose another career path.

                      A Offline
                      A Offline
                      Aghosh Babu
                      wrote on last edited by
                      #10

                      thanks for the advice! unfortunately i havn't born as a programmer ;P

                      W 1 Reply Last reply
                      0
                      • A Aghosh Babu

                        thanks for the advice! unfortunately i havn't born as a programmer ;P

                        W Offline
                        W Offline
                        wasimsharp
                        wrote on last edited by
                        #11

                        <pre> i am developing an sms gateway application</pre> If ur not programmer then how you develop that gateway. :sigh: But Here is the Solution

                        ArrayList arrObj = new ArrayList();
                        arrObj.Add(YourObject);

                        foreach(object o in arrObj)
                        {
                        if(o is YourObjectClass)
                        {
                        //Use this object
                        }
                        }

                        WANTED wasim khan(Killed 50 Innocent Buggs, Distroyed 200 Exception, make 5 Project Hostage) any Compnay Hire him will pay 30,000. Best place where u can get him is Sorcim Technologies Murre Road RWP

                        1 Reply Last reply
                        0
                        • J J4amieC

                          Aghosh Babu wrote:

                          this is my univ project!

                          Then you've learnt nothing, choose another career path.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          If I handed in my University project that composed (mainly) of an array of generic objects I don't think I would have passed.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups