Storing records in ArrayList.


http://social.microsoft.com/forums/en-us/profile/thread/28c83ddc-38bf-4078-abb8-9879c51e8c31#28c83ddc-38bf-4078-abb8-9879c51e8c31

 

hi,

i using below c# code storing record in arraylist.

i want code handle following:
if record coming first time should add directly, if record existing in list should increment appearancecount.

the output of below code is:
330, 450, 2
330, 589, 1

since '330,450' values came twice incremented , appearancecount i.e third field set 2.


please me code this.

thanks.

*****************************************************************
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.collections;

namespace csharpstorage
{
    class program
    {
        static void main(string[] args)
        {

            arraylist datastructuretwo = new arraylist();
        
            datastructuretwo.add(new twopathinfo(330, 450, 1));
            datastructuretwo.add(new twopathinfo(330, 589,  1));
            datastructuretwo.add(new twopathinfo(330, 450, 1));
            console.readline();

        }
    }
}
*****************************************************************
using system;
using system.collections.generic;
using system.linq;
using system.text;

namespace csharpstorage
{
        public class twopathinfo
        {
            int path1;
            int path2;
            int appearancecount;

            public twopathinfo(int path1, int path2,  int appearancecount)
            {
                this.path1 = path1;
                this.path2 = path2;
                this.appearancecount = appearancecount;
               
            }
        }
}
*****************************************************************

like have encountered in previous posts view today, arraylist deprecated i.e. used better ways utilize have been created , suggested use them.

for arraylist, list<objecttype>.

now coming to your program:

static void main(string[] args){

mydatastructure myds = new mydatastructure();

myds.add(330,450);

myds.add(330,589);

myds.add(330,450);

console.readline();

} //end of main

class mydatastructure{

list<twopathinfo> tpilist;

public mydatastructure(){

tpilist = new list<twopathinfo>();

}

public void add(int param1, int param2){

foreach(twopathinfo tpiloopvar in tpilist){

if(tpiloopvar.path1 == param1 && tpiloopvar.path2 == param2){

tpiloopvar.appearancecount++;

break;

}//end of if

}//end of foreach

}//end of add method

}//end of class

 

hope solves problem



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

BizTalk Server 2013 Azure VM Log Shipping and HA for hosts

How to Share webservice object to all user