Solved

Working with List in Structure c#

Posted on 2009-05-10
5
2,042 Views
Last Modified: 2012-05-08
Hello,

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:

thx


// my structs

public struct structWoords

       {

           public string woord1;

           public string woord2;

       }
 

       public struct structSentence

       {

         public List<structWoords> woords ; 

       }
 

//code

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

0
Comment
Question by:Pit76
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:zwei
Comment Utility
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

0
 
LVL 7

Accepted Solution

by:
zwei earned 500 total points
Comment Utility
oops I meant
new List<structWoords>();

Open in new window

0
 

Author Comment

by:Pit76
Comment Utility
where do i need to put that line of code then?
0
 

Author Closing Comment

by:Pit76
Comment Utility
Thx, I found where to put that line of code.. :)

0
 
LVL 6

Expert Comment

by:Cebik
Comment Utility

public struct structSentence

{

    public List<structWoords> woords; //cannot have instance field initializers in structs

}
 

public class structSentence2

{

    public List<structWoords> woords = new List<structWoords>();

}
 

//Code

structSentence mystruct = new structSentence();

mystruct.woords = new List<structWoords>();

(..)
 

//or

structSentence2 mystruct2 = new structSentence2();

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now