473,395 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Design Question

Okay so I've got a delegate called "InstructionRun" (just a simple void with no parameters), and a class called "Instruction" which contains a delegate InstructionRun object as a member variable, as well as a method called "Run" which just calls the InstructionRun object's current functionality. There is also a byte in this class called "code".

Another class ("Instructions") has a static array of Instruction objects. It also has a static method whose purpose is to initialize all the Instruction objects.

And then somewhere else are a bunch of static void() methods that will be stored into the "InstructionRun" object for each Instruction object in the array.

My predicament is that I would like something like a simple text file or database to provide me with the data for this Instruction array. I want it to have a bunch of pairs, my "code" byte and then my static void() method. However, I would not want to resort to any of the following:

1) Not using a data file and just putting hundreds of "new Instruction()" lines to fill the array. I hate mass blocks like that when the design could change and then I have to change hundreds of those lines. I want something like a simple text file to just match up "code"s with functionality.
2) Putting the method's actual name in the data file, and then using reflection to read it during runtime and match it up with a method. Just seems sluggish and icky.

Please help out! If I wasn't clear on something, I can explain more.
Jun 8 '09 #1
8 1606
tlhintoq
3,525 Expert 2GB
I'm guessing the end goal is to change the order of start-up events by changing the config.txt file. But ...
[not] Putting the method's actual name in the data file
yet
Not using a data file and just putting hundreds of "new Instruction()" lines to fill the array. I hate mass blocks like that when the design could change and then I have to change hundreds of those lines.
Implies that you do have hundreds of methods, that all match the same delegate.

So, what... You're trying to create a text like


27, add
19, remove
89, load

Something like that?

That will eventually become a sequence that effectively does

Add(LoadedByte); // LoadedByte being 27
Remove(LoadedByte); // LoadedByte being 19
Load(LoadedByte); // 89 LoadedByte being 89

Do I interpret the goal correctly?
Jun 8 '09 #2
You are pretty much correct. I will have hundreds of methods, which is why I chose a delegate system as I don't want this:

Expand|Select|Wrap|Line Numbers
  1. switch (code)
  2. {
  3.    case 0x28:
  4.     RunADD();
  5.     break;
  6.    case 0x2A:
  7.     RunJMP();
  8.     break;
  9.    //so on...
  10. }
However, they are parameterless methods...they don't take a byte (actually I haven't decided that yet, they MIGHT have parameters down the line, but that's not important right now, right now I just need some way to match up the bytes with methods), but rather the byte determines which method to call.

So yes, my text would be something like:

28 RunADD
2A RunJMP
...
etc.

and then there would just be:

static void RunADD()
static void RunJMP()
...
etc.
Jun 9 '09 #3
tlhintoq
3,525 Expert 2GB
Got it. Looks like you are making an assembly language simulator/translator.

Why not just make the methods match the bytes then? If you have a byte of 2A then run static void 2A(), which is the JMP instruction? Why do the lookup table at all?
Jun 9 '09 #4
That would be fine, I don't care what the methods are named....but even then, how would I go about matching up that byte coming in to that function named "2A"? Do I have to resort to reflection?
Jun 9 '09 #5
tlhintoq
3,525 Expert 2GB
Do I have to resort to reflection?
You say that like it's a bad thing. What are you trying to avoid about reflection, or what is it that makes your project hard to deal with reflection?
Jun 9 '09 #6
Nothing in particular, wouldn't it just slow it down? It seems a little excessive...getting a method name from a file, passing the string to look up a symbol name of the very program you're running in currently...when all you really need to do (though it might be uglier) is just call the damn method in the program. I was merely trying to look for a non-ugly way, maybe, avoiding reflection.

Thanks for being patient with me.
Jun 9 '09 #7
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Reflection;
  3.  
  4. class CallMethodByName
  5. {
  6.    string name;
  7.  
  8.    CallMethodByName (string name)
  9.    {
  10.       this.name = name;
  11.    }
  12.  
  13.    public void DisplayName()      // method to call by name
  14.    {
  15.       Console.WriteLine (name);   // prove we called it
  16.    }
  17.  
  18.    static void Main()
  19.    {
  20.       // Instantiate this class
  21.       CallMethodByName cmbn = new CallMethodByName ("CSO");
  22.  
  23.       // Get the desired method by name: DisplayName
  24.       MethodInfo methodInfo = 
  25.          typeof (CallMethodByName).GetMethod ("DisplayName");
  26.  
  27.       // Use the instance to call the method without arguments
  28.       methodInfo.Invoke (cmbn, null);
  29.    }
  30. }
Jun 9 '09 #8
Okay, thanks for your help.

I just have seen that most people, including even in this community, frown upon retrieving a variable via a string (its name). I figured the same would apply here. But I guess in some cases it is acceptable for such behavior, especially when it would simplify the code so greatly.
Jun 9 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Don Vaillancourt | last post by:
Hello all, Over the years as I design more database schemas the more I come up with patterns in database design. The more patterns I recognize the more I want to try to design some kind of...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
2
by: Test User | last post by:
Hi all, (please excuse the crosspost as I'm trying to reach as many people as possible) I am somewhat familiar with Access 2000, but my latest project has me stumped. So, I defer to you...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
17
by: roN | last post by:
Hi, I'm creating a Website with divs and i do have some troubles, to make it looking the same way in Firefox and IE (tested with IE7). I checked it with the e3c validator and it says: " This...
6
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
19
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
8
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.