Connecting Tech Pros Worldwide Help | Site Map

Why can't I get this to work

!NoItAll's Avatar
Member
 
Join Date: May 2006
Location: Madison, Wi
Posts: 76
#1: Sep 4 '09
Expand|Select|Wrap|Line Numbers
  1. Friend Structure eMailMessageType
  2.      Friend GoodMsg as System.Text.Stringbuilder
  3.      Friend BadMsg as System.Text.StringBuilder
  4. End Structure
  5.  
  6. Friend Function MakeMessage() as Boolean
  7.  
  8.       Dim Messages as New eMailMessageType
  9.  
  10.       Messages.GoodMsg.Append("Some Good Messages")
  11.       Messages.BadMsg.Append("Some Bad Messages")
  12.       'do something with the message struct here...   But it never gets here without an exception
  13.  
  14.       Return True
  15.  
  16. End Function
  17.  
  18.  
When I try to use this it says
Object reference not set to an instance of an object.
use the new keyword to create an object reference

I've tried to use this every which way from Sunday (an inane American expression meaning "nearly every way I can"), but have just not been able to use the stringbuilder class in a structure.
I've tried it with and without the new declaration, as friend, as public...
I'd love a hint or two!
!NoItAll's Avatar
Member
 
Join Date: May 2006
Location: Madison, Wi
Posts: 76
#2: Sep 4 '09

re: Why can't I get this to work


Ok - after trying 1/2 the day yesterday and a bunch this morning I got it to work - so I will answer my own question in case someone finds it helpful in the future.
The issue appears that we need to initialize the member variables to the structure using the NEW keyword - not just the object itself. This requires a little more code in the creation of the object - but not a lot.
It's steeped in cryptic science (never used the "with" statement and curly brackets before), but it works. Perhaps someone else can suggest a better way - but here is the working version of my code sample from above:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Text
  2. ---------------------------------------------------
  3.    Public Structure eMailMessageType
  4.         Public GoodMsg As StringBuilder
  5.         Public BadMsg As StringBuilder
  6.     End Structure
  7.     Friend Messages As New eMailMessageType With {.BadMsg = New StringBuilder(), .GoodMsg = New StringBuilder()}
  8.  
  9.     Friend Function MakeMessage() As Boolean
  10.  
  11.         Messages.GoodMsg.AppendLine("Some Good Messages")
  12.         Messages.BadMsg.AppendLine("Some Bad Messages")
  13.  
  14.        'now I can do something useful with Messages...
  15.  
  16.         Return True
  17.  
  18.     End Function
  19.  
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#3: Sep 4 '09

re: Why can't I get this to work


Sorry I didn't read this earlier.

In VB.NET there's not much of a difference between Structures and Objects....the line is very thin.

Anyways (even though you've already discovered this the hard way), just like Objects, Structures have to be instantiated using the "new" keyword.
Reply