473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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

Feb 9 '07 #1
5 4650
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, Valuem_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, intwrapper = new
DictionaryWrapper<string, int>();
wrapper["key1"] = 10;
wrapper["key2"] = 15;
Console.WriteLine( "Sum of values = " + ( wrapper["key1"] +
wrapper["key2"] ) );
}
}
}
"pamela fluente" <pa***********@libero.itwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
>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

Feb 9 '07 #2
On 9 Feb, 20:53, "Ashot Geodakov" <a_geoda...@hotmail.comwrote:
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, Valuem_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

Feb 9 '07 #3
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" <a_********@hotmail.comwrote in message
news:e3**************@TK2MSFTNGP02.phx.gbl...
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, Valuem_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, intwrapper = new
DictionaryWrapper<string, int>();
wrapper["key1"] = 10;
wrapper["key2"] = 15;
Console.WriteLine( "Sum of values = " + ( wrapper["key1"] +
wrapper["key2"] ) );
}
}
}
"pamela fluente" <pa***********@libero.itwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
>>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


Feb 9 '07 #4
On 9 Feb, 21:43, "Ollie Riches" <ollie_ric...@hotmail.comwrote:
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

Feb 9 '07 #5
Hi Pamela,

In the following lines:

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

That's where the wrapper passes the types that it accepts straight to the
internal Dictionary object (note <Key, Valuepairs). 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, intwrapper = new DictionaryWrapper<string,
int>();

Note <string, intpairs: 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" <pa***********@libero.itwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
On 9 Feb, 20:53, "Ashot Geodakov" <a_geoda...@hotmail.comwrote:
>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, Valuem_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

Feb 9 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
4
by: Maria | last post by:
How can I display the items in a collection in a listbox? Then, what is the code to remove an item from the collection when the item is selected in the listbox ? Thanks for all help!! Maria
4
by: Darrel | last post by:
I'm creating a table that contains multiple records pulled out of the database. I'm building the table myself and passing it to the page since the table needs to be fairly customized (ie, a...
15
by: Joe Fallon | last post by:
I would like to know how you can figure out how much memory a given instance of a class is using. For example, if I load a collection class with 10 items it might use 1KB, and if I load it with...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
28
by: Michael Primeaux | last post by:
What is the recommended pattern for implementing a synchronized (thread-safe) class that inherits from Collection<T>? For example, I want to implement a SyncRoot property . I do see where I can...
0
by: pamela fluente | last post by:
Wrapping the old classic collections was straightforward. But with System.Collections.Generic we have that particular constructor (Of SomeType...) and I am not clear how I can wrap these typed...
6
by: VK | last post by:
I must be missing something very obvious, but my nightly head doesn't work anymore. Press "Insert" button to add <insnodes after each <br>. Now press "Delete" - only even <insare being removed....
19
by: Mitesh | last post by:
Hi all, I have the following code: $req = "SELECT * FROM table1"; $res = mysql_query($req); if(!$res) return;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.