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

  • SEARCH
  • Home
  • 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 8578241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:20:32+00:00 2026-06-11T20:20:32+00:00

Can we initialize an Object inside a block or function (without declaring it before)

  • 0

Can we initialize an Object inside a block or function (without declaring it before) so that it can be used outside its block too…?

Here Type2 is a derived class from Type1

for eg:

if(a==b)
{ Type1 obj = new Type1();}
else
{Type2 obj = new Type2();}

obj.getFunction(); 

I have a base class USER derived class ADMIN, have to create an object according to the role of the user while he logs in and use that object afterwards so thath i dont need to check the role of the user every time.. (am a newbie)

IS THIS POSSIBLE??

  • 1 1 Answer
  • 0 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-06-11T20:20:33+00:00Added an answer on June 11, 2026 at 8:20 pm

    The variable can only be of one type or another – otherwise how would the compiler know what member access to allow afterwards?

    If there’s some common base type (base class or interface) which declares function, that should be the type of the variable:

    BaseType obj;
    if (a == b)
    {
        obj = new Type1();
    }
    else
    {
        obj = new Type2();
    }
    obj.Function(); // Name fixed to follow conventions
    

    This would be simpler as:

    BaseType obj = a == b ? (BaseType) new Type1() : new Type2();
    obj.Function();
    

    If they’re really different types, so you’re effectively calling entirely separate methods, then either you’ll need to declare the variable within each block and call the method there, or if you’re using C# 4 or later you could use dynamic typing:

    dynamic obj = a == b ? (dynamic) new Type1() : new Type2();
    obj.Function();
    

    You should really be trying to think about whether this is a genuinely-common function, in which case it should be declared in a base type, or whether it’s just a coincidence that you’ve got two functions with the same name, in which case trying to treat them the same way is a bad idea. (Imagine you refactor one type to change the method name, for example…)

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

Sidebar

Related Questions

i have a function(initialize) inside the a.as File.i want to access the that Variable
How can I find and execute a function inside of an object based on
Can i initialize object in my first activity and you it in all activity???
I would like to know: how can i initialize class object with parameters? I've
Can I use form_for orbinary object? Controller: class AgendaState attr_accessor :base_date def initialize(date) @base_date
Using C# 3.0, we can initialize objects without their constructors for syntactical reasons. Such
One can create an anonymous object that is initialized through constructor parameters, such as
My problem here is that I would like to pass an object to a
In Ruby, can one object destroy another? For example: class Creature def initialize @energy
Is there possible to initialize a object-table of a struct in c before main?

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.