CodingC#- classes inside classes... or something

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  Ally  
 To:  ALL
35454.1 
So, in my "never learn it at university/school" way, I've come up against a C# problem that is probably incredibly simple.

Basically, I've started doing things properly with some objects I'm making, thus:

csharp code:
 
public class AffiliateConstants
    {
        private string m_AffiliateName;
        public string AffiliateName
        {
            get { return m_AffiliateName; }
        }
    }
 


So far so good. But I want to make sort of sub-object, accessible like AffiliateConstant.SubObject that'd have another load of properly created properties that are settable internally by the child and parent but not outside of them.

If I make a separate class then I can't change the subobject's values (unless I do so on the constructor) because, of course, they're private.

0/0
 Reply   Quote More 

 From:  Ally  
 To:  THERE IS NO GOD BUT (RENDLE)     
35454.2 In reply to 35454.1 
(hello)
0/0
 Reply   Quote More 

 From:  dave (10_ROGUE)  
 To:  Ally     
35454.3 In reply to 35454.2 

In my was taught it at Uni but didn't really pay attention and havn't used it since way:

 

Are you looking at subclasses to pull some properties from the parent class then set the additional properties based on the subclass?

 

Although I have no idea how to code that.

0/0
 Reply   Quote More 

 From:  THERE IS NO GOD BUT (RENDLE)  
 To:  Ally     
35454.4 In reply to 35454.1 
A nested class can access private members of the containing class, but not vice-versa. If you defined the classes in their own assembly, you could mark the members as internal, which would protect them from outside access.

What are the semantic reasons for the nested class?

0/0
 Reply   Quote More 

 From:  Ally  
 To:  THERE IS NO GOD BUT (RENDLE)     
35454.5 In reply to 35454.4 

Well basically it's just a class that loads some settings from an XML file. Included in these settings are filters and output settings. So I thought it'd be useful to have nested classes for Settings.Filters and Settings.Output- just for ease of use, really.

 

Going for it's own assembly seems a little excessive... I might just go down the route of making separate classes for Filters and Output and then having them as public objects inside Settings- and define their values in the constructors.

 

Cheers!

0/0
 Reply   Quote More 

 From:  THERE IS NO GOD BUT (RENDLE)  
 To:  Ally     
35454.6 In reply to 35454.5 
By "ease of use" I guess you mean within Intellisense? There's a trick you can use for organising properties within a class so they appear to be in nested classes. You define nested interfaces for each organisational class, and explicity implement those interfaces on the containing class. Then add properties which return the instance cast to the interface.

Like this:

c# code:
    public class AffiliateConstants : AffiliateConstants.IFilters
    {
        public IFilters Filters
        {
            get { return (IFilters)this; }
        }
 
        public interface IFilters
        {
            string FileFilter { get; }
        }
 
        #region IFilters Members
 
        private string m_FileFilter;
 
        string IFilters.FileFilter
        {
            get { return m_FileFilter; }
        }
 
        #endregion
    }
 


Then you can only access the FileFilter property through the Filters property:

c# code:
AffiliateConstants ac = new AffiliateConstants();
Console.WriteLine(ac.Filters.FileFilter);
 

0/0
 Reply   Quote More 

 From:  Ally  
 To:  THERE IS NO GOD BUT (RENDLE)     
35454.7 In reply to 35454.6 
Ah, that would appear to be just what I need. Unfortunately, when I try that out I get:

quote:
Circular base class dependency involving 'Erm.TaskDevelop.AffiliateConstants' and 'Erm.TaskDevelop.AffiliateConstants.IFilters'


Am I being daft?
0/0
 Reply   Quote More 

 From:  Ally  
 To:  ALL
35454.8 
Here's another one (hooray). I'm declaring a private variable like so:

csharp code:
private string m_AffiliateName;


Then trying to write a sort-of "cover all" function to convert from an XML file (thus, a string) into the relevant data type:

csharp code:
if (target.GetType() == typeof(System.DateTime))
{
      target = DateTime.Parse(nav.Value);
} else {
      target = nav.Value;
}


Problem is, when I run it, it tells that it can't GetType on target, because it's null. Which it is- this is the first time I'm trying to assign to it. But is there some way to get the underlying type of an object, even if it's null?
0/0
 Reply   Quote More 

 From:  THERE IS NO GOD BUT (RENDLE)  
 To:  Ally     
35454.9 In reply to 35454.7 
Have you declare IFilters as a class? It should be an interface.

0/0
 Reply   Quote More 

 From:  Ally  
 To:  THERE IS NO GOD BUT (RENDLE)     
35454.10 In reply to 35454.9 
Definitely an interface. I mean, I copy and pasted it. Is there any reason I couldn't do this to get my result:

csharp code:
public class AffiliateFilters
        {
            AffiliateConstants _parent;
            public AffiliateFilters(AffiliateConstants parent)
            {
                _parent = parent;
            }
            public DateTime StartDate { get { return _parent.m_FilterStartDate; } }
            public DateTime EndDate { get { return _parent.m_FilterEndDate; } }
            public String MetroCode { get { return _parent.m_FilterMetroCode; } }
            public String ZipCode { get { return _parent.m_FilterZipCode; } }
        }
 
0/0
 Reply   Quote More 

 From:  THERE IS NO GOD BUT (RENDLE)  
 To:  Ally     
35454.11 In reply to 35454.10 

No, that'd be fine too.

 

Are you using some ancient version of C#? Maybe the compiler wasn't as clever before 3.0 or something.

0/0
 Reply   Quote More 

 From:  Ally  
 To:  THERE IS NO GOD BUT (RENDLE)     
35454.12 In reply to 35454.11 

I'm on Visual Studio 2008 but it's targetting 2.0, so that's possibly the case.

 

At least I'm not using 1.1 any more. Slow progress.

0/0
 Reply   Quote More 

Reply to All    
 

1–12

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats