Persist Array in C#

AlexCode

Senior Contributor
Joined
Jul 7, 2003
Messages
931
Location
Portugal
Hi...
I've being doing this with VB.net, but now I'm developing a control on C# but I can't find the way to correctly presist a typed array in code, it allways persists the content of the desing-time created array on the resources.

Everything works fine if the class inherits from the System.Windows.Forms.Control... but I don't need/want those bunch of extra properties...

How do I do this in C#?

Thanks!

Alex :p
 
You should be able to just make the array a class member. It will then persist throughout the lifetime of the control.

Code:
public class MyClass
{  
   const int COUNT = 50;
   private MyType[] array = new MyType[COUNT];

   public MyClass()
   {     
       for (int i=0; i<COUNT; i++)
          array[i] = new MyType();
   }
}
 
Hi...
Possibly I didn't make myself clear...

My persistence need are to code not to memory.
As I'm developing a control that have some properties that are typed collections, edited with the Collections Editor, the items of those collections must be persisted to code at design-time.

In VB.net this task is plain easy... in C# it's not so...

Thanks!
 
PlausiblyDamp said:
How are you doing this in VB then? Is there no direct translation that would work?

In VB.net something like the following would work:

Visual Basic:
Public Class A

     Private _coll() as B
     <System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content)> _
     Public Property Coll() As B()
        Get
             Return _coll
        End Get
        Set(ByVal Value As B())
             _coll = Value
        End Set
     End Property


End Class

Public Class B

     Public property ...

End Class

Translating this to C#, the auto-generated code will put the collection items inside the resources file. This will cause me to be unable to edit those objects at design-time...

I'm aware that I may sound a little confusing... but it's what's happening... :s sorry!

Alex :p
 
Yep...

Anyway, I've just changed my typed collections to BaseCollection inherited classes and it persists the code correctly.

I've just opened another thread with my new trouble.
Now I can edit my collections with the Object Collection Editor...
It shows, but the Members are allways System.Object type.

How do I edit a BaseCollection object with Object Collection Editor?

Thanks!

Alex :p
 
In your overridden Collection class, don't you simply return the object of the correct type?

Something like:

Code:
Public Class MyCollection
	Inherits System.Collections.CollectionBase
	Public Overridable Sub Add(ByVal Item As MyItem)
		list.Add(aItem)
	End Sub
	Public Overridable Sub Insert(ByVal Item As MyItem, ByVal location As Integer)
		list.Insert(location, aItem)
	End Sub

	Public Overridable Sub Remove(ByVal index As Integer)
		If index > Count - 1 Or index < 0 Then
			Throw New IndexOutOfRangeException()
		Else
			list.RemoveAt(index)
		End If

	End Sub
	Public Overridable Function Replace(ByVal index As Integer, ByVal aItem As MyItem
		If index > Count - 1 Or index < 0 Then
			Throw New IndexOutOfRangeException()
		Else
			list.Item(index) = aItem
		End If
	End Function

	Default Public ReadOnly Property Item(ByVal index As Integer) As MyItem		
		Get
			If index < list.Count And index >= 0 Then
				Return CType(list.Item(index), MyItem)
			Else
				Throw New IndexOutOfRangeException()
			End If
		End Get
	End Property
End Class
 
Damn I'm stupid... ****...

I'd already tryed that but with no real hoppe and it didn't worked somehow...

It works now... thanks a lot... saved my day from my stupidity... damn!

Alex :p
 
As I'm on this issue, and the persistence its being donne properlly, just a quick question...

What's the bert way, at design-time, to distroy the objects of a collection when the parent object is deleted?

Example:
Class A have several properties witch type is inherited from CollectionBase.
I add some items into each one of them (the Items have no visibility on the designer whatsoever).
The items are presisted automaticly on code.

When I delete the parent object, it desapears but leaves the collection items... with no parent collection assigned...

I want to remove all the items when the collection is disposed...


Thanks

Alex
 
Isn't that something that should be handled in the destructor of the collection?

Surely the destructor will get called if the object is destroyed, even at design time?

Without testing, I'd assume that:

Code:
Private Sub Finalize()
    For index as Integer = 0 to list.Count - 1
        list.Item(index).Dispose()
    Next

Obviously, your item has to implement IDisposable, or some other GC method.

B.
 
At least here, the collection Finalize method is never called... :(

Something else strange here either...
The collection's property, on the property grid, isn't expandable...
By expandable I mean the common behavior of the collections when represented on the PropertyGrid, displaying a '+' sign that allow us to view its inner items...

I even tryed to user the TypeConverter(GetType(ExpandableObjectConverter))... the '+' sign appears but when clicked desapears...

I may have some kind of disfunction voodoo dealingwith collections at design-time... :eek:

Thaks!

Alex :p
 
Last edited:
Ok... the disposing problem is solved...

Now the only thing is missing its that litle '+' sign to expand the collection items...
On a typed collection is's a default behavior, on a System.Collections.CollectionBase colelction it doesn't show...

Thanks...
 
Back
Top