locked
Things About Variable Declaration RRS feed

  • Question

  • I've many forms, one of which is a main form

    I've declared many variables in each form

    So I want to know in what sequence these variables are loaded i.e. when the app runs, only those variables which are declared in that form(main form) are loaded or all variables from all forms are loaded before the app starts?

    Basically, I need this to load the data into different forms from my splash screen


    Sameer Thigale
    Wednesday, January 19, 2011 7:33 AM

Answers

  • Sameer,

    if you need to be that sure, you should get some memory profiler and do some tests in real. You should read this http://www.getdotnetcode.com/gdncstore/free/Articles/The%20Memory%20Mystery.htm especially the last chapter, so you know memory doesn`t matter that much ->  "I was told that if RAM memory is plentiful, an application may grab a very generous chunk of it. An application will not be so 'greedy' (my own words) if available memory is scarce. "


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:07 PM
    Wednesday, January 19, 2011 4:46 PM
  • Hi Sameer,

    for every class ( a form isn`t anything else ) the first thing which is be called is the Sub New() method, but before the code in this New() method is executed the variables which are declared in the declaration part of the class and which are actually createing an new instance of some class are initialised first.

    As an example:

     

    The order of execution/initialisation will be:

    'Visual Basic 2008 - .net 3.5 - Any CPU
    Public Class Form1
     Private var1 As New String("1"c, 1)
     Private var2 As Point = New Point(1, 1)
     Private var3 As testclass
     Private var4 As New testclass
     Public Sub New()
     InitializeComponent()
     var3 = New testclass
     End Sub
    
    End Class
    
    Public Class testclass
     Private var1 As Point
     Private var2 As Point = New Point(1, 1)
     Public Sub New()
    
     var1 = New Point(1, 1)
     End Sub
    End Class
    

     

    - Form1.New() will be called
    - Form1.var1 will be created as a new instance of the String class
    - Form1.var2 will be created as a new instance of the Point class
    - Form1.var4 will be created as a new instance of the testclass class
           -> the New() method of the testclass will be called
               - testclass.var2 will be created as a new instance of the Point class
            -> the code inside the New() method will be executed
               - testclass.var1 will be created as a new instance of the Point class
    - the code inside the Form1.New() method will be executed
        -> 
          - Form1.var3 will be created as a new instance of the testclass class
           -> the New() method of the testclass will be called
               - testclass.var2 will be created as a new instance of the Point class
            -> the code inside the New() method will be executed
               - testclass.var1 will be created as a new instance of the Point class


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:05 PM
    Wednesday, January 19, 2011 8:35 AM
  • Sameer,

    It is not the program who does it but the programmer.

    To get more insight in that do:

    Do in your project 

    Go to solution Explorer, then look to the top of that, one of the icons is the "show all files".

    After activating that the

    ClassName.designer.vb files become visible.

    Have a look at those, don't change them.

    You will see in that in what sequence all kind of things are added to memory.

     

     


    Success
    Cor
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:07 PM
    Wednesday, January 19, 2011 9:25 AM
  • VB.net will do the very minimum to save memory, so it will only call when required, so unless the variable is in the form_load sub or out of a sub in the class header then it wont be called till its needed.

    and seperate forms will only be called when needed otherwise your app will not run as lean as possible, this can cause slow apps and in suvear cases memory leaks and PC crashes.


    Sorry if my replies are a bit brief but i feel we learn more if we have to think about things. A small clue is better than a spoiler.
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:06 PM
    Wednesday, January 19, 2011 9:31 AM
  • Hi Heslacher,

    What do you reckon happens then with the default instances ?

    Their isn't a NEW instance of Form2 when I use .Show() in this code.

    In other words I do not have the following:

    Dim frm2 As New Form2

    frm2.Show()

     

    Public Class Form1
    
     Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
    
      Form2.Show()
      Form2.BringToFront()
    
     End Sub
    
    End Class

     

    Here is something for fun for you to think about.

    • Add this Class to a New Project or PASTE this code after the Form1 Class ( after End Class for Form1 ).
    • Select the first item in the BUILD menu.
    • Drop an instance of MyTextBox from the ToolBox onto your Form1 in design mode.

     

    You will see the TextBox text scrolling without even starting the main application .  ;-)   :-D   :-)

     

    Public Class MyTextBox
      Inherits System.Windows.Forms.TextBox
      Friend WithEvents myTimer As New Timer
    
      Public Sub New()
    
        myTimer.Start()
    
      End Sub
    
      Private Sub myTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles myTimer.Tick
    
        Dim someString As String = "supercalifragilisticexpialidocious"
        Static index As Integer
    
        Me.Text = someString.Substring(index)
        index += 1
        If index = someString.Length Then index = 0
    
      End Sub
    End Class

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    • Edited by John Anthony Oliver Wednesday, January 19, 2011 2:40 PM
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:06 PM
    Wednesday, January 19, 2011 2:22 PM
  • It's getting quite confusing!!

    So  thought a way to get the final answer

    I've posted a code similar to what I've and expect

    Please tell me now in what sequence the variables will be stored in memory and how the forms will be initialized:

     

    Public Class MainForm ' this is the startup form
    Dim a1 As Integer = 0
    Dim a2 As String
    
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Form2.b1=1
    Form2.b2 = "Test"
    
    End Sub
    End Class
    Public Class Form2 'Next Form
    Dim b1 As Integer
    Dim b2 As String
    
    
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    b1=12
    End Sub
    End Class
    Public Sub Form3 'Never loaded form
    Dim c1 As Integer
    Dim c2 As String
    
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    End Class
    
    

     

    Now tell me:

     

    1. Will all a1,a2,b1,b2,c1,c2 be loaded into memory when the app starts at the same instance?
    2. Since Form3 is never called i.e. [no Form3.Show()] will the variables c1,c2 never occupy any space in memory?
    3. What about b1 and b2, since they are initialized before the Form2.Show() sub?

     

    The program does has no bugs. These are Windows Forms. Reply Immediately.

    Please Reply Heslacher, Cor and todda2008


    Sameer Thigale
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:06 PM
    Wednesday, January 19, 2011 2:50 PM
  • Sameer,

    after the MainForm load event:

    a1=0
    a2=""
    b1=1 -> because the Form load event of Form2 isn`t called until execution of the Show() or ShowDialog() method
    b2="Test"

    c1 and c2 won`t be in memory.


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    • Proposed as answer by John Anthony Oliver Wednesday, January 19, 2011 4:20 PM
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:06 PM
    Wednesday, January 19, 2011 3:09 PM
  • Isn't there any book published the .NET developer him(/her)self to refer to?

    B'coz Heslacher says NO, John says YES

    Or each one of you just tell me your references so that the other person can get to know


    Sameer Thigale


    Hi Sameer,

    Heslacher is correct , when you run an application unless you access a variable

    name that is a member of Form3 then Form3 and its variables do NOT exist in memory until you do so.



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:08 PM
    Wednesday, January 19, 2011 4:44 PM
  • Finally I mark answers closing this post. :(

    The site mentioned by Heslacher is the best solution

    And also not forgetting the work of John, Cor and of course myself!

    Also what todda2008 has posted is correct. :)

    This Post is quiet lengthy, in case any one referring it, do read it carefully post by post

    Happy To Help


    Sameer Thigale
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:10 PM
    • Edited by Sam9584 Thursday, January 20, 2011 4:13 PM Being Happy To Help
    Thursday, January 20, 2011 4:05 PM

All replies

  • Hi Sameer,

    for every class ( a form isn`t anything else ) the first thing which is be called is the Sub New() method, but before the code in this New() method is executed the variables which are declared in the declaration part of the class and which are actually createing an new instance of some class are initialised first.

    As an example:

     

    The order of execution/initialisation will be:

    'Visual Basic 2008 - .net 3.5 - Any CPU
    Public Class Form1
     Private var1 As New String("1"c, 1)
     Private var2 As Point = New Point(1, 1)
     Private var3 As testclass
     Private var4 As New testclass
     Public Sub New()
     InitializeComponent()
     var3 = New testclass
     End Sub
    
    End Class
    
    Public Class testclass
     Private var1 As Point
     Private var2 As Point = New Point(1, 1)
     Public Sub New()
    
     var1 = New Point(1, 1)
     End Sub
    End Class
    

     

    - Form1.New() will be called
    - Form1.var1 will be created as a new instance of the String class
    - Form1.var2 will be created as a new instance of the Point class
    - Form1.var4 will be created as a new instance of the testclass class
           -> the New() method of the testclass will be called
               - testclass.var2 will be created as a new instance of the Point class
            -> the code inside the New() method will be executed
               - testclass.var1 will be created as a new instance of the Point class
    - the code inside the Form1.New() method will be executed
        -> 
          - Form1.var3 will be created as a new instance of the testclass class
           -> the New() method of the testclass will be called
               - testclass.var2 will be created as a new instance of the Point class
            -> the code inside the New() method will be executed
               - testclass.var1 will be created as a new instance of the Point class


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:05 PM
    Wednesday, January 19, 2011 8:35 AM
  • My question is a bit different. I'm using Windows Forms with VB.NET.

    Sorry but I already know what you've posted.

    The question is, When I run my app in Step Into mode, I saw that,

    all variables of only the start up form was first initialized, then when my app called a variable from another form it was first initialized then the call took place

    Strange and weird.

    So was that form loaded? before the Form1.Show() is called?


    Sameer Thigale
    Wednesday, January 19, 2011 9:07 AM
  • Sameer,

    It is not the program who does it but the programmer.

    To get more insight in that do:

    Do in your project 

    Go to solution Explorer, then look to the top of that, one of the icons is the "show all files".

    After activating that the

    ClassName.designer.vb files become visible.

    Have a look at those, don't change them.

    You will see in that in what sequence all kind of things are added to memory.

     

     


    Success
    Cor
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:07 PM
    Wednesday, January 19, 2011 9:25 AM
  • So was that form loaded? before the Form1.Show() is called?


    No, but the constructor ( New() method ) of the form will be executed and anything else i have posted above.
    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Wednesday, January 19, 2011 9:28 AM
  • VB.net will do the very minimum to save memory, so it will only call when required, so unless the variable is in the form_load sub or out of a sub in the class header then it wont be called till its needed.

    and seperate forms will only be called when needed otherwise your app will not run as lean as possible, this can cause slow apps and in suvear cases memory leaks and PC crashes.


    Sorry if my replies are a bit brief but i feel we learn more if we have to think about things. A small clue is better than a spoiler.
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:06 PM
    Wednesday, January 19, 2011 9:31 AM
  • Thanks todda2008 and Heslacher,

    So is the conclusion that, when an app starts, all new() of each and every form is called but the variables are stored only it is called, even if it is a public variable?


    Sameer Thigale
    Wednesday, January 19, 2011 10:20 AM
  • Not exactly,

    the new() method is called when you either create a new instance of the form or when you access a public variable of the form. After the execution of the new() method the variables inside the form will be initialised.

    The code inside the  form_load event is executed when you have called the form.show/showdialog method.


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Wednesday, January 19, 2011 10:30 AM
  • Sameer,

    No that is not the conclusion, it is your conclusion but not the conclusion.

    It depends how the programmer did it when the variables are created. 

    Stored is even a complete different matter.

     


    Success
    Cor
    Wednesday, January 19, 2011 12:17 PM
  • Hi Heslacher,

    What do you reckon happens then with the default instances ?

    Their isn't a NEW instance of Form2 when I use .Show() in this code.

    In other words I do not have the following:

    Dim frm2 As New Form2

    frm2.Show()

     

    Public Class Form1
    
     Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
    
      Form2.Show()
      Form2.BringToFront()
    
     End Sub
    
    End Class

     

    Here is something for fun for you to think about.

    • Add this Class to a New Project or PASTE this code after the Form1 Class ( after End Class for Form1 ).
    • Select the first item in the BUILD menu.
    • Drop an instance of MyTextBox from the ToolBox onto your Form1 in design mode.

     

    You will see the TextBox text scrolling without even starting the main application .  ;-)   :-D   :-)

     

    Public Class MyTextBox
      Inherits System.Windows.Forms.TextBox
      Friend WithEvents myTimer As New Timer
    
      Public Sub New()
    
        myTimer.Start()
    
      End Sub
    
      Private Sub myTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles myTimer.Tick
    
        Dim someString As String = "supercalifragilisticexpialidocious"
        Static index As Integer
    
        Me.Text = someString.Substring(index)
        index += 1
        If index = someString.Length Then index = 0
    
      End Sub
    End Class

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    • Edited by John Anthony Oliver Wednesday, January 19, 2011 2:40 PM
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:06 PM
    Wednesday, January 19, 2011 2:22 PM
  • Hi Heslacher,

    What do you reckon happens then with the default instances ?

    Their isn't a NEW instance of Form2 when I use .Show() in this code.

    In other words I do not have the following:

    Dim frm2 As New Form2

    frm2.Show()

     

    Public Class Form1
    
     Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
    
      Form2.Show()
      Form2.BringToFront()
    
     End Sub
    
    End Class
    

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Sorry but I couldn't understand :(


    Sameer Thigale
    Wednesday, January 19, 2011 2:35 PM
  • John,

    I hope you don't mind I answer also.

    The Form instance is created as shared on the stack (in VB and framework 2.0 and newer). 

     But a form still references all kind of other objects, those are only put in memory as soon as the program gives an instruction for that.

    Most of those things, as it is a form, is done in the designer.vb code but that message from me is totally ignored by the OP.

     


    Success
    Cor
    Wednesday, January 19, 2011 2:38 PM
  • Hi Heslacher,

    What do you reckon happens then with the default instances ?

    Their isn't a NEW instance of Form2 when I use .Show() in this code.

    John,

    but the constructor(New() method) is called anyway and the rest is like i have written in my first post.


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Wednesday, January 19, 2011 2:48 PM
  • It's getting quite confusing!!

    So  thought a way to get the final answer

    I've posted a code similar to what I've and expect

    Please tell me now in what sequence the variables will be stored in memory and how the forms will be initialized:

     

    Public Class MainForm ' this is the startup form
    Dim a1 As Integer = 0
    Dim a2 As String
    
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Form2.b1=1
    Form2.b2 = "Test"
    
    End Sub
    End Class
    Public Class Form2 'Next Form
    Dim b1 As Integer
    Dim b2 As String
    
    
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    b1=12
    End Sub
    End Class
    Public Sub Form3 'Never loaded form
    Dim c1 As Integer
    Dim c2 As String
    
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    End Class
    
    

     

    Now tell me:

     

    1. Will all a1,a2,b1,b2,c1,c2 be loaded into memory when the app starts at the same instance?
    2. Since Form3 is never called i.e. [no Form3.Show()] will the variables c1,c2 never occupy any space in memory?
    3. What about b1 and b2, since they are initialized before the Form2.Show() sub?

     

    The program does has no bugs. These are Windows Forms. Reply Immediately.

    Please Reply Heslacher, Cor and todda2008


    Sameer Thigale
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:06 PM
    Wednesday, January 19, 2011 2:50 PM
  • Hi Cor,

    If that is the case, what happens when you do something like I have done at the end of my last post?

    I create a Class with a Timer, start the Timer in the NEW Sub.

    As soon as you add an instance of MyTextBox ( select BUILD from the BUILD menu first ) to a Form from

    the ToolBox in design mode the IDE shows the scrolling text TextBox without running the main application .

     

    So is all of this happening on the Form instance on the stack?

    How is the myTimer code running without running the main application?

    Sorry if I am making this thread a bit more complicated.



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Wednesday, January 19, 2011 2:53 PM
  • John,

    by placing your control onto the form the constructor of your control is called.


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Wednesday, January 19, 2011 2:59 PM
  • Sorry but I couldn't understand :(


    Sameer Thigale

    Hi Sameer,

    Do not worry, keep on coding , you will soon get the idea of an instance of a Class.

    Just think of two different items like a pen and a pencil. They are two different objects.

    When it comes to OOP ( object orientated programming ) you will begin to think about separate items.

    VB.Net creates a "default instance" when it comes to Forms.

     

    Dim myForm As New Form1

     

    In the above line myForm is another Form1



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Wednesday, January 19, 2011 3:00 PM
  • Hey people have a look at the code I've published :(
    Sameer Thigale
    Wednesday, January 19, 2011 3:06 PM
  • It's getting quite confusing!!

    So  thought a way to get the final answer

    I've posted a code similar to what I've and expect

    Please tell me now in what sequence the variables will be stored in memory and how the forms will be initialized:

     

     

    Now tell me:

     

    1. Will all a1,a2,b1,b2,c1,c2 be loaded into memory when the app starts at the same instance?
    2. Since Form3 is never called i.e. [no Form3.Show()] will the variables c1,c2 never occupy any space in memory?
    3. What about b1 and b2, since they are initialized before the Form2.Show() sub?

     

    The program does has no bugs. These are Windows Forms. Reply Immediately.

    Please Reply Heslacher, Cor and todda2008


    Sameer Thigale

    Hi Sameer,

    I reckon you will also have c1 and c2 loaded into memory.

    The reason being is that Form3 still has an instance in memory that you are choosing not to .Show()

     

    Can you delete one of your duplicate posts above please ?



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Wednesday, January 19, 2011 3:07 PM
  • Sameer,

    after the MainForm load event:

    a1=0
    a2=""
    b1=1 -> because the Form load event of Form2 isn`t called until execution of the Show() or ShowDialog() method
    b2="Test"

    c1 and c2 won`t be in memory.


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    • Proposed as answer by John Anthony Oliver Wednesday, January 19, 2011 4:20 PM
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:06 PM
    Wednesday, January 19, 2011 3:09 PM
  • c1 and c2 won`t be in memory.


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Hi Heslacher,

    Are you sure?

    Try this.>>

    Public Class Form1
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        MessageBox.Show(Form3.c1.ToString)
        MessageBox.Show(Form3.c2)
    
      End Sub
    End Class
    
    Public Class Form3
    
      Public c1 As Integer = 246
      Public c2 As String = "Hi there Heslacher. :-)"
    
    End Class
    

     

    Just because the original Form3 here used DIM ( which defaults to PRIVATE ) it does not

    mean the variables are not loaded into memory just because you can not see the Form.

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Wednesday, January 19, 2011 3:19 PM
  • John,

    again: by accessing the var of the form you execute the code inside the constructor of the form and before that the variables which have got a value assigned like in your example ) will be initialised to that value.

    Like:

     

    'Visual Basic 2008 - .net 3.5 - Any CPU
    Public Class Form2
      Public x As Integer
      Public s As String
      
    End Class
    

     guess what happens here:


    'Visual Basic 2008 - .net 3.5 - Any CPU
        If Form2.s Is Nothing Then
          MsgBox("Not initialised")
        End If
    

     



    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Wednesday, January 19, 2011 3:27 PM
  • Hi Sameer,

    You would not be able to do this.>>

     

    Public Class MainForm ' this is the startup form
    Dim a1 As Integer = 0
    Dim a2 As String
    
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Form2.b1=1
    Form2.b2 = "Test"
    
    End Sub
    End Class

     

    as DIM defaults to PRIVATE you can not access b1 and b2 on Form2.

    You should only use DIM and STATIC to declare variables within a SUB or FUNCTION.

    Outside of any SUB or FUNCTION please try to stick to using the keywords;

    Public , Private or Friend

    and please learn their meaning.

    Later on look up the meaning of the keywords;

     

    • Shared
    • Protected

     

    Public Class MyForm
    
      Public myString As String = "Hi!!"
      Private myNumber As Integer = 123
      Friend aDoubleVariable As Double
    
    End Class

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Wednesday, January 19, 2011 3:35 PM
  • I know Public, Protected and all. It can be understood. Don't forget The main point I'm interested in that.
    Sameer Thigale
    Wednesday, January 19, 2011 3:59 PM
  • John,

    again: by accessing the var of the form you execute the code inside the constructor of the form and before that the variables which have got a value assigned like in your example ) will be initialised to that value.

    Like:

     

    'Visual Basic 2008 - .net 3.5 - Any CPU
    
      If
     Form2.s Is
     Nothing
     Then
    
       MsgBox("Not initialised"
    )
      End
     If
    
    

     



    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Hi Heslacher,

    But the variable x in

    Public x As Integer isn't Nothing

    You are forgetting the default instances .

    If we did not have default instances I would not be able to access any of the Public variables.

     

    Are you trying to tell me that we only get a New instance of Form3 only when a variables value is looked up?

    If that is the case why should a call to GET a variables value also call the New method?

     

    Public Class Form1
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        MessageBox.Show(Form3.x.ToString, "x =")
        MessageBox.Show(Form3.s & IIf(Form3.s Is Nothing, " = Nothing!!", "").ToString, "s =")
    
        MessageBox.Show(Form3.nullableX.ToString & IIf(Form3.nullableX Is Nothing, " = Nothing!!", "").ToString, "nullableX =")
        MessageBox.Show(Form3.myString, "myString =")
    
      End Sub
    End Class
    
    Public Class Form3
    
      Public x As Integer
    
      Public s As String = Nothing
    
      Public nullableX As Nullable(Of Integer) = Nothing
      Public myString As String = "Hi"
    
    End Class
    



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Wednesday, January 19, 2011 3:59 PM
  • John,

    as soon as you access a method/variable of a default instance of a form this instance is created and the rest like above.

    Try this:

     


    'Visual Basic 2008 - .net 3.5 - Any CPU
        If Form2 Is Nothing Then
          MsgBox("Not initialised")
        Else
          MsgBox("initialised")
        End If
        Form2.s = "1"
        If Form2 Is Nothing Then
          MsgBox("Not initialised")
        Else
          MsgBox("initialised")
        End If
    

     


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Wednesday, January 19, 2011 4:09 PM
  • Isn't there any book published the .NET developer him(/her)self to refer to?

    B'coz Heslacher says NO, John says YES

    Or each one of you just tell me your references so that the other person can get to know


    Sameer Thigale
    Wednesday, January 19, 2011 4:35 PM
  • Hi Heslacher,

    Okay, I've learned something else today.

    Therefore I have proposed one of your earlier posts as answer .  :-)

    I have marked your above post as helpful .

    Thanks for your time, patience and persistance in this thread.  :-)  ;-)  :-D

    My earlier example of the control MyControl shows a custom control

    instance existing without a Form then until the main application is made to run?



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Wednesday, January 19, 2011 4:36 PM
  • John,

    i liked the discussion also ( it has diverted me from my lovely(sarcasm) java project ) ;-)

    Btw, you can callme Hannes ;-)


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Wednesday, January 19, 2011 4:42 PM
  • John,

    The application framework for forms is running in newer VB Versions as you have set that in the properties like default is done with a windows forms program and currently WPF forms.

    Project -> Application -> Enable Application Framework 

    VB has endless methods to initialize a Forms Applications a little bit to help the former VB6 developers. 

    All C# application starts with in a way this code, which can be used (translated) as well be done instead of that application Framework.

        [STAThread]
        static void Main()
        {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Application.Run(new Form1());
        }
      
    

    In VB we see this in fact in a Console application but than we use very correct the Module.

     

     


    Success
    Cor
    Wednesday, January 19, 2011 4:42 PM
  • Isn't there any book published the .NET developer him(/her)self to refer to?

    B'coz Heslacher says NO, John says YES

    Or each one of you just tell me your references so that the other person can get to know


    Sameer Thigale


    Hi Sameer,

    Heslacher is correct , when you run an application unless you access a variable

    name that is a member of Form3 then Form3 and its variables do NOT exist in memory until you do so.



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:08 PM
    Wednesday, January 19, 2011 4:44 PM
  • Sameer,

    if you need to be that sure, you should get some memory profiler and do some tests in real. You should read this http://www.getdotnetcode.com/gdncstore/free/Articles/The%20Memory%20Mystery.htm especially the last chapter, so you know memory doesn`t matter that much ->  "I was told that if RAM memory is plentiful, an application may grab a very generous chunk of it. An application will not be so 'greedy' (my own words) if available memory is scarce. "


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:07 PM
    Wednesday, January 19, 2011 4:46 PM
  • John,

    Also checked my reply on your question to me, I did not even see it so quick you was with answering another part in this thread.

     


    Success
    Cor
    Wednesday, January 19, 2011 4:50 PM
  • Cor, should I draw any conclusion now, cause I'm waiting for your reply?
    Sameer Thigale
    Thursday, January 20, 2011 11:45 AM
  • I guess this may be happening with all .NET apps, whether C#, C++, VB or WPF

    this is such a  bug generating process

    And what about the Dispose() method then?


    Sameer Thigale
    Thursday, January 20, 2011 11:57 AM
  • I guess this may be happening with all .NET apps, whether C#, C++, VB or WPF

    this is such a  bug generating process

    And what about the Dispose() method then?


    Sameer Thigale

    Bug? I cannot see where a bug has been demonstrated. Can you explain what you mean by bug and demonstrate it?

    Stephen J Whiteley
    Thursday, January 20, 2011 2:41 PM
  • Cor, should I draw any conclusion now, cause I'm waiting for your reply?

     

    Sameer,

    You should go through this tread and search for the replies which I've given, this thread becomes so long, that the information in hit has not any sense anymore.

    You can mark all the answers which have helped you in a way and then create a new question with a new header and description starting at the point you are now.

     


    Success
    Cor
    Thursday, January 20, 2011 3:39 PM
  • Finally I mark answers closing this post. :(

    The site mentioned by Heslacher is the best solution

    And also not forgetting the work of John, Cor and of course myself!

    Also what todda2008 has posted is correct. :)

    This Post is quiet lengthy, in case any one referring it, do read it carefully post by post

    Happy To Help


    Sameer Thigale
    • Marked as answer by Sam9584 Thursday, January 20, 2011 4:10 PM
    • Edited by Sam9584 Thursday, January 20, 2011 4:13 PM Being Happy To Help
    Thursday, January 20, 2011 4:05 PM