Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Working with List in Structure c#

Posted on 2009-05-10
5
Medium Priority
?
2,054 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
[X]
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
5 Comments
 
LVL 7

Expert Comment

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

0
 
LVL 7

Accepted Solution

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

Open in new window

0
 

Author Comment

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

Author Closing Comment

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

0
 
LVL 6

Expert Comment

by:Cebik
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>();
}
 
//Code
structSentence mystruct = new structSentence();
mystruct.woords = new List<structWoords>();
(..)
 
//or
structSentence2 mystruct2 = new structSentence2();

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

610 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