Thread: structs in C#

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    37

    structs in C#

    Hello.

    I am now starting to program in C# with no formal learning on this language :P by reading a program that I have.
    I would appreciate some help.

    I have two structs
    Code:
    private struct firstone
    {
       public string name;
       public int[] num;
       public int[,] x;
    .....
    }
    
    private struct secondone
    {
       public string name;
       public double[] num;
    .....
    }
    
    firstone[] firstvar;
    firstone  another;
    
    secondone[] model;
     secondone other;


    Then inside the form1 load function I have
    Code:
    firstvar= new firstone[5];
    model= new secondone[5]; 
    another=new firstone();


    Now look at the last line. The variable another is created ?? with a new with something that must be a constructor to the struct
    I guess

    however later on, all four variables are used eventhough other was never created with a new!!

    I mean, I though new was not necessary in the first place for the variables another and other , so is new needed or not?

    and why only in one case and not the other?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should post the smallest and simplest compilable program that demonstrates what you are talking about.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    new creates an instance of the object, which has access to data members
    of the class/struct.

    In your example, secondone is already has an instance, which is model.
    When you create other, it becomes a second instance to secondone,
    therefore having access to all data members that model has access to.

    In C++ or example, If I have a class Player, and an object of ply:

    Code:
    class Player
    {
    // blash
    };
    
    // main
    int main()
    {
        Player ply;
    }
    ply has access to all the data members (if allowed) of the Player class.
    Then, if I add another object of the same class...

    Code:
    Player ply;
    Player ply2;
    ply2 has the same access (if allowed) as ply. There are small
    differences however, access specifiers place restrictions on member
    access fro example.

    That is the best I can read into it, unless there is something in the code not
    present.
    Double Helix STL

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Come on, man. This is really easy to google.

    Using Structs (C# Programming Guide)

    When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. In such a case, there is no constructor call, which makes the allocation more efficient. However, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    37
    Quote Originally Posted by itsme86 View Post
    Come on, man. This is really easy to google.

    Using Structs (C# Programming Guide)

    When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. In such a case, there is no constructor call, which makes the allocation more efficient. However, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.
    already googled, that is why I ask.
    Let's see what the quote above says:

    When you create a struct object using the new operator, it gets created and the appropriate constructor is called.
    Alright... In my case there are two variables another and other . And only another gets created with a new operator. Why?

    Moreover, the quote implies that there is an appropriate constructor, but in the code I have, there is no constructor for the firstone struct. So, why use new? and why use new only on one struct variable and not on the other?

    Unlike classes, structs can be instantiated without using the new operator.
    Yes, I can see that, because other never gets instantiated with a new, and yet it is used.

    ----------
    so maybe I should rephrase my question

    Can I use single instances of a struct (like another and other) without using new??

    (for arrays I suppose I should use new)

    (I am starting to think that the guy who wrote the code I have just made a mistake...)

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Have you tried compiling/running the code? Does it work?
    If it does, then step through the code with the debugger and take notice of
    how variables and objects change. That may give you some clue. Default
    constructors are implemented when the program has no psychical constructor
    present. So it may auto-initialize the object from the struct on the data heap
    then free it using the garbage collector.
    Double Helix STL

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Can I use single instances of a struct (like another and other) without using new??
    It tells you right in that snippet that I quoted:
    Unlike classes, structs can be instantiated without using the new operator. In such a case, there is no constructor call, which makes the allocation more efficient. However, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.
    (for arrays I suppose I should use new)
    Not sure what you mean. Instantiating the structs in an array? It doesn't matter if they're in an array or not. The snippet from MSDN still applies.

    (I am starting to think that the guy who wrote the code I have just made a mistake...)
    Not a coherent sentence.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    In C#, int is a struct. So is double. You don't need to use new on those, do you? A struct is a value type. Value types do not need to, but can be created with new.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-08-2013, 07:55 AM
  2. Typedef Structs inside Typdef structs
    By gremory in forum C Programming
    Replies: 21
    Last Post: 12-30-2011, 07:48 PM
  3. [ noob question ] Help with structs within structs
    By Riverfoot in forum C Programming
    Replies: 3
    Last Post: 04-26-2011, 07:24 PM
  4. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  5. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM

Tags for this Thread