Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 29th, 2007, 02:43 PM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Generic List error

hello,

I am having trouble using a List object inside a structure:

struct SomeStruct
{
...
public List<Socket> socket;
};

// In a method
SomeStruct somestruct = new SomeStruct();
somestruct.socket = new List<Socket>(); // do I need to new the list?.. still doesn't work

// throws exception "Unhandled Exception: System.NullReferenceException: Object reference not set to
// an instance of an object."
somestruct.socket.Add(asocket);

If I create the list outside of the structure it works fine.

thanks a lot for any help!
Reply With Quote
  #2 (permalink)  
Old October 29th, 2007, 04:35 PM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

*fixed*

declaring as a class and then initialising the List inside it worked:

class someclass
{
...
public List<Socket> socket = new List<Socket>();
};

Reply With Quote
  #3 (permalink)  
Old October 30th, 2007, 03:28 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The following code works for me in a console application:
Code:
using System;
using System.Collections.Generic;
using System.Net.Sockets;

namespace Structs
{
  class Program
  {
    static void Main(string[] args)
    {
      TestStruct1 ts1; //No 'new TestStruct1()' needed.
      ts1.Sockets = new List<Socket>();
      Socket socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IPv4);
      ts1.Sockets.Add(socket1);
      Socket socket2 = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IPv6);
      ts1.Sockets.Add(socket2);
      foreach (Socket socket in ts1.Sockets)
      {
        Console.WriteLine("Address Family: {0}, Socket Type: {1}, Protocol Type: {2}.", socket.AddressFamily, socket.SocketType, socket.ProtocolType);
      }
      Console.ReadLine();
    }
  }

  struct TestStruct1
  {    
    public List<Socket> Sockets;
  }
}
Something you can't do is initialise struct members within the struct itself and notice you don't need to use new when declaring the struct as it's a value type, like Integer32 (int).

--

Joe (Microsoft MVP - XML)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generic List bound to GridView klavastrius ASP.NET 2.0 Basics 0 April 21st, 2008 12:05 PM
Generic List Serialisation with custom property rodmcleay C# 2005 1 April 23rd, 2007 12:52 PM
generic List<T> Sort() shadowcodes C# 2005 2 February 15th, 2007 01:30 PM
Generic Error occured in GDI+ labby C# 2005 0 September 22nd, 2006 04:37 AM
paging a generic list trancehead ASP.NET 2.0 Professional 0 July 7th, 2006 03:39 AM



All times are GMT -4. The time now is 09:08 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.