Results 1 to 4 of 4

Thread: [RESOLVED] [2.0] How to do

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [RESOLVED] [2.0] How to do

    Hi all
    See the class I know that my constructor are wrong but how to use the same constructor for setting different value.

    I do not want to use the class property!



    C# Code:
    1. class Test
    2.     {
    3.         private string _CatID;
    4.         private string _CatName;
    5.         private string _SubCatName;
    6.         private string _SubCatMotherName;
    7.         public Test(string CatID, string CatName)
    8.         {
    9.             _CatID = CatID;
    10.             _CatName = CatName;
    11.                      
    12.         }
    13.  
    14.         public Test(string CatID, string SubCatName)
    15.         {
    16.             _CatID = CatID;
    17.             _SubCatName = SubCatName;
    18.         }
    19.  
    20.         public Test(string CatID, string SubCatMotherName)
    21.         {
    22.             _CatID = CatID;
    23.             _SubCatMotherName = SubCatMotherName;
    24.         }
    25.  
    26.     }

    How to set value through constructor without using the property I have some more variable , above was just little example

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: [2.0] How to do

    You can have as many constructors as you like but they all have to have different signatures, which means the types of the arguments must be different. You simply cannot do what you are showing there. You can, of course, declare a constructor with four arguments and allow nulls to be passed to the string arguments.

  3. #3

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2.0] How to do

    Thanks Sir

  4. #4
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: [RESOLVED] [2.0] How to do

    Maybe use in combination with an enumeration.
    Like ...
    Code:
            public Test(String catID, String name, CatNameKind nameKind)
            {
                 _catID  = catID;
                 switch (nameKind){
                    case CatNameKind.Normal:
                        _catName = name;
                        break;
                    case CatNameKind.Sub:
                        _subCatName = name;
                        break;
                    case CatNameKind.Mother:
                        _motherCatName = name;
                        break;
                 }
            }
    In combination with an enumeration like this ...
    Code:
    public enum CatNameKind{
        Normal,
        Sub,
        Mother
    }
    Creation gets easy like:
    Code:
    Test t = new Test("007XV", "KlaartjeTheCat", CatNameKind.Normal);
    I hope it's a useful idea
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

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