简体   繁体   中英

Trying to understand classes in c#

New to C# and showing the world my lack of understanding (ignorance). I have an SQL database with various tables. In this case Category. I have different queries that return different data for each table depending on the application. For example I have a form to set up categories and then other forms use the categories in various ways. To set up a category I have a class, Category.

using System;

namespace Toolcrib
{
    public partial class Globals
    {
        public class Category
        {
            private int categoryID;
            private string categoryDesc;
            private string shortCode;
            private bool isActive;
            private bool canDelete;
            private int idx;

            public Category()
            {
            }
            public Category (string CategoryDesc, string ShortCode, bool IsActive, bool CanDelete, int Idx)
            {
                categoryID = 0;
                categoryDesc = CategoryDesc;
                shortCode = ShortCode;                    
                isActive = IsActive;
                canDelete = CanDelete;
                idx = Idx;
            }

            public Category(int CategoryID, string CategoryDesc, string ShortCode, bool IsActive, bool CanDelete, int Idx)
            {
                categoryID = Convert.ToInt32(CategoryID);
                categoryDesc = CategoryDesc;
                shortCode = ShortCode;
                isActive = IsActive;
                canDelete = CanDelete;
                idx=Idx;
            }

            public int CategoryID
            {
                get { return categoryID; }
                set { categoryID = value; }
            }

            public string CategoryDesc
            {
                get { return categoryDesc; }
                set { categoryDesc = value; }
            }

            public string ShortCode
            {
                get { return shortCode; }
                set { shortCode = value; }
            }

            public bool IsActive
            {
                get { return isActive; }
                set { isActive = value; }
            }

            public bool CanDelete
            {
                get { return canDelete; }
                set { canDelete = value; }
            }

            public int Idx
            {
                get { return idx; }
                set { idx = value; }
            }
        }
    }
}

When using Category on other forms I don't need isActive, canDelete, or idx. I may have misunderstood but I thought I could create a base class with only the categoryID, categoryDesc, and shortCode then when I implement it on my SetUpCategory form add to the class. I have been looking but have not found a good example of how to make this work. I can define two classes on for setup and one for implementation but, again, my thinking is that is not necessary. Anyone who can point me in the right direction or provide a snippet of code will have my eternal gratitude.

You must create the constructor for your need

public Category(int CategoryID, string CategoryDesc, string ShortCode)
{
    categoryID = Convert.ToInt32(CategoryID);
    categoryDesc = CategoryDesc;
    shortCode = ShortCode;
}

You have at least two separate use cases for your object:

  1. One with isActive , canDelete , and idx (and possibly all other Category data).
  2. One without isActive , canDelete , and idx .

To solve your problem, define two interfaces:

public interface ICategory
{
    int CategoryID { get; set; }
    string CategoryDesc { get; set; }
    string ShortCode { get; set; }
}

public interface ICategoryInfo : ICategory
{
    bool isActive { get; set; }
    bool canDelete { get; set; }
    int idx { get; set; }
}

Then, have your Category class implement those interfaces:

public class Category : ICategory, ICategoryInfo 

Finally, wherever you need to use the Category class, instead of passing Category , pass either ICategory or ICategoryInfo as appropriate:

public void SaveCategory(ICategory category)
{
    // Do something awesome here.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM