views:

220

answers:

3

Hi, I'm trying to do the following:

I need a static variable to get a ListItemCollection from a List control (I can do this, but if I don't set it as Shared It's not preserving the values as it should). The thing is that this class is a SharePoint webpart, so I most probably will be using the webpart more than once, and I need this variable to be unique to each webpart, which shared doesn't accomplish.

I tried everything you can imagine. I placed a Static variable within a Sub (shared and not shared), I tried it with Properties (also Shared and not shared)...

Any Ideas are welcome.

Thanks.

A: 

If you need to save the property on the webpart, add the WebPartStorageAttribute on the property, also throw on a FriendlyNameAttribute on there to make it clean:

C# Version:

[FriendlyNameAttribute("What the setting will be called")]
[WebPartStorage(Storage.Shared)]
private string MyStringThatGetsSaved { get; set; }

VB.Net Version:

<WebPartStorage(Storage.Personal), FriendlyNameAttribute("What the setting will be called")>
Private mMyStringThatGetsSaved As String
Public Property MyStringThatGetsSaved () As String
    Get
        Return mMyStringThatGetsSaved 
    End Get
    Set(ByVal Value As String)
        mMyStringThatGetsSaved = Value
    End Set
End Property

Is this what you're after? If not can you clarify a bit further?

Nick Craver
Could you add an example in vb.net, as the question is tagged as such.
Lachlan Roche
@Lachlan: Thanks, missed the tag...added both versions now, in case someone in c# trips over this and needs it.
Nick Craver
I tried both Storage.Personal and Storage.Shared, none worked. I think this has to do with the variable being an array (a ListItemCollection) and it's like the add doesn't trigger the Set. So I tried to do a temporal variable and set it, still it doesn't work... I'm going to try to use checkboxes and several properties for all the things in the array I want to display. I'll make a post with more details if needed. Thanks.
Santiago
+1  A: 

By definition, static members are per-class (or per-thread with a ThreadStatic attribute).

Lachlan Roche
Without the ThreadStatic attribute (http://msdn.microsoft.com/en-us/library/system.threadstaticattribute%28VS.71%29.aspx) a Shared (VB) variable will be per-class AND per-AppDomain. If a broader scope is required (e.g. per-process or machine) Shared is insufficient.
Jason Weber
A: 

I finally went on another way, I just added some checkboxes to the toolpart and setted the properties on the webpart.

Anyway, what I was trying to do is:

Have a Web Part that changes its controls on Edit & Browse mode. In Edit mode I show two ListBox controls, two buttons (add, remove). When I click the add button, the value has to be removed from the left ListBox and be added to the right ListBox, so far so good I was able to make this functionality with no problems... The thing is when I go back to Browse mode I need to use the items in the ListBox from the right to show (so I added a ListItemCollection control that would store the values from the ListBox on the right), the text of the item and a TextBox control, then the user would enter their text in that textbox and hit the "Search" button and a search query would be executed.

My problem is that when I go from Edit to Browse the ListItemCollection variable I added is getting restarted. So I declared it as Shared, and that does work, but when I add a new instance of the WebPart, they have the same fields displayed... I don't know if there is a way of doing a Static Class-Level variable that is unique to each instance, so I went the ToolPart way...

Santiago