• Welcome to Valhalla Legends Archive.
 

My PropertyBag Class

Started by Joe[x86], December 04, 2005, 12:33 PM

Previous topic - Next topic

Joe[x86]

Anyone want to critique this? I thought it'd be interesting to write one of these, so eh. Possible uses, holding information on a user in a channel (variables Ping, Flags, Username, ChannelListIndex, Product, Statstring, etc), holding bot profile information, etc.

/*
* Class:   PropertyBag
* Package: Util
* Author:  Joe[e2]
* Purpose: This houses our variables, and their values.
*/

package util;

public class PropertyBag {

   String names[]  = new String[255];   // The names of our variables
   String values[] = new String[255];   // The values of our variables
   int position = 0;               // The position that we're inserting new variables in
   
   /**
    * @author Joe[e2]
    * @param name: Variable name to return value for.
    * @return: Value if variable exists, otherwise null.
    */
   public String getValue(String name) {
      for(int i = 0; i < 255; i++) {
         if(names == name) {
            return values;
         }
      }
      return null;
   }
   
   public void setValue(String name, String value) {
      for(int i = 0; i < 255; i++) {
         if(names == name) {
            values = value;
            return;
         }
      }
      names[position]  = name;
      values[position] = value;
      position++;
      return;      
   }

}


Demonstration/test class:
/*
* Class:   Main
* Author:  Joe[e2]
* Purpose: Create + test our PropertyBag object.
*/

import util.PropertyBag;

public class Main {
   
   public static void main(String args[]) {
      PropertyBag pbag = new PropertyBag();
      pbag.setValue("Name1", "Value1");
      pbag.setValue("Name2", "Value2");
      pbag.setValue("Name3", "Value3");
      System.out.println(pbag.getValue("Name1"));
      System.out.println(pbag.getValue("Name2"));
      System.out.println(pbag.getValue("Name3"));
   }

}
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.


Kp

Why're you preallocating a fixed maximum length?  Let it grow dynamically.  That'll also avoid the problem that it can silently fail to store a property.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!