Struct in side a class

Last post 07-17-2009 5:47 PM by JeffreyABecker. 3 replies.

Sort Posts:

  • Struct in side a class

    07-17-2009, 2:17 AM
    • Member
      22 point Member
    • sridharakrishna
    • Member since 10-20-2008, 9:56 AM
    • HYDERABAD
    • Posts 28

    DbHelper is class which have structure named 'parameters' inside of it,thats ok.

    DbHelper.cs

    public struct Parameters;

    *************************Then pls explain about this block of code**********************

    DBHelper oDBHelper = new DBHelper();

    DBHelper.Parameters[] colParameters = null;

    colParameters = new DBHelper.Parameters[] ;

    {

           new  DBHelper.Parameters(parametrename, parametrevalue)),

          new  DBHelper.Parameters(parametrename1, parametrevalue2)),

        like this  we can add  any no .of parameters

    }

    *******************************************

    my understanding is  'colParameter' is an array of type structure 'parameters',is it correct??

    please expalin in detail about above code block.

    what key word  'new' will do.

  • Re: Struct in side a class

    07-17-2009, 3:23 AM
    Answer
    • Member
      406 point Member
    • Ashish.K
    • Member since 07-09-2009, 7:16 AM
    • Hyderabad
    • Posts 63

     Hi,

    You are right  if you say

    DBHelper.Parameters[] colParameters = null;

    colParameters = new DBHelper.Parameters[] ;

    That means  its collection of new DBHelper.Parameters[] ; .

    When you say

     

    colParameters =

     

    {

           new  DBHelper.Parameters(parametrename, parametrevalue)),

          new  DBHelper.Parameters(parametrename1, parametrevalue2)),

        like this  we can add  any no .of parameters

    }

    new DBHelper.Parameters[] ;

    It will create new object of type DBHelper.Parameters and store it in this collection.

    to create new instance of an object we use new key work.

    Hope this helps.

    Thanks,
    Ashish Khadloya.

    Please Mark right answer if this post helps.
  • Re: Struct in side a class

    07-17-2009, 3:27 AM
    • Member
      152 point Member
    • jalpesh.patel
    • Member since 07-12-2009, 5:27 PM
    • India
    • Posts 34

    Hi sridharakrishna,

    The reason for this may be:

    Because structures are simply an inline area of memory, they cannot be null, and so the CLR has to be able to ensure that the area of memory is totally initialized rather than being partly garbage. For this reason, you'll often hear the 'constructors' on structures called (arguably more correctly) 'initializers' because they don't construct an object they just initialize an area of memory.

    You can't define a default initializer because the CLR has an intrinsic understanding of structures and automatically zeroes out the required amount of memory for a default structure.

    When you define a non-default initializer, C# requires you to set all fields because it skips the zeroing of memory and lets you initialize it.

    --------------------------------------------------------------

    Another way: Structures can be used without any constructors, but if all the public properties exposed by the structure have get methods only and no set methods, then you will need some mechanism to set its value, and constructors might be it.


    Remember to click “Mark as Answer” on the post, if it helps you. Because It helps others to find the solution.

    Regards,
    Jalpesh B. Patel
  • Re: Struct in side a class

    07-17-2009, 5:47 PM
    • Star
      14,226 point Star
    • JeffreyABecker
    • Member since 10-04-2004, 4:27 AM
    • Philadelphia, PA
    • Posts 2,909

    Is struct really the right thing for this case?  Are you really expecting copy-by-value behavior for your Parameter objects?  From the code posted I would expect copy-by-reference behavior which indicates that the struct declaration could lead to confusion in the future.

Page 1 of 1 (4 items)