Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8866629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:50:15+00:00 2026-06-14T16:50:15+00:00

I have a Dictionary of CParam with Register as the Key, CParam has one

  • 0

I have a Dictionary of CParam with Register as the Key,

CParam has one field that is read from external text file and Description is used as a key to read in HumanDesc.

Text file is a translation file and Description has to be a string.
Something Like this

PLACE_HOLDER1 "First Place where things are put"
PLACE_HOLDER2 "Secod Place where things are put"
.....

I can easily do this by inserting Register as and putting it in quotes. But There is a 100’s of register and it would be tedious(and not very elegant).
Is there a way that constructor could handle that for me.

Below is a very simplified example of what I ma trying to do:

using System;
using System.Collections.Generic;
namespace Var2String
{
    public class CParam
    {
    public ushort Register;
    public string Description;
    public ushort Content;
    public string HumanDesc;
    public CParam(ushort t_Register, string t_Description, string t_HumanDesc, ushort DefaultVal)
    {
        Register = t_Register;
        Description = t_Description;
        Content = DefaultVal;
        HumanDesc = t_HumanDesc;
    }
};

static class Device1
{
    public const ushort PLACE_HOLDER1 = 0x0123;
    public const ushort PLACE_HOLDER2 = 0x0125;
    public const ushort PLACE_HOLDER_SAME_AS_1 = 0x0123;
    public static Dictionary<ushort, CParam> Registers;
    static Device1()
    {
        Registers = new Dictionary<ushort, CParam>()
     {
       {PLACE_HOLDER1, new CParam(PLACE_HOLDER1,"PLACE_HOLDER1","Place One Holder",100)},
       {PLACE_HOLDER2, new CParam(PLACE_HOLDER1,"PLACE_HOLDER2","Place Two Holder",200)}
     };
        /*
         * Like to be able to do this
         * And constructor CParam
          Registers = new Dictionary<ushort, CParam>()
     {
       {PLACE_HOLDER1, new CParam(PLACE_HOLDER1,"Place One Holder",100)},
       {PLACE_HOLDER2, new CParam(PLACE_HOLDER1,"Place Two Holder",200)}
     };
        */
    }

}
class Program
{
    static private string LookUpTranslationFor(string Key)
    {
        string Translated = "Could not find Val for " + Key;
        //This would read XML file use Key to get translation
        return Translated;
    }
    static void Main(string[] args)
    {
        Console.WriteLine(Device1.Registers[Device1.PLACE_HOLDER1].HumanDesc);
        Console.WriteLine(Device1.Registers[Device1.PLACE_HOLDER2].HumanDesc);
        Device1.Registers[Device1.PLACE_HOLDER2].HumanDesc = LookUpTranslationFor(Device1.Registers[Device1.PLACE_HOLDER2].Description);
        Console.WriteLine(Device1.Registers[Device1.PLACE_HOLDER2].HumanDesc);
        Console.ReadKey(true);
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:50:16+00:00Added an answer on June 14, 2026 at 4:50 pm

    I’m not sure what i understand you right, but if you register variables is not const you can do something like this:

    1.Add another constructor to CParam

    public class CParam
    {
        ....
    
        public CParam(Expression<Func<ushort>> ex, string t_HumanDesc, ushort DefaultVal)
        {
            Content = DefaultVal;
            HumanDesc = t_HumanDesc;
            Description = ((MemberExpression) ex.Body).Member.Name;
            Register = ex.Compile().Invoke();
        }
    };
    

    2.Chage your device class like this:

    internal static class Device1
    {
        public static ushort PLACE_HOLDER1 = 0x0123;
        public static ushort PLACE_HOLDER2 = 0x0125;
        public static ushort PLACE_HOLDER_SAME_AS_1 = 0x0123;
        public static Dictionary<ushort, CParam> Registers;
    
        static Device1()
        {
            Registers = new Dictionary<ushort, CParam>()
                            {
                                {PLACE_HOLDER1, new CParam(() => PLACE_HOLDER1, "Place One Holder", 100)},
                                {PLACE_HOLDER2, new CParam(() => PLACE_HOLDER1, "Place Two Holder", 200)}
                            };
        }
    }
    

    Note what this would not work with const variables!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary that has keys associated with lists. mydict = {'fruits': ['banana',
I have dictionary entries that are read to a Dictionary <string,string> myDict=null; The entries
I have a dictionary that I normally access with a key, so I need
I have C# Dictionary and I want create a .csv file from it. For
I have Dictionary from string key i want to get Value of corresponding key
I have a dictionary table called car_status that has an 'id' column and a
I have dictionary which has {key : []} format. I need to prepare a
Have Dictionary <Int64, byte> that gets used a lot. I mean in a loop
Does VBA have dictionary structure? Like key<>value array?
I have a Dictionary where the key and value are both strings. It's possible

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.