OCCRA
Go to Post I mean, there is a 40 second period where your alliance has to play defense, so you might as well play it well. - Jeff Rodriguez [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Events   CD-Media   CD-Spy   FRC-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 06-05-2012, 03:50 PM
joelg236 joelg236 is offline
Registered User
AKA: Joel Gallant
no team
Team Role: Alumni
 
Join Date: Dec 2011
Rookie Year: 2012
Location: Calgary
Posts: 678
joelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond repute
Preferences check if the element exists

Hi CD,

I am working on some code to interface with the preferences class that is accessible in SmartDashboard. Unfortunately, I don't know how to figure this part out.

Code:
/**
     * Gets the integer from preferences.
     * 
     * @param ID ID of the integer
     * @param backupValue The value to return in preferences does not contain it
     * @param replace If the backup value has to be used, should the value be inserted?
     * @return Returns the integer saved in preferences
     */
    public static int getInt(String ID, int backupValue, boolean replace) {
        int backup = backupValue;
        try {
            if(m_data.elementAt(m_names.indexOf(ID)) != null) {
                backup = ((Integer)m_data.elementAt(m_names.indexOf(ID))).intValue();
// Checks to see if my code has the element stored^^^ 
            }
        }
        catch(ClassCastException ex) {} 
        catch(ArrayIndexOutOfBoundsException ex) {}
//These exceptions are okay to catch because my code might not store it already
        if (Preferences.getInstance().getInt(ID, backup) == backup && replace) { 
// This is where I have a problem. I can check if its the same as backup - 
//which will happen when it goes to the backup value yes, which does its 
//purpose. But for optimization, everytime the field is actually the backup 
//(No matter if it exists or not), it will save the value. That takes up 
//bandwidth and slows the code down. NOT what I want to do, especially 
//because a lot of the values will be the same as backup.
            Preferences.getInstance().putInt(ID, backup);
            save();
        }
        return Preferences.getInstance().getInt(ID, backup);
    }
I wrote the problem in the code in comments, just in case you didn't notice it.
__________________
Team 4334 2012-2014
Reply With Quote
  #2   Spotlight this post!  
Unread 07-05-2012, 09:18 PM
joelg236 joelg236 is offline
Registered User
AKA: Joel Gallant
no team
Team Role: Alumni
 
Join Date: Dec 2011
Rookie Year: 2012
Location: Calgary
Posts: 678
joelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond repute
Re: Preferences check if the element exists

I found the solution to this problem, so I'll post the answer for reference.

Code:
    public static String getSetting(String name) throws NoSettingFound {
        if(Preferences.getInstance().containsKey(name)) {
            return Preferences.getInstance().getString(name, null);
        }else {
            throw new Errs.NoSettingFound(name);
        }
    }
Basically, there is a method meant just for this problem.
__________________
Team 4334 2012-2014
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 02:02 AM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi