Results 1 to 4 of 4

Thread: Silly Error... Need Someones Advice (Win form)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Silly Error... Need Someones Advice (Win form)

    hi,

    I am encountering this silly error. i have two forms and a module. Form1 Collects data, Module contains Public Variable which stores data. Form2 Process the request. in Form 2 there are two classes, first one processes, and second one initiates the process. In second class which initiates the process(filling listbox of form by value present in public variable), in that class i din created object of first class. i simple used form2.listbox1.items.add("xxxx"). it was working fine. Now in order to set mdiparent property i used dim s as new form1 s.mdiparent=parentform s.show(). in form 2 at begining of class1(Class that processes) i set dim ins1 as new initates1. in constructor of initiates1 i have called a function in which i have used streamreader to insert values in arraylist.

    Now problem, is that i cant find anything in my listbox. its working fine if i din created any object, and if i have used from2.listbox.items.add..

    please help me.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Silly Error... Need Someones Advice (Win form)

    There isn't enough information to be certain, but there are a couple points that could be causing you trouble. When you mention using Form2.Listbox, that makes me think that you are using the default instance of Form2, which suggests that you were using the default instance of form1, as well. I really hate default instances because they confuse people who don't know about them. You have to have an instance of a form to work with it, and for all other objects you have to specifically create an instance. With forms, there is a default instance that is quietly created for each form which has the same name as the form class name (the default instance of form type Form2 is called Form2). For people who are new to Object Oriented design, this blurrs the distinction between an object type and an instance of the object.

    In this case, you don't specifically state that you create an instance of Form2, but you specifically state that you WERE using default instances, and have created a different instance of Form1. Therefore, I suspect that either you are showing the wrong instance of Form2 (you are showing one that was never loaded), or that your code makes use of the default instance of Form1 to fill the global variables, and now that you are using a non-default instance of Form1, you are no longer filling the globals.

    To see which it is, we'd probably have to see some code that shows the form that has the listbox, but that would only be a first step, as it might not be where the problem lies.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Silly Error... Need Someones Advice (Win form)

    mate actually global variables are filled, but listbox on form1 is not getting filled. now i didi something.. and its filling. can u please explain me about object type and instance type?

    ~~~~~distinction between an object type and an instance of the object~~~~~

    I am a leaner(beginner).. please explain me more clearly.. i would really appreciate it..

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Silly Error... Need Someones Advice (Win form)

    That's what I dislike about default instances, you can get a long ways without knowing the difference.

    When you declare a form:

    Dim myForm as SomeForm

    "SomeForm" is the type of form. You can make any number of instances of type SomeForm:

    Dim myForm1 as SomeForm
    Dim myForm2 as SomeForm
    etc.

    In those statements, myFormx could be seen as instances of type SomeForm. However, I would have to use the New keyword to actually create them:

    Dim myForm1 as New SomeForm

    The first lines just told the compiler that there will be an object of type SomeForm created at some time, so it knows to set aside enough memory for the object. When I use the New keyword, it tells the compiler not just to set aside the memory, but to actually create an instance of SomeForm and put it in that memory. The type is SomeForm, the instance is myForm1.

    In VB.NET up until 2005, that was how you had to create ANY form. However, in VB6 you never needed to create a specific instance, and to make .NET more familiar to VB6 programmers, MS added default instances in 2005. The way a default instance works is that when you add a form to the designer called SomeForm, VB creates a global instance of the type with the same name as the type. This would be the same as if you added:

    Public SomeForm As New SomeForm

    to a module. SomeForm is the type, but there is also an instance of SomeForm called SomeForm that is visible to ALL. If that last statement seems confusing, that's because it is. For everything else you write, YOU have to create the variables, and YOU decide how to use them, where they are visible, who can access them, etc. For forms, MS decided to add in a universally accessible instance that is created automatically, so you never actually see it, and give it the same name as the class name. It makes it easy as long as you are doing really simple programs, and sows confusion if you are doing anything more complex. You ALWAYS have to work with an instance of an object, but for most forms, MS has created an instance for you, whether you like it or not. Use it at your own peril.

    One thing I haven't actually tested is what happens if you get rid of the default constructor for the form and add a constructor that takes an argument:
    Code:
    Public Class SomeForm
      Inherits Windows.Forms.Form
    
     Public Sub New (myInt as integer)
    
     End Sub
    
    End Class
    Since the default instance is global, it has to exist prior to the start of the program, which means that it has to be constructed before the program begins. Since this form requires an integer, no valid form CAN be constructed before the program begins, so a default instance should be either impossible or invalid. I haven't tested which route MS chose. Is there a default instance created for forms with no default constructor?
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width