Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question
4

Optional IDF and Workspace Object

asked 2016-02-08 11:05:58 -0600

ngkhanh's avatar

updated 2017-02-02 01:54:19 -0600

I put this code as measure writing guide :

new_sch = "
    Schedule:Compact,
    Return rate 95, !- Name
    Fractional,        !- Schedule Type Limits Name
    Through: 12/31,          !- Field 1
    For: AllDays,            !- Field 2
    Until: 24:00,            !- Field 3
    0.95;                    !- Field 4
    "
    new_sch_idfobj = OpenStudio::IdfObject::load(new_sch)
    puts "compact schedule #{new_sch_idfobj}"
    new_sch_idfobj1 = new_sch_idfobj.get
    sch_returnrate = workspace.addObject(new_sch_idfobj1)
    puts "compact schedule1 #{sch_returnrate}"

it returns in screen :

compact schedule #<OpenStudio::OptionalIdfObject:0x000000062ceda8>
compact schedule1 #<OpenStudio::OptionalWorkspaceObject:0x000000062ceb50>

What difference between Optional and Non-Optional Object? Have any difference with standard objects when we need to use them?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2016-02-08 14:45:51 -0600

An Optional is basically something that gets returned when you can't know for sure if something will work on not. It allows you to write code that won't crash, you just have to make sure you test the objects before you do get them

new_sch_idfobj = OpenStudio::IdfObject::load(new_sch)
if new_sch_idfobj.empty?
    # Decide what to do if it didn't work
   puts "Couldn't load the schedule. Double check your input string"
else
    # If it did work, get it
    new_sch_idfobj = new_sch_idfobj.get
    # Do something with it...
end

You could read up more on the boost documentation if you are interested in knowing exactly why it's a good thing and what advantages it offers.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Training Workshops

Careers

Question Tools

1 follower

Stats

Asked: 2016-02-08 11:05:58 -0600

Seen: 419 times

Last updated: Feb 02 '17