0

Re-written everything exactly as my program has it:

class DictionaryInitializer
{
    public class DictionarySetup
    {
        public string theDescription { get; set; }
        public string theClass { get; set; }
    }
    public class DictionaryInit
    {
        //IS_Revenues data
        public Dictionary<int, DictionarySetup> accountRevenue = new Dictionary<int, DictionarySetup>()
            {
                { 400000, new DictionarySetup {theDescription="Call", theClass="Revenues"}},
                { 400001, new DictionarySetup {theDescription="Bill", theClass="Revenues"}},
                { 495003, new DictionarySetup {theDescription="Revenue", theClass="Revenues"}}
            };


        public Dictionary<int, DictionarySetup> accountExpenses = new Dictionary<int, DictionarySetup>()
            {
                {790130, new DictionarySetup { theDescription="Currency Hedge", theClass="Other income/expense"}},
                {805520, new DictionarySetup { theDescription="Int Income", theClass="Other income/expense"}}
            };
}

On my mainform:

    DictionaryInit theDictionary;
    btnClick() {

    theDictionary = new DictionaryInit();
    //Some code to loop through a datagridview        
    //Somemore Code
                    foreach (var item in theDictionary.accountRevenue)
                    {
                        int theKey = item.Key;
                        if (theKey == keyCode)
                        {
                            DictionarySetup theValues = item.Value;
                            DGVMain.Rows[rowindex].Cells[3].Value = theValues.theDescription;
                            DGVMain.Rows[rowindex].Cells[11].Value = theValues.theClass;
                            DGVMain.Rows[rowindex].Cells[12].Value = "Sale of Services";
                            Recording(rowindex);
                        }
                    }
    }

Current work in progress:

                    DictionarySetup theValue;
                    if (theDictionary.accountExpenses.TryGetValue(keyCode,out theValue.theDescription) //[5]-> Account Type
                    {
                      //Some code to write dictionary data to the data grid view.

I'm working on making the TryGetValue and Contains(value) dictionary functions to work for now.

My current error messages are as follows:

"a property or indexer may not be passed as an out or ref parameter" when attempting the trygetvalue

and finally when trying the extension method i'm trying to create:

 "Inconsistent accessibility, Dictionary<int, DictionaryInitializer.DictionarySetup> is less accessible than the method DictionaryUse<int, DictionaryInitializer.DictionarySetup>"
Aroueterra
  • 336
  • 3
  • 18
  • `accountExpenses` or `accountRevenue`? Your code doesn't compile. Make it work first please. – Patrick Hofman Mar 27 '17 at 08:02
  • What compiler errors did u get? – Václav Struhár Mar 27 '17 at 08:02
  • 4
    "I wanted to make an extension method like this" Please check on the extension method syntax and requirements. – Patrick Hofman Mar 27 '17 at 08:03
  • 4
    What is it that you *really* need, it looks like you're trying to re-invent the wheel. For example your entire foreach can be replaced with `Dicationary.TryGetValue` – Ofir Winegarten Mar 27 '17 at 08:08
  • 1
    Well, I noticed I call the last code block I posted several times in my code, so I wanted to call it like DictionaryUse(1151, accountRevenue) or DictionaryUse(20159, accountExpense) where the second parameter is a Dictionary within DictionaryInit. Unfortunately, I don't know how to pass that. – Aroueterra Mar 27 '17 at 08:11
  • 2
    Pass in a `Dictionary`…? – poke Mar 27 '17 at 08:12
  • 3
    Why are you iterating a dictionary in search of a key instead of using `TryGetValue` or `ContainsKey`? – Pieter Witvoet Mar 27 '17 at 08:15
  • 1
    @PieterWitvoet Yes, my mistake. I merely used the example on MSDN. I'll see if those meet my requirements after I test them. – Aroueterra Mar 27 '17 at 08:18
  • @PieterWitvoet I'm trying to implement the TryGetValue or ContainsKey into my solution, however though it should be relatively simple, it seems that the way I structured my data, it is difficult to call it. With my current setup it throws: Argument 2: Cannot convert from `out int` to `out DictionaryInitializer.DictionarySetup`, updated the op with the code. – Aroueterra Mar 27 '17 at 08:46
  • 2
    @Arvayne `value` needs to be of the same type as the values inside your dictionary, i.e. `DictionarySetup`. – poke Mar 27 '17 at 08:52
  • I changed my value to `DictionarySetup theValue;` and wrote `TryGetValue(keyCode,out theValue.theDescription)` which is saying "a property or indexer may not be passed as an out or ref parameter". I think i'm doing it wrong. – Aroueterra Mar 27 '17 at 09:00
  • 1
    As the error states, you cannot pass in a property; you need a local variable which you pass as the out parameter. – poke Mar 27 '17 at 09:05
  • 1
    Sidenote: you may want to use more descriptive names. It looks like `DictionarySetup` holds financial account information, but its name implies something else entirely. And those `the` prefixes don't add any meaning (imho). It's already confusing me right now - imagine yourself having to read this code a couple months later. – Pieter Witvoet Mar 27 '17 at 09:17
  • 1
    As for your latest error, why are you passing `theValue.theDescription` as `out` parameter? `theDescription` is a property, and the error is telling you that you can't pass that as an `out` parameter. Did you check the type that `TryGetValue` expects? – Pieter Witvoet Mar 27 '17 at 09:18
  • @PieterWitvoet I'll make a new question for this attempt on the TryGetValue. For now, my question about creating an extension method has been answered successfully. Before I write that new question though, I'm gonna look over my code and see if I can find the answer from your suggestions. – Aroueterra Mar 27 '17 at 09:20

1 Answers1

1

You have to make your field public....

Dictionary<int, DictionarySetup> accountRevenue

should be

public Dictionary<int, DictionarySetup> accountRevenue

if you want to refer to it from outside the class..

This part seems to also be missing a variable name;

public void DictionaryUse (int code, int key, Dictionary)

should be

public void DictionaryUse (int code, int key, Dictionary<int, DictionarySetup> theDictionary)

But I agree with the other comments, you seem to be re-inventing the wheel, just use the existing Dictionary utility methods

Milney
  • 6,253
  • 2
  • 19
  • 33
  • Ah, yes. I forgot I needed to make these changes. My code actually works, I was just wondering how to add it to an extension method because I call this block alot. (I removed alot of unnecessary code, the only parameter I'm trying to pass to the extension method is the name of the dictionary within `DictionaryInit`, which has more than 1 dictionary. – Aroueterra Mar 27 '17 at 08:16
  • Ah, I'll test that last suggestion. Didn't know I needed to call all of its properties. – Aroueterra Mar 27 '17 at 08:17
  • Hmmm, I don't think the dictionary parameter in the `DictionaryUse` is throwing any more errors, however DictionaryUse is throwing 'inconsistent accessibility'. I don't know what that means. – Aroueterra Mar 27 '17 at 08:24
  • @Arvayne The error message should say more than just “inconsistent accessibility”. – poke Mar 27 '17 at 08:29
  • @poke Sorry, I was cutting corners. I posted it earlier as a comment to the OP. Inconsistent accessibility, Dictionary is less accessible than the method DictionaryUse – Aroueterra Mar 27 '17 at 08:31
  • I told you already.... Inconsistent accessibility means one of your properties/fields is public, but it references something private. Check your DictionarySetup class is public and your field accountRevenue is public. Or please post the whole exact text of the error message as it certainly doesn't say "Inconsistent accessibility, Dictionary<> is less accessible than Dictionary use" as you have suggested - Error messages in visual studio are usually not in broken english – Milney Mar 27 '17 at 08:35
  • @Milney, You're right it would seem. My DictionarySetup was public, so was my DictionaryInit class and well, everything else was public except the very top level DictionaryInitializer class.... thank you for being persistent. Though, new questions have arisen since. – Aroueterra Mar 27 '17 at 09:22