Multiple Types, one member

FreakBoy

Member
Joined
Jul 22, 2006
Messages
15
Location
Near Atlanta
Programming Experience
Beginner
I apologize if this has shown up before, I searched and could not find the correct thread... if it has been brought up I'd be happy to have a link to the relevant thread.

I am attempting to create a class which holds a Key/Value pair like a dictionary object but is limited to one pair.

The Key would always be of type String, while the Value could be of any type needed without using type Object.

The individual instances of this class are to be added to a strongly typed dictionary based collection.

"Thing" being the class with the single Key/Value pair
"Things" being the strongly type diction of type Thing

VB.NET:
Dim x1 as Thing
Dim x2 as Thing
Dim x3 as Thing

Dim xs as New Things

x1.Name = "x1Name"
x2.Name = "x2Name"
x3.Name = "x3Name"

x1.Value = 5
x2.Value = 5.3
x3.Value = "Five"

xs.add(x1)
xs.add(x2)
xs.add(x3)

I realize that it is possible I am over-thinking this, though in all of the projects I have created to help me learn more I keep running in to this same problem and haven't been able to come up with a solution.

Any help at is appreciated.
 
I am attempting to create a class which holds a Key/Value pair like a dictionary object but is limited to one pair.

The Key would always be of type String, while the Value could be of any type needed without using type Object.
Not possible

The individual instances of this class are to be added to a strongly typed dictionary based collection.
How do you expect to create a class that breaks the strong-type feature of the language, then refer to this class in a strongly typed way?

I realize that it is possible I am over-thinking this, though in all of the projects I have created to help me learn more I keep running in to this same problem and haven't been able to come up with a solution.

Any help at is appreciated.


Instead of telling us the problem with your solution, why dont you tell us the actual problem youre trying to solve?
 
Sorry about that.

I am trying to have a list of Key/Value pairs as a property of a class. The Key/Value pairs are supposed to be able to be used to display the Key and Value to the user on a form, as well as making the Value available for programmatic modification. The Value portion of the pair might be a String, Integer, Double, Boolean, etc.

I seem to keep running into this problem every time I start a new learning-project.

I feel as though I'm over thinking the logic here but I can't seem to see clearly through it.
 
Sorry about that.

I am trying to have a list of Key/Value pairs as a property of a class.

Why? The names and types of a class property are your concern (the developer) not the end user. It is likely the user doesnt know the difference between a numerical string and a number

The Key/Value pairs are supposed to be able to be used to display the Key and Value to the user on a form, as well as making the Value available for programmatic modification.
Again, I would ask why you are attempting this level of genericity and then transferring the skill of editing the value to something that is acceptable, to a person who is likely incapable of making that decision

The Value portion of the pair might be a String, Integer, Double, Boolean, etc.
Then it must derive from object. We dont usually bundle together such disparate things. Think about it. When you go to the supermarket, and walk down the aisle, do you see shelves with all products placed randomly? Bacon on top of oranges, next to peas and obscuring the whipped cream? No, we deal with things in collections of logical groups.

It still feels like youre telling us the problem with your solution, not the actual problem; you want a form that will edit any value of any type. WHY?

I seem to keep running into this problem every time I start a new learning-project.
If this problem recurs across multiple projects then I'm pretty dure the flaw is in your thinking? In 10+ year sof coding, I've only come across it once in the following sense:

I'm currently writing a reports app
A report has parameters that are one of 3 distinct types, a number, string, or date
Additionally, some parameters are from a fixed list of values, so the choice of colour is limited to {"red", "green", "blue"}
I have controls that code for editing of each type of value, and a master table associated named parameters (of which there may be 50) with the 7 types of editor that may be used (number, date, freetext, singlechoice text, multiplechoice text)
When a report is determined to have parameters a, b, x and z and the relevant editors are freetext, freetext, siglechoice and number, then those are the editors I show


It is a very rare case that things like this need be applied, and its usually in cases where the solution has to be hugely flexible to allow a downstream developer to program it further so the user can use it.. Still the user is prohibited from having to make sure that the value is correctly formatted, but it is a more involved system than the easy design time mode of whacking N number of controls on a form..
 
Why? The names and types of a class property are your concern (the developer) not the end user. It is likely the user doesnt know the difference between a numerical string and a number

The project is a very very simple game and I am anticipating an admin screen in which modifications can be made to the values. The main class is going to be a class representing a game character which will have a list of attributes and a list of skills.

Again, I would ask why you are attempting this level of genericity and then transferring the skill of editing the value to something that is acceptable, to a person who is likely incapable of making that decision

Since the project is mainly for me to learn more VB.NET the only person who is going to be editing will be me from the above mentioned admin screen within the game. I have no intentions of publishing the game at all.

Then it must derive from object. We dont usually bundle together such disparate things. Think about it. When you go to the supermarket, and walk down the aisle, do you see shelves with all products placed randomly? Bacon on top of oranges, next to peas and obscuring the whipped cream? No, we deal with things in collections of logical groups.

I understand the analogy. I am looking for any kind of solution under which I would be able to group together a list of attributes, for example "Age" and "Name" or "Weight" which could have varying Types but still qualify as Attributes of the character. Since the list of possible attributes could be different per character, I think I need to be able to pull their Key and Value for display dynamically on the form, but I am open to other suggestions on how I might get the same effect.

If this problem recurs across multiple projects then I'm pretty dure the flaw is in your thinking?

Absolutely. The only problem is that I don't know a different way of attacking the problem through ignorance.

In 10+ years of coding, I've only come across it once in the following sense:

I'm currently writing a reports app
A report has parameters that are one of 3 distinct types, a number, string, or date
Additionally, some parameters are from a fixed list of values, so the choice of colour is limited to {"red", "green", "blue"}...

snip

...It is a very rare case that things like this need be applied, and its usually in cases where the solution has to be hugely flexible to allow a downstream developer to program it further so the user can use it.. Still the user is prohibited from having to make sure that the value is correctly formatted, but it is a more involved system than the easy design time mode of whacking N number of controls on a form..

I'm hoping that my problem might be more easily corrected by your help. I'm positive that there is something I'm just not thinking of and need a little help clearing things up.
 
Last edited:
Since the project is mainly for me to learn more VB.NET the only person who is going to be editing will be me from the above mentioned admin screen within the game.
Wow.. This is quite a complex use of vb.net if youre new to programming

I understand the analogy. I am looking for any kind of solution under which I would be able to group together a list of attributes, for example "Age" and "Name" or "Weight" which could have varying Types but still qualify as Attributes of the character. Since the list of possible attributes could be different per character, I think I need to be able to pull their Key and Value for display dynamically on the form, but I am open to other suggestions on how I might get the same effect.
Hmm... well your character sounds like my report. My report must show all transactions after a certain date, under a certain value, having a certain text.. Your character will have a birthday, some money and a name maybe.. So we both prompt the user for a date, a number and a string..

It's not simple though.. :)


I'm hoping that my problem might be more easily corrected by your help. I'm positive that there is something I'm just not thinking of and need a little help clearing things up.

You really are going to need a Dictionary(Of String, Object) of this character's attributes (unless we get into the messy route of reflection, which we should avoid), TypeOf(xxx) can be used to determine the type of an object, and a TableLayoutPanel can dynamically show the varying attributes
I can give you code of how I do it, but it's in C#. You can read it or try converting it if you like
 
Wow.. This is quite a complex use of vb.net if youre new to programming

heheh... I've done a bunch of reading, not nearly enough doing. I've always had an interest in programing, but I'm only now getting serious about it.


You really are going to need a Dictionary(Of String, Object) of this character's attributes (unless we get into the messy route of reflection, which we should avoid), TypeOf(xxx) can be used to determine the type of an object, and a TableLayoutPanel can dynamically show the varying attributes
I can give you code of how I do it, but it's in C#. You can read it or try converting it if you like

That would be fantastic. I think I'll be able to read through it. I have a little bit of experience in C++. Combine that with the little I know with VB.NET and I should be able to understand your code enough to get started with my project.

THANK YOU.
 
Here is how I build the gui to edit a variety of param types. You might adopt a similar method:

VB.NET:
    private void LoadReportArguments(string procedureName)
    {
      Label argLbl;
      

      argumentsTableLayoutPanel.Hide();
      argumentsInfoLabel.Text = "Loading parameters...";
      
      argumentsTableLayoutPanel.RowStyles.Clear();
      argumentsTableLayoutPanel.Controls.Clear();


      argumentsReadOnlyTA.FillByProcedureName(realDS.Arguments, procedureName);
      argumentsTableLayoutPanel.RowCount = realDS.Arguments.Rows.Count;

      if(argumentsTableLayoutPanel.RowCount == 0) {
        argumentsInfoLabel.Text = "(This report has no parameters you can edit)";
        return;
      }

      currentReportArgs = new GenericArgument[realDS.Arguments.Rows.Count];

      RealDS.ArgumentsRow ro;      
      for(int i = 0; i < realDS.Arguments.Rows.Count; i++) {
        ro = realDS.Arguments[i];
     
        switch (ro.EDITOR_TYPE)
        { 
          case "freetext":
            currentReportArgs[i] = new FreeTextArgument(ro.EDITOR_DEFAULT);
            break;

          case "date":
            if(ro.IsEDITOR_DEFAULTNull())
              currentReportArgs[i] = new DateArgument(DateTime.Now.Date);
            else
              currentReportArgs[i] = new DateArgument((DateTime)(RunScalarSQL(ro.EDITOR_DEFAULT)));
            break;

          case "singlechoice":
            if(ro.IsEDITOR_DEFAULTNull())
              throw new ArgumentException("You cannot have a single choice parameter with a null EDITOR_DEFAULT");
            currentReportArgs[i] = new SingleChoiceArgument(RunColumnarDispValSQL(ro.EDITOR_DEFAULT));
            break;

          case "multiplechoice":
            if(ro.IsEDITOR_DEFAULTNull())
              throw new ArgumentException("You cannot have a multiple choice parameter with a null EDITOR_DEFAULT");
            currentReportArgs[i] = new MultipleChoiceArgument(RunColumnarDispValSQL(ro.EDITOR_DEFAULT));
            break;

          case "numeric":
            if(ro.IsEDITOR_DEFAULTNull())
              currentReportArgs[i] = new NumericArgument(0M);
            else
              currentReportArgs[i] = new NumericArgument(Convert.ToDecimal(ro.EDITOR_DEFAULT));
            break;

          default:
            throw new Exception(
              "This report cannot be used currently. It requests use of an EDITOR_TYPE \"" + 
              ro.EDITOR_TYPE + "\" which is unknown. If the situation persists, notify Support");

        }

        currentReportArgs[i].ArgumentName = ro.ARGUMENT_NAME;
       
        argLbl = new Label();
        if(ro.IsUSER_DESCRIPTIONNull())
          argLbl.Text = "(User Label was null)";
        else
          argLbl.Text = ro.USER_LABEL;
        argLbl.TextAlign = ContentAlignment.MiddleRight;
        argLbl.AutoSize = false;
        argLbl.Width = 150;
        argLbl.Height = currentReportArgs[i].EditorControl.Height + 5;

        argumentsTableLayoutPanel.RowStyles.Add(
          new RowStyle(SizeType.Absolute, (float)(currentReportArgs[i].EditorControl.Height + 5)));
        
        argumentsTableLayoutPanel.Controls.Add(argLbl, 0, i);
        argumentsTableLayoutPanel.Controls.Add(currentReportArgs[i].EditorControl, 1, i);
        
      }
      argumentsTableLayoutPanel.RowStyles.Add(new RowStyle());
      argumentsTableLayoutPanel.Show();
    }

It shouldb e quite readable. If you need it commenting, say
 
Back
Top