Working with List in Structure c#

Posted on 2009-05-10
Last Modified: 2012-05-08

I have 2 structures, (example) 1 called Woords and the other one called Sentence.
Sentence contains nothing more then a List<> of Woords.

Now when I try to add Woords to the List on Sentence it says "NullReferenceException". Now I can understand that I need to create a New List before adding any items to it, but can't figure out how to do it with the structs.
See sample code below:


// my structs
public struct structWoords
           public string woord1;
           public string woord2;
       public struct structSentence
         public List<structWoords> woords ; 
Structs.structWoords myWords;
            myTest.woord1 = textBox1.Text;
            myTest.woord2 = textBox2.Text;
            Structs.structSentence mySentence = new Structs.structSentence();
            mySentence.woords.Add(myWords); // error happens here
//  I think the problem is that I don't make a new List but don't know how to do it with the struct...

Open in new window

Question by:Pit76
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 24349811
You can't initialize variables in a struct, as far as I know.  You'll have to do it outside the struct:

mySentence.woords = new List<string>();

Open in new window


Accepted Solution

zwei earned 500 total points
ID: 24349819
oops I meant
new List<structWoords>();

Open in new window


Author Comment

ID: 24350044
where do i need to put that line of code then?

Author Closing Comment

ID: 31656982
Thx, I found where to put that line of code.. :)


Expert Comment

ID: 24350106

public struct structSentence
    public List<structWoords> woords; //cannot have instance field initializers in structs
public class structSentence2
    public List<structWoords> woords = new List<structWoords>();
structSentence mystruct = new structSentence();
mystruct.woords = new List<structWoords>();
structSentence2 mystruct2 = new structSentence2();

Open in new window


Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question