473,396 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why can't I get this to work

!NoItAll
297 100+
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!
Sep 4 '09 #1
2 1111
!NoItAll
297 100+
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.  
Sep 4 '09 #2
Frinavale
9,735 Expert Mod 8TB
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.
Sep 4 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: The Road To Utopia | last post by:
Here's one for the trolls...a common jibe from them is setting up audio/video hardware under linux. Ok, true story: at work today, someone asked me if I could tell him why his XP Home would play...
119
by: rhat | last post by:
I heard that beta 2 now makes ASP.NET xhtml compliant. Can anyone shed some light on what this will change and it will break stuff as converting HTML to XHTML pages DO break things. see,...
12
by: lkrubner | last post by:
Is there any reason why this code wouldn't prompt someone to save their work every 5 minutes? It doesn't seem to be working. This code shows up in the HEAD of an HTML document. function...
1
by: Lyle Fairfield | last post by:
I created a new MS-SQL Database, "TestODBC". I made Table1 and StoredProcedure1. I made an ODBC DSN for that MS-SQL Database. I created a new AccessXP mdb, "TestODBC". I linked to the...
5
by: MGFoster | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I've converted an ACC97 .mdb file to an ACC2K2 .adp. A report that worked in ACC97 doesn't work in ACC2K2. Report setup: ACC97 ...
9
by: kernelxu | last post by:
hi, everyone. now, I'am confused on such a problem: function setbuf(stdin, NULL) or setvbuf(stdin, NULL, _IONBF, 0) can set the stadard input stream unbuffered. however, why does my program...
1
by: Larry | last post by:
These is a interface " IPerson" and a class "SoftwareDeveloper" support the interface; public __gc __interface IPerson { void Eat(); void Sleep(; void Work();
9
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not...
15
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.