NameValueList question

NameValueList question

Old forum URL: forums.lhotka.net/forums/t/11289.aspx


griff posted on Tuesday, April 10, 2012

Hi

Noticed NaveValueList only seems to accept an <int, string> - so there appears no facility to handle a key value other than int - how do we handle this e.g. when the key is say a guid or string?

Richard

msrs_it replied on Tuesday, April 10, 2012

Hi,

The NameValueList will accept any datatypes. consider the following example below.

using System;
using Csla;

namespace NameValueListExample
{
    class Program
    {
        public static void Main(string[] args)
        {
            //Console.WriteLine("Hello World!");
            
            var userList = UserList.GetList();
            foreach (var element in userList) {
                Console.WriteLine(string.Format("Key: {0}, Value: {1}",element.Key,element.Value));
            }
            
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
    
    public class UserList : NameValueListBase<Guid,string>
    {
        public static UserList GetList()
        {
            return DataPortal.Fetch<UserList>();
        }
        
        private void DataPortal_Fetch()
        {
            var rlce = RaiseListChangedEvents;
            RaiseListChangedEvents=false;
            IsReadOnly=false;
            for(int i =0; i<10;i++)
            {
                Add(new NameValuePair(Guid.NewGuid(), string.Format("User{0}",i+1)));
            }
            IsReadOnly=true;
            RaiseListChangedEvents=rlce;
        }
    }
}

 

The program output will be like this

Key: df9bf737-397e-452a-82bd-9237e29a496d, Value: User1

Key: 2602d50d-cb34-44b4-86c4-5f2384a36517, Value: User2

Key: 3433a71b-5973-4b75-8ad5-4c76ec384639, Value: User3

Key: 13546196-5a5f-4caf-ab55-e4eae9a2b93d, Value: User4

Key: cd29cd22-6030-4bac-b82f-f4f5450f551e, Value: User5

Key: 251eaa69-c66f-4381-a7c4-5807a9911be2, Value: User6

Key: a73e7417-f520-4cc4-a332-f2f0f1e38339, Value: User7

Key: 765358aa-06e1-46b0-b28c-92691b0a7072, Value: User8

Key: cfaf159e-23e1-4116-9de5-4bb28df9d64d, Value: User9

Key: 83b2641e-77a6-434c-9e72-5e1d27dbcaac, Value: User10

Press any key to continue . . .

griff replied on Wednesday, April 11, 2012

This is what I thought but I am getting compilation errors.....

D:\Users\Richard\Development\VS2010\SLApps\WACA\WACA.Business\Collections\NVL\RoleList.cs(129,25): error CS1502:

The best overloaded method match for 'Csla.NameValueListBase<int,string>.NameValuePair.NameValuePair(int, string)' has some invalid arguments

D:\Users\Richard\Development\VS2010\SLApps\WACA\WACA.Business\Collections\NVL\RoleList.cs(129,43): error CS1503:

Argument 1: cannot convert from 'System.Guid' to 'int'

for the line of code:

 Add(new NameValuePair(Guid.NewGuid(), string.Format("User{0}", i + 1)));

Any reason for this?


 

 

JonnyBee replied on Wednesday, April 11, 2012

The type of NameValuePair must match the definition of the class:

    public class UserList : NameValueListBase<Guid,string>

griff replied on Wednesday, April 11, 2012

Thanks Jonny Bee - spot on (thanks  also SreeRamaSaran)


Copyright (c) Marimer LLC