CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2007
    Posts
    54

    Possible to do this in C#???

    I have a class.....with different public properties ...

    Class A {
    public int x; // Non - compilable code..
    public float y;
    public decimal z;
    }

    Now can I expose only some of the public properties to an object based on some conditions???

    Like:-


    [code]
    A aObj;

    if(someCondition = true)
    aObj. (Can I just expose 'x' property and NOT 'y' and 'z' ... ?? ... In Intellisense??)

    esle
    aOBj. (Can I just expose 'y' property and NOT 'x' and 'z' ... ?? ... In Intellisense??)
    [\code]


    Is this even possible??
    Thanks!

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Possible to do this in C#???

    If you were following ANY of the recognized texts, you would not even ask such a question....

    Go get a good book, start at the beginning, read everything, type-in everything, step through everything with your debugger.

    If you have questions, please come back and ask...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Possible to do this in C#???

    And the short answer would be "No". If they are public, they're public.

  4. #4
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Possible to do this in C#???

    no you can't conditionally expose them, but you can make them invisible to intellisense (though users will still be able to access the public members, it won't show up in intellisense. It will show up in the object browser, reflector, and other class documentation).

    you can also use internal which allows classes of the same assembly (you can also make internals visible to other external assemblies thorough an assembly level attribute), but anyone using a reference to your assembly will not.


    you could conditionally expose members through use of multiple interfaces, by exposing the instance as an instance of a particular interface:

    Code:
    public interface IA {
        string A { get; set; }
    }
    
    public interface IB {
        string B { get; }
    }
    
    public interface IC {
        string C { get; set; }
    }
    
    public class Foo : IA, IB, IC {
    
        string a, b, c;
    
        public string A { get { return a; } set { a = value; } }
    
        public string B { get { return b; } }
    
        public string C { get { return c; } set { c = value; } }
    
    }
    
    // ...
    
    if(someCondition)  ((IA)instanceOfFoo).A = "asdf"; // B and C are not included
    else if(someOtherCondition) string fooB = ((IB)instanceOfFoo).B; // A and C are not included
    else if(yetAnotherCondition) ((IC)instanceOfFoo).C = "qwerty"; // A and B are not included
    Last edited by MadHatter; January 19th, 2009 at 09:11 PM.

  5. #5
    Join Date
    Mar 2007
    Posts
    274

    Re: Possible to do this in C#???

    And the wizard said unto thee:

    Quote Originally Posted by TheCPUWizard View Post
    If you were following ANY of the recognized texts, you would not even ask such a question....
    I believe this is a direct quote from the movie Harry Potter.


    To expand just a little on the topic of this thread. Why do you want to conditionally expose properties anyway? Is this for like some kind of plugin architecture?

  6. #6
    Join Date
    Jun 2007
    Posts
    54

    Re: Possible to do this in C#???

    Wow....Thanks MadHatter for the input...Yes...It does help me a lot .....at least this is a very good idea for my project......


    Thanks again.....for being open-minded .... ...and not thinking that this was a really pointless Q...
    Last edited by harrypotter28685; January 20th, 2009 at 11:40 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured