C# - Reference Type Key In A Generic Dictionary In .Net?

Mar 16, 2010

I have a mutable class that I'm using as a key to a generic dictionary. Two keys should be equal only if their references are equal. From what I've read, in this case, I don't need to override Equals, GetHashCode , or implement IEqualityComparer.

View 5 Replies


ADVERTISEMENT

.net - Filter Custom Dictionary With LINQ ToDictionary - "Unable To Cast Object Of Type 'System.Collections.Generic.Dictionary`2"

Jul 7, 2010

I have created a Dictionary class (MyDictionary for the example). I am currently trying to pass MyDictionary into a function, filter it into a new instance of MyDictionary and pass this new instance into another method. When I am attempting to create the second instance from the filtered first instance of MyDictionary via Lambda Expressions and the ToDictionary Method, I am getting the following error:

Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.Int32,System.String]' to type 'MyDictionary'. I have simplified the example and recreated it in LINQPad and am getting the same error.

Here's the simplified version of my code:

[Code]...

View 2 Replies

.net - Can A Generic Version Of A Derived Class Override A Base Method Using The Generic Type

Apr 13, 2012

Consider:

Public MustInherit Class Column
Public ReadOnly Property ReturnSomethingUseful() As Object
Get
'return something useful

[code]....

But this gives the following error:

Public Overrides Function ParseValue(sValue As String) As Boolean'
cannot override 'Public Overridable Function ParseValue(sValue As String) As Object'
because they differ by their return types.

I accept that you can't do this, but I'd like to be able to preserve the semantics of what I'm. trying to do, which is to have an untyped version that deals with Object, but a typed version in derived classes that knows about the specific type T.

View 2 Replies

Asp.net - Generic Dictionary In Session .Net?

Aug 31, 2011

I have a generic class called SessionManager to control the types of variables I store in session. I have been doing it for String, Integer, List(Of T) without any issues like this.

Public NotInheritable Class SessionManager
'''<remarks>
'''Private constructor to prevent instantiation of, or inheritance from, this class.

[code].....

I would like to do the same for Dictionary(Of T, T). I would like 2 generic private methods for Dictionary(Of T, T) for getting and setting; GetFromSessionAsDictionary and SetInSessionAsDictionary. (When nothing returns empty dictionary like the List(Of T) properties and sets in session) And this should allow me to create as many public properties of Type Dictionary (e.g. Dictionary(Of String, String), Dictionary(Of String, Integer)) as possible to store in session.

EDIT:This is what I have ended up doing after trying to make it generic Dictionary(Of T, T), and it works without any issues. What I want instead is a solution that would not constraint the type to Dictionary(Of String, Integer).

Private Const _Dictionary1OfStringInt As String = "DICTIONARY_1_OF_STRING_INT"
Public Shared Property DictionaryOf As Dictionary(Of String, Integer)
Get

[code].....

View 1 Replies

Code - Way To Do With A Generic Dictionary ?

Jul 9, 2009

You can do this

Code:
Dim Test() As String = {"Stuff", "Stuff", "Additional Stuff"}

Is there a way to do that with a generic dictionary? Also, is there a name for that?

View 4 Replies

LINQ And Generic Dictionary?

Jan 2, 2010

As I am beginner in LINQ, I have small problems. I have dictionary of Char and Long, and I would like to use LINQ to retrieve Char with highest Long Value. I saw some C# samples, which look pretty easy, but I can't achieve such expression in VB.Here are two expressions, and I don't think they are most efficient possible.

[Code]...

View 14 Replies

.net - Determining A Generic Type At Runtime In Non-Generic Class

Aug 14, 2010

I have a Journal that records entries for different types: Journal(Of ParentT)

[Code]....

View 2 Replies

Access Generic Dictionary Contained Or Nested In Another One?

Nov 17, 2009

I have a Generic Dictionary contained or nested as a child in another Generic Dictionary. I need to be able to access the child dictionary objects by key. When I attempt to do this I get the following error:
"Value of type 'System.Collections.Generic.KeyValuePair(Of String, String)' cannot be converted to 'System.Collections.Generic.Dictionary(Of String, String)'."

So it seems that the contained Generic Dictionary is being returned as a Generic KeyValuePair when accessed via a key as opposed to iterating through the parent dictionary in a For/Each loop.

The first section of the sample works OK (using a key to access a string value in a dictionary).
The second section of the code works OK (iterating through the dictionary values in a For/Each).
The third section of the sample is where the error occurs (using a key to access a nested generic dictionary...see underlined code).

Dim td1 As Dictionary(Of String, String)
For Each thisKey As String In td1.Keys
Debug.Print(td1(thisKey))
Next
Dim td2 As Dictionary(Of String, Dictionary(Of String, String))
[Code] .....

View 1 Replies

Binding Combo Boxes To Generic Dictionary?

Feb 19, 2011

I'm having problems binding 2 combo boxes to a generic dictionary, this is what i have done so far

Dim cb As ComboBox = sender
Dim name As String = cb.Name.ToString
Dim dict As New Dictionary(Of String, List(Of String))

[Code]....

View 2 Replies

Create A Generic Dictionary That Implements IXmlSerializable ?

Apr 19, 2010

I was trying to create a generic Dictionary that implements IXmlSerializable (credit to Charles Feduke).

Here is my trial:

Sub Main()
Dim z As New SerializableDictionary(Of String, String)
z.Add("asdf", "asd")

[Code].....

View 2 Replies

System.Collections.Generic.Dictionary `Add` Vs Set `Item`

May 20, 2011

If i wish to put items into a System.Collections.Generic.Dictionary, I can either Add or set the Item.I know if you do Add it helps you check if the key already exists and if not it throws an exception.Now I'm curious, so if I'm adding a ton of items, should I prefer setting Item instead of Add since Add does unnecessary checks that may actually slow things down?

View 3 Replies

.net - Creating A Generic List Of A Generic Type

Jun 30, 2011

I am trying to create a list of a generic type in vb.net 2.0 framework.
This is the generic type definition:

Public Class GenericParamMap(Of T)
Public Sub New(ByVal pParamName As String, ByVal pPropValue As T)
mParamName = pParamName

[Code]....

The compiler does not allow a "T" in the method's parameter because it's not defined, but I'm not sure how or where to define it. I thought it was okay to have a generic method definition.

View 2 Replies

Generic Cannot Instantiate Instance Of The Generic Type With New?

Feb 8, 2012

I am trying to write a generic method, to avoid code duplication, which will create or activate a Form as an MDI children, based on its type. But I have to lines in error (see comments).

[Code]...

View 4 Replies

Insert Into A Generic Dictionary With Possibility Of Duplicate Keys?

Apr 2, 2010

Is there any reason to favor one of these approaches over the other when inserting into a generic dictionary with the possibility of a key conflict? I'm building an in-memory version of a static collection so in the case of a conflict it doesn't matter whether the old or new value is used.

If Not mySettings.ContainsKey(key) Then
mySettings.Add(key, Value)
End If

[code].....

View 4 Replies

Warning: Type Library Exporter Encountered A Generic Type Instance In A Signature

May 5, 2009

I'm compiling a VB.Net 2.0 app (created in VS2008) using msbuild, and now I've added a generic return type, it's giving me the following:

Warning: Type library exporter encountered a generic type instance in a signature. Generic code may not be exported to COM.

Having just spent ages removing all of the previous warnings, I don't really want to add a new one. Any idea how to get rid of it (aside from not using generics)?I don't know what details I'd put in the attribute, or what number to put in the project-level ignore list.

View 1 Replies

Unable To Cast Object Of Type .objectquery To Type Generic.list

Sep 12, 2011

I have a question about ASP.Net, visual basic I have 2 LINQ query's, the first one works, the second one doesnt, produces a

"Unable to cast object of type 'System.Data.Objects.ObjectQuery'1[SelmaV2.Products]' to type 'System.Collections.Generic.List'1[System.String]'.

[Code]....

View 1 Replies

Complete Reference/dictionary For VB?

Aug 25, 2010

I downloaded Visual Basic 2010 Express recently. I did a few of the tutorials and now feel that I know the basics. I can start a project and know where to put code. I don't feel like buying a beginner's book and slowly learning all the different sections, keying in endless lines of code and learning things that I'm not interested in yet. I wish there was such a thing as a complete reference work of instructions, a dictionary or something. Then I could browse this and use the code words that I'm interested in. Also, books seem to only mention the most common code. I did find a reference on the Visual Basic Microsoft pages but it wasn't complete. Does anyone know where there is a complete reference?

If, on reading this, you think that my idea is misguided I don't mind being told.

View 4 Replies

Cast Value Type To Nullable Enumeration Type In Generic Object If All Types Are Unknown At Write-time?

Dec 14, 2011

I have a generic Class I'm using to hold information loaded from a database.I have a method which takes a DataRow as an argument, uses the object's known column name and extracts the data from the DataRow, such that:Dim loadData As T = CType(myDataRow("myColumnName"), T))works as my default assignment in most cases.Unfortunately, due to some horrifying design constraints, some of my columns may be null, and may also be taken from enumerations.This means that when <T> is Nullable(Of SomeEnumeration) the above code does not work because I can't cast 0 directly to SomeEnumeration.Zero.Is there some way to check whether <T> is Nullable(Of [Enum])? Or some way to write a method which allows Integers to be cast to Nullable(Of [Enum])?I feel like I'm forgetting something that would allow me to write one of the other of these, but my weak google-fu is turning up nothing.

EDIT: Okay, thanks to dasblinkenlight's answer below, I can detect when this circumstance is occurring, but what I need to do now is to take a type <T> which I know is Nullable(Of SomeClass), get a type reference to SomeClass and then create a new object of type Nullable(Of SomeClass) and assign that to LoadData.My problem was that I had a lot of difficulty in finding any function which would accept baseType as an actual Type.Parse accepted baseType as a parameter, I knew baseType was an [Enum] type because of dasblinkenlight's code, so I was, in this instance, able to code a solution. It's a solution which is very specific to my problem (i.e., T is Nullable(of SomeEnumeration)), but it's a solution nonetheless.

View 2 Replies

Using An Interface Defined In A Generic Type From Within The Defining Type

Feb 14, 2012

In trying to add a bit of usage variety to a generic class I'm working on, I ran into this issue with trying to cast an object into an interface instance where the interface is defined inside the generic class.

[Code]...

View 1 Replies

VB Allows The Type Parameters To Be Used As The Base Class For The Generic Type?

Mar 30, 2010

1) VB Allows non-type template parameters2) VB supports explicit specialization 3) VB allows the type parameters to be used as the base class for the generic type4) VB allows a generic type parameter itself to to be a generic 5) VB enforces that all codes are valid for all types of parametrs

View 1 Replies

Getting A Reference To A Generic List Item?

Jun 2, 2011

Does anybody know how to pass a reference to the current item in a generic list, when processing that list? In VB.NET you have to use a delegate wrapper to pass parameters in a delegate, which I have set up OK, but I don't know how to get a reference to the current item that would be the parameter used at in the example below. Using the Current property of the list enumerator doesn't seem to work, but how else could this be done?

[Code]...

View 1 Replies

Unable To Cast Object Of Type 'System.Collections.Generic.List`1[System.Int32]' To Type 'crmWebService.ArrayOfInt'?

May 14, 2012

I'm getting an error after sending a list to a web service.This is the code that is calling the web service:

Dim sProgramInterest As New List(Of Integer)
crmService.InsertProspectGetId(sProgramInterest.ToList)
But I'm getting this error.

[code].....

View 3 Replies

C# Dictionary Has No Value Type Option?

Aug 9, 2010

I am coverting portions of a code project article [URL] to VB.Net for my own uses but I've run across a piece of C# code written in a way that I've never seen before and which I don't know how to convert:

private static Dictionary<type,> _cachedIL = new Dictionary<type,>();

To me this looks like a dictionary decleration with no type specified for the value. Later on in the same code block the author returns what I believe is a delegate that does something that looks syntactically similar:

return ((Func<t,>)myExec)(myObject);

View 3 Replies

Using Pointer To Type In Dictionary Value?

Feb 23, 2011

This small snippet of code is to increment a count value (integer) which is stored in a dictionary using my referenced object as a key. When the dictionary is small, multiple lookups aren't a big deal but this particular dictionary can get quite large.

Private RefCount As IDictionary(Of ILifeTimeManaged, Integer)
......... CODE HERE.....
Private Sub IncrementRefCount(ByVal entity As ILifeTimeManaged)
Dim prevCount As Integer
''# if we have no reference entry, add one and set its count to 1
[Code] .....

I find a corresponding dictionary entry then increment the int stored in the value, or add a new dictionary entry. Is it a bad idea to use a pointer to the dictionary value? Then I can avoid the second key lookup when I already have gotten the value. How would you implement it? Is this even possible in .NET4? Can I do it using IntPtr do you think? [URL]
RefCount.Item(entity) = prevCount + 1

View 2 Replies

Unable To Cast Object Of Type 'ImagesFlag' To Type 'System.Collections.Generic.IEnumerable`1[ImagesFlag]'

Jan 7, 2012

And here is the code that produces the error

CODE:

How can i cast this one?

View 1 Replies

Reference - Does .Net 2003 Support System.Collections.Generic

Sep 21, 2011

I am planning to use a List in my application but when I was searching for System.Collections.Generic, it only has System.Collections. I tried to look it up in the "add reference" and its not there.

View 2 Replies

Local (Generic) Data Directory Reference For Excel Files?

Dec 8, 2011

I can reference the local (generic) data directory for an SQL file as follows:

Dim
T01Database As SqlCeConnection =
New SqlCeConnection("Data
Source=|DataDirectory|Test File.sdf")

I get a "file not found" error when trying the same syntax for referencing an Excel file:

Dim
T01Workbook As Microsoft.Office.Interop.Excel.Workbook
= T01Application.Workbooks.Open("|Data Directory|Test File.xlsx"
)

correct syntax for referencing an Excel file in this manner?

View 1 Replies

How Are Value Type Properties In A Reference Type Class Allocated

Sep 11, 2009

In VB.NET, if I create a class it is a reference-type. But, if that class it chock full of value type properties, how is this handled? If the class is instantied but never filled, I suspect a pointed for to the heap is allocated. But is more space allocated on the stack for all of it's value type properties?

View 4 Replies

Returning Subset Of Dictionary With A Complex Type As A Key With Linq

Oct 5, 2010

I have a collection as follows

Private _bankRates As Dictionary(Of RateSourceBank.Key, RateSourceBank)

Where RateSourceBank.Key is simply

Public Class Key
Public RateType As String
Public EffectiveDate As DateTime

[Code].....

View 1 Replies

Generic Declaration Using Type?

Jul 31, 2009

I'm trying to declare a generic using a variable of type System.Type, with VB 2005, and haven't found the right approach. Consider the typical:

Dim m_GenObj As MyGenericObject(of T)
Instead of using "T" I want to use a variable of System.Type, for something like:
Dim m_MyType as Type
Dim m_GenObj as MyGenericObject(of m_MyType)

But of course that doesn't work.

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved