PC Review


Reply
Thread Tools Rate Thread

How can I wrap a collection ? (re-repost)

 
 
pamela fluente
Guest
Posts: n/a
 
      9th Feb 2007
I have been posting this question with no success. I do not know if I
am not being clear of the question is too difficult :-)) or unclear.
It seems to me that the need to wrap a collection is quite common in
real world programs.

I am having problem to understand how I can wrap collections in
System.Collections.Generic.
For example I want to wrap a System.Collections.Generic.Dictionary. I
wish the wrapping class to have a constructor similar to that of the
dictionary (besides some possible other argument).

I think it should be actually simple, but I cannot see the right
syntax to implement this idea. Could anyone suggest how ?

Intuitively I want something "like" (please * excuse me * if I use the
VB notation, which I am familiar with, I will have no problem to
understand any C# solution to my question )

'------------------------------------
Class DictionaryWrapper

Public Whatever As WhateverObject

Public MyDict As System.Collections.Generic.Dictionary(Of
MyObjectType1, MyObjectType2)(MyComparer)


Sub New(ByVal MyObjectType1 as type?, ByVal MyObjectType2 as
type?, ByVal MyComparer as IComparer , ByVal WhateverObj as
WhateverObject )

Me.MyDict = New System.Collections.Generic.Dictionary(Of
MyObjectType1, MyObjectType2)(MyComparer)

Me.Whatever = WhateverObj

End Sub

End Class
'------------------------------------

Thanks


-Pam

 
Reply With Quote
 
 
 
 
Ashot Geodakov
Guest
Posts: n/a
 
      9th Feb 2007
Hi Pamela,

here's a small sample based on what I could understand from your
requirements:


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication1
{
class DictionaryWrapper<Key, Value>
{
private Dictionary<Key, Value> m_dict = new Dictionary<Key,
Value>();

public Value this[Key key]
{
get
{
return m_dict[key];
}
set
{
m_dict[key] = value;
}
}

// Define whatever other public methods you need that utilize
m_dict.
}

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
DictionaryWrapper<string, int> wrapper = new
DictionaryWrapper<string, int>();
wrapper["key1"] = 10;
wrapper["key2"] = 15;
Console.WriteLine( "Sum of values = " + ( wrapper["key1"] +
wrapper["key2"] ) );
}
}
}


"pamela fluente" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have been posting this question with no success. I do not know if I
> am not being clear of the question is too difficult :-)) or unclear.
> It seems to me that the need to wrap a collection is quite common in
> real world programs.
>
> I am having problem to understand how I can wrap collections in
> System.Collections.Generic.
> For example I want to wrap a System.Collections.Generic.Dictionary. I
> wish the wrapping class to have a constructor similar to that of the
> dictionary (besides some possible other argument).
>
> I think it should be actually simple, but I cannot see the right
> syntax to implement this idea. Could anyone suggest how ?
>
> Intuitively I want something "like" (please * excuse me * if I use the
> VB notation, which I am familiar with, I will have no problem to
> understand any C# solution to my question )
>
> '------------------------------------
> Class DictionaryWrapper
>
> Public Whatever As WhateverObject
>
> Public MyDict As System.Collections.Generic.Dictionary(Of
> MyObjectType1, MyObjectType2)(MyComparer)
>
>
> Sub New(ByVal MyObjectType1 as type?, ByVal MyObjectType2 as
> type?, ByVal MyComparer as IComparer , ByVal WhateverObj as
> WhateverObject )
>
> Me.MyDict = New System.Collections.Generic.Dictionary(Of
> MyObjectType1, MyObjectType2)(MyComparer)
>
> Me.Whatever = WhateverObj
>
> End Sub
>
> End Class
> '------------------------------------
>
> Thanks
>
>
> -Pam
>



 
Reply With Quote
 
pamela fluente
Guest
Posts: n/a
 
      9th Feb 2007
On 9 Feb, 20:53, "Ashot Geodakov" <a_geoda...@hotmail.com> wrote:
> Hi Pamela,
>
> here's a small sample based on what I could understand from your
> requirements:
>
> using System;
> using System.Collections.Generic;
> using System.Windows.Forms;
>
> namespace WindowsApplication1
> {
> class DictionaryWrapper<Key, Value>
> {
> private Dictionary<Key, Value> m_dict = new Dictionary<Key,
> Value>();



Thanks Ashot .

My problem is I do not know how to pass the types that are declared
externally (as parameters for the wrapper constructor)
for the Key and the Value to the internal typed dictionary

System.Collections.Generic.Dictionary(Of MyObjectType1, MyObjectType2)
(MyComparer)

I do not see any type declaration in your code. I am not sure
whether you have provided
the answer to what I was looking for ...

Perhaps c# and vb are too different here or I do not get it ...

mmm I am having hard time with this one.

Anyone more suggestions ?

-Pam



 
Reply With Quote
 
Ollie Riches
Guest
Posts: n/a
 
      9th Feb 2007
Pam,

To add to the previous post here is a C# example that shows how to inherit
of the generic dictionary collection as opposed to aggregating the generic
collection

public class MyCustomDictionary<TKey, TValue> : Dictionary<TKey, TValue>
{
public MyCustomDictionary() : base()
{
}

public MyCustomDictionary(MyCustomDictionary<TKey, TValue>
myCustomDictionary) : base()
{
if (myCustomDictionary == null)
return;

MyCustomDictionary<TKey, TValue>.Enumerator enumDictionary =
myCustomDictionary.GetEnumerator();
while (enumDictionary.MoveNext())
base.Add(enumDictionary.Current.Key,
enumDictionary.Current.Value);
}

public void DoMyStuff()
{
}
}

HTH

Ollie Riches

"Ashot Geodakov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Pamela,
>
> here's a small sample based on what I could understand from your
> requirements:
>
>
> using System;
> using System.Collections.Generic;
> using System.Windows.Forms;
>
> namespace WindowsApplication1
> {
> class DictionaryWrapper<Key, Value>
> {
> private Dictionary<Key, Value> m_dict = new Dictionary<Key,
> Value>();
>
> public Value this[Key key]
> {
> get
> {
> return m_dict[key];
> }
> set
> {
> m_dict[key] = value;
> }
> }
>
> // Define whatever other public methods you need that utilize
> m_dict.
> }
>
> static class Program
> {
> /// <summary>
> /// The main entry point for the application.
> /// </summary>
> [STAThread]
> static void Main()
> {
> DictionaryWrapper<string, int> wrapper = new
> DictionaryWrapper<string, int>();
> wrapper["key1"] = 10;
> wrapper["key2"] = 15;
> Console.WriteLine( "Sum of values = " + ( wrapper["key1"] +
> wrapper["key2"] ) );
> }
> }
> }
>
>
> "pamela fluente" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I have been posting this question with no success. I do not know if I
>> am not being clear of the question is too difficult :-)) or unclear.
>> It seems to me that the need to wrap a collection is quite common in
>> real world programs.
>>
>> I am having problem to understand how I can wrap collections in
>> System.Collections.Generic.
>> For example I want to wrap a System.Collections.Generic.Dictionary. I
>> wish the wrapping class to have a constructor similar to that of the
>> dictionary (besides some possible other argument).
>>
>> I think it should be actually simple, but I cannot see the right
>> syntax to implement this idea. Could anyone suggest how ?
>>
>> Intuitively I want something "like" (please * excuse me * if I use the
>> VB notation, which I am familiar with, I will have no problem to
>> understand any C# solution to my question )
>>
>> '------------------------------------
>> Class DictionaryWrapper
>>
>> Public Whatever As WhateverObject
>>
>> Public MyDict As System.Collections.Generic.Dictionary(Of
>> MyObjectType1, MyObjectType2)(MyComparer)
>>
>>
>> Sub New(ByVal MyObjectType1 as type?, ByVal MyObjectType2 as
>> type?, ByVal MyComparer as IComparer , ByVal WhateverObj as
>> WhateverObject )
>>
>> Me.MyDict = New System.Collections.Generic.Dictionary(Of
>> MyObjectType1, MyObjectType2)(MyComparer)
>>
>> Me.Whatever = WhateverObj
>>
>> End Sub
>>
>> End Class
>> '------------------------------------
>>
>> Thanks
>>
>>
>> -Pam
>>

>
>



 
Reply With Quote
 
pamela fluente
Guest
Posts: n/a
 
      9th Feb 2007
On 9 Feb, 21:43, "Ollie Riches" <ollie_ric...@hotmail.com> wrote:
> Pam,
>


Thank you Ollie , Thanks Ashot

finally I got it. Ashot code is what I was looking for. I was missing
the possibility to indicate the type after the class keyword. Here it
is in vb:

Imports System.Collections.Generic

Public Class Form1

Private Class DictionaryWrapper(Of Key, Value)

Private m_dict As New Dictionary(Of Key, Value)()

Default Public Property Item(ByVal key As Key) As Value
Get
Return m_dict(key)
End Get
Set(ByVal value As Value)
m_dict(key) = value
End Set
End Property

' Define whatever other public methods you need that utilize
m_dict.
End Class

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Dim wrapper As New DictionaryWrapper(Of String, Integer)()
wrapper("key1") = 10
wrapper("key2") = 15

Me.Text = "Sum of values = " + (wrapper("key1") +
wrapper("key2")).ToString

End Sub

End Class


thank you very much

 
Reply With Quote
 
Ashot Geodakov
Guest
Posts: n/a
 
      9th Feb 2007
Hi Pamela,

In the following lines:

class DictionaryWrapper<Key, Value>
{
private Dictionary<Key, Value> m_dict = new Dictionary<Key, Value>();

That's where the wrapper passes the types that it accepts straight to the
internal Dictionary object (note <Key, Value> pairs). Key is a type, Value
is a type.

Then when you create the wrapper object, you specify the exact types that
you want it to handle:

DictionaryWrapper<string, int> wrapper = new DictionaryWrapper<string,
int>();

Note <string, int> pairs: string is the type for keys, int is the type for
values. Of course you can do with other types of your choice.

<string, string>, <int, string>, <int, long>, etc...



"pamela fluente" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 9 Feb, 20:53, "Ashot Geodakov" <a_geoda...@hotmail.com> wrote:
>> Hi Pamela,
>>
>> here's a small sample based on what I could understand from your
>> requirements:
>>
>> using System;
>> using System.Collections.Generic;
>> using System.Windows.Forms;
>>
>> namespace WindowsApplication1
>> {
>> class DictionaryWrapper<Key, Value>
>> {
>> private Dictionary<Key, Value> m_dict = new Dictionary<Key,
>> Value>();

>
>
> Thanks Ashot .
>
> My problem is I do not know how to pass the types that are declared
> externally (as parameters for the wrapper constructor)
> for the Key and the Value to the internal typed dictionary
>
> System.Collections.Generic.Dictionary(Of MyObjectType1, MyObjectType2)
> (MyComparer)
>
> I do not see any type declaration in your code. I am not sure
> whether you have provided
> the answer to what I was looking for ...
>
> Perhaps c# and vb are too different here or I do not get it ...
>
> mmm I am having hard time with this one.
>
> Anyone more suggestions ?
>
> -Pam
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
REPOST: HTML Issue/Bug - Email Data Collection in Access 2007 RDB3 Microsoft Access Forms 0 10th Apr 2008 12:30 AM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft ASP .NET 1 18th May 2007 10:24 AM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft Dot NET 1 18th May 2007 10:24 AM
How to make a wrapper for a COLLECTION (repost) pamela fluente Microsoft VB .NET 0 7th Feb 2007 03:37 PM
How to get PropertyValueChanged from a Collection editor? (Repost) Michael Kairys Microsoft Dot NET Framework Forms 0 20th Jun 2005 06:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:45 PM.