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

  • Thread starter Thread starter pamela fluente
  • Start date Start date
P

pamela fluente

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
 
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"] ) );
}
}
}
 
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
 
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 said:
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 said:
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
 

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
 
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top