Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 667549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:56:27+00:00 2026-05-13T23:56:27+00:00

I have a Rect object that I’d like to create and set its properties

  • 0

I have a Rect object that I’d like to create and set its properties only once. After that, I want to just modify its properties since it already exists. This is my general idea

if(theRect == undefined){

  Alert.show("creating");

  var theRect:Rect = new Rect();
  //then set properties
  addElement(theRect); //then add it using addElement because addChild() does not work

} else {

  Alert.show("updating");

  //no need to create it since it's already been created
  //just access and change the properties

}

I tried several ways and combinations for the if conditional check:

if(theRect == undefined){
if(theRect == null){
declaring and not declaring `var theRect:Rect;` before the if check
declaring and instantiating to null before the if check `var theRect:Rect = null;` 

but can’t get the desired effect. Everytime this code block runs, and depending on which version I’ve used, it either gives me a “can’t access null object” error or the if statement always evaluates to true and creates a new Rect object and I get the “creating” Alert.

What’s the right way of creating that Rect but only if doesn’t exist?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:56:27+00:00Added an answer on May 13, 2026 at 11:56 pm

    You have some scoping issues in the code you presented.

    What I think you want to do is:

    var theRect:Rect;
    
    ...
    
    if(theRect == null)
    {
        theRect = new Rect();
        ...
    }
    ...
    

    You need to first declare theRect, but you don’t have to create it. You can instantiate it lazily later by checking if it is null.

    The way you have it set up, you created a local version of theRect inside your if statement that would not be visible elsewhere. You will also get an error trying to access theRect if you did not declare it beforehand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My program is crashing because my TextField is sending messages to its delegate after
I have a Winforms application that the user uses to take a region based
I have following components - ColorButton, that represents single button that is basically colored
I have created code in WPF to let the window remember its last location
What would cause my program to pause on Xcode? I have no breakpoints set
I have difficulties understanding a problem I encountered during instantiation of a QT class
I have the following code, which loops through an array of menuoptions and on
How to draw a circle on a form that covers the whole working area?
I have a Visual Studio solution which consists of a DLL and an EXE.
I'm creating an application (Windows Form) that allows the user to take a screenshot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.