Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have this code:
C#
public class DB
{
public Dictionary<string,object> DATA = new Dictionary<string,object>();
public DB()
{
this.DATA = new Dictionary<string,object>();
}
public override string ToString()
{
    return DATA.Count.ToString();
}
}

I wanting to override the Dictionary<string,object> as object just like the "override string ToString()" like the codes below:
C#
void main()
{
DB value = new DB();
value.Add("Attr1", new ClassA());
value.Add("Attr2", new ClassB());
value["Attr1"] = new ClassC();
}


But for now, I only able to do this instead of above:
C#
void main()
{
DB value = new DB();
value.DATA.Add("Attr1", new ClassA());
value.DATA.Add("Attr2", new ClassB());
value.DATA["Attr1"] = new ClassC();
}



Is there any solution to that or similar to it?
Posted
Updated 24-Jan-15 5:46am
v4
Comments
Thomas Daniels 24-Jan-15 8:40am    
The Dictionary in your code block only has one generic type parameter -- is that intentional? (if not, please edit your question to include the second one)
BillWoodruff 24-Jan-15 10:35am    
This is probably the result of a very common error by the CP editor when formatting pasted-in code: arguments in brackets disappear.
BillWoodruff 24-Jan-15 10:38am    
I see nothing wrong with your code other than the obvious missing generic Type parameter. 'Count will return the number of KeyValue Pairs in the Dictionary, and you do the right thing to override ToString, convert the Count to a string and return it.
Sergey Alexandrovich Kryukov 24-Jan-15 12:36pm    
Start with this: of course you can override the property, but why doing in it in your case?
How the overridden class would be different? All you can do is to replace one dictionary type (complete type, obtained from the generic class) by the dictionary of exact same type. What will be changed? How can it be different from just adding different data to the same dictionary?
—SA
BillWoodruff 25-Jan-15 0:09am    
I see no reason to add another solution here because I think you now know all the possible ways (with the exception of using the 'implicit operator) to achieve having your class behave AS a Dictionary.

imho, you are much better off just making your class inherit from Dictionary: that way you are sure that EVERY property and method of a generic Dictionary is exposed by your class.

Well, you can't override a field. But I think you want an indexed property - but you will still need to implement the Add() methods. Take a look here: https://msdn.microsoft.com/en-us/library/aa288464%28v=vs.71%29.aspx[^].

[Update1]
If yor only need is to oveddide ToString, override it simply:
C#
public class DB : Dictionary<string,object>
{
public override string ToString()
{
    return this.Count.ToString();
}
}

[Update2]
See how real encapsulation and index property could be used in this case. Exactly as you wanted:
C#
public class DB
{
    private Dictionary<string,object> DATA = new Dictionary<string,object>();
    public DB()
    {
        this.DATA = new Dictionary<string,object>();
    }

    public string ToString()
    {
        return DATA.Count.ToString();
    }

    public void Add(string index, object value)
    {
        DATA.Add(index, value);
    }

    public object this[string index]
    {
        get
        {
            if(DATA.ContainsKey(index))
            {
                return DATA[index];
            }
            else
            {
                return null;
            }
        }
        set
        {
            if(DATA.ContainsKey(index))
            {
                DATA[index] = value;
            }
            else
            {
                DATA.Add(index, value);
            }
        }
    }
}

void Main()
{
    DB value = new DB();
    value.Add("Attr1", 1);
    value.Add("Attr2", 2);
    value["Attr1"] = 3;
    value["Attr3"] = 4;

    Console.WriteLine(value.ToString()); // 3
    Console.WriteLine(value["Attr1"]);// 3
    Console.WriteLine(value["Attr2"]);// 2
    Console.WriteLine(value["Attr3"]);// 4
    Console.WriteLine(value["Attr4"]);// null
}
 
Share this answer
 
v5
Comments
BillWoodruff 24-Jan-15 10:42am    
Hi Zoltan, In this case the OP is over-riding a method in a very standard way; their code looks fine to me.
Zoltán Zörgő 24-Jan-15 12:00pm    
Yes, but if you look at the desired usage, you will see that OP wants to hide the inner dictionary property.
BillWoodruff 25-Jan-15 0:01am    
Good point ! I have voted your response a #4. I do think your code here is more complicated than necessary, and you are defining default behaviors that are not truly consistent with the Class behaving AS a Dictionary. A "real" Dictionary would throw an error if you tried to access a Key that was not present.
Zoltán Zörgő 25-Jan-15 1:08am    
Fair enough :)
spaika 24-Jan-15 11:51am    
So the type still unable to override? Yes, the codes just fine, but I'm not sure unless if I use extend concept, but have not tried it yet.

Like:

public class DB
{
...
}

to:

public class DB : Dictionary<string,object>
{
...
}


edit: solved, solution already posted below
use extend

C#
public class DB 
{
public Dictionary<string,object> DATA = new Dictionary<string,object>();
public DB()
{
this.DATA = new Dictionary<string,object>();
}
public override string ToString()
{
    return DATA.Count.ToString();
}
}



to:

C#
public class DB : Dictionary<string,object>
{
public Dictionary<string,object> DATA = new Dictionary<string,object>();
public DB()
{
this.DATA = new Dictionary<string,object>();
}
public override string ToString()
{
    return DATA.Count.ToString();
}
}
 
Share this answer
 
Comments
Zoltán Zörgő 24-Jan-15 12:05pm    
Yes, it is working, but see for yourself: you embed a dictionarry in an other dictionarry. Ugly :(
What about simply overriding ToString?

public class DB : Dictionary<string,object>
{
public override string ToString()
{
return this.Count.ToString();
}
}

</string,object>
spaika 24-Jan-15 12:23pm    
Yep you're right, the values are embedded in the other base's Dictionary, not the current class's Dictionary, which the "Dictionary<string, object=""> DATA" is pointless.

I'm quite sure, maybe the abstract & base probably works, but still cant figure it out how since I'm not good enough with the abstracts and base. Kind confusing how to manage and relate the DATA as base's Dictionary so that I able to use both:

DB value = new DB();
value.Add("Attr1", (String)"HELLO WORLD");
Console.WriteLine(value.DATA["Attr1"].ToString());
Console.WriteLine(value["Attr1"].ToString());

Result:
HELLO WORLD
HELLO WORLD

The flow suppose to be like:
(start) > DB : Dictionary > DATA > base > (end)
or :
(start) > DB : Dictionary > base > DATA > (end)
Zoltán Zörgő 24-Jan-15 12:42pm    
See second update
spaika 24-Jan-15 13:05pm    
aw nice :D thanks, totally solved and that's the exactly I wanted :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900