Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 29th, 2007, 02:43 PM
Registered User
 
Join Date: Oct 2007
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!
 
Old October 29th, 2007, 04:35 PM
Registered User
 
Join Date: Oct 2007
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>();
};

 
Old October 30th, 2007, 03:28 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)





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





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