Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string[] as property of object

Status
Not open for further replies.

crazyboybert

Programmer
Jun 27, 2001
798
GB
Hi all

A bit of a theory question and my primary concern here is performance.

In my ASP.NET app I have an object which stores a collection as a CSV. This is necessary as the collection is generated on the client and is recieved as CSV by the object on postback.

I wish the object to have a read only public property which exposes this collection as an Array. The following code shows a simplified example of the class and another which uses it.
Code:
public class A{

  private csv = "1,2,3,4";

  public string[] Items{
    get{ return csv.Split(new char[] {','}); }
  }

}

public class B{
  
  public void method(){
    A oA = new A();
    for(int i=0; i<oA.Items.Length; i++){
      //some processing on oA.Items[i]
    }
  }

}
My concern is that everytime the code in B accesses the collection it will need to be rebuilt by the property getter in A. Is this the case or is the collection loaded into memory the first time the property is accessed and then retrieved from there each time afterwards?

Is this the best way to approach this or is there ar more suitable mehtod for exposing a collection as a property of a class?

Thanks

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Yes, everytime the code in B accesses the collection it will need to execute the Split().
If it is about ASP.NET you can use also Cache to store the csv and the client will retrive it from Cache and use Split() to get the array.
I would modify the above code like here:
Code:
public class A
	{

		private object _csv = "1,2,3,4";

		public object csv
		{
			get{ return _csv; }
		}

	}

public class B
	{
  
		public void method()
		{
			A oA = new A();
			object[] Items = oA.csv.ToString().Split(new char[] {','});
			foreach (object o in oA.Items)
			{
				System.Windows.Forms.MessageBox.Show(o.ToString());
                                // ...
			}
		}
-obislavu-
 
Thanks obislavu

That is as I thought then and not a sensible way of achieveing this. An alternative I have in mind is to create a private member list which is exposed by a public property. In this xcase the code would be
Code:
public class A{
  
  private string _list;

  public string[] list{
    get{ return _list; }
  }
}

public class B{

  public void method(){
    A oA = new A();
    for(int i=0; i<oA.list.length; i++){
     //processing
    }
  }
}
As some background the class A is actually an asp.net composite server control which contains a dropdownlist. I wish to make the items in this list available to other classes without actually exposing the child control.

Assuming some OnLoad processing in the control to create and populate the _list array would the above method suffer from the same problems or woudl this be ok.

Thanks again

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top