VBnet: Reference a form object from a class

VBnet: Reference a form object from a class



plan



im sure this question must have been answered somewhere because its pretty basic, but unfortunately I havent found the answer ...



My plan is to create a simple log function that uses a RichTextField as an output and implements functionalities like Add, AddLine, RemoveLine, ReplaceLine, ...



what doesnt work



whenever i try to access the RichTextBox object from within the log Class, i get a "is nothing" exception.



my approach



the idea was to store a reference to the RichTextBox in the class itsself, that is passed on creating a new class instance:


Public Class Log
Dim _logBox As RichTextBox

Public Sub New(ByRef logBox As RichTextBox)
_logBox = logBox
End Sub

Public Sub AddLine(ByVal text As String)
Me.Add(text)
_logBox.AppendText(Environment.NewLine)
End Sub
End Class



And in my Form class, a RichTextBox is created at startup and passed to the log Class:


Public Class Form1
Dim log As New Log(RtbxLog) ' RtbxLog: RichTextBox object created on form

[on some button click event]
log.AddLine("entry with new line") ' THIS CAUSES "nothing" EXCEPTION
End Class






what is RtbxLog?

– Simo
Sep 6 '18 at 14:53






Thats the RichTextBox element used for the log. Updated question.

– L. Heinrichs
Sep 6 '18 at 14:54






I'm glad you found a solution! However, please don't put it into your question as doing so makes it harder to find, and it doesn't actually belong there :). The question should only contain the actual question and the information necessary to reproduce the problem. If you want to show others how you solved the problem you are very welcome to post your own answer as well, in addition to the_lotus's! (Just don't forget to accept his/hers :)

– Visual Vincent
Sep 6 '18 at 15:23







Couldnt accept before, as i had to wait some time first, but thats done now. Hm i always add the solution i chose to the questions once they were answered, so people find everything in one place :P

– L. Heinrichs
Sep 6 '18 at 15:29






Yeah, it's not that uncommon, but it should preferably not be done as it defeats the purpose of Stack Overflow's Q&A system. Even answers that aren't marked as the accepted one can still be of value.

– Visual Vincent
Sep 6 '18 at 15:33





1 Answer
1



RtbxLog isn't initialized until the New is called. This is usually done during InitializeComponent(). I suggest you create your log after InitializeComponent is called (in the new).



Also, might I suggest you create your own control (that inherit from RichTextBox) instead. Or an other option is that your log class only store the information and the form takes care of display it in the textbox.






thanks! this was the reason. Moved the instance creation of Log Class to Form.OnLoad.

– L. Heinrichs
Sep 6 '18 at 15:35



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế