?
Solved

Best practices for managing multiple forms in VB.NET CF

Posted on 2007-11-14
4
Medium Priority
?
1,417 Views
Last Modified: 2013-12-21
I'm at a stand stil at the moment with an application I am developing under contract. Unfortunately I've run into a fairly serious native exception error that I'm having a helluva time debugging. I paid the cash for a Microsoft incident to help, but my support engineer won't be available until tomorrow morning. He took an initial look at my code and said the following:
--------------------------------------------------------------------
Hi Mike,

I did a review of your code and I think I know what is happening, I’m just not quite sure why; I don’t think you should be able to do what you are code is doing:

The errant code is:

    Private Sub btnBagDates_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBagDates.Click
        Try
            QuarterBagChange.CollectBagChangeDates()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "btnBagDates_Click")
            ExitApp(True)
        End Try
    End Sub

You can’t do that. QuarterBagChange is a Public Windows Form class and CollectBagChangeDates is an instance method, not a class  (Shared) method. The instance method is referring to internal fields in the class, otherwise, it could be made Shared to make it a class method.

To use an instance method of an object, you have to instantiate the object and make calls via the instance. You haven’t created a new instance of QuarterBagChange. You need something like this:

    Dim qbc as QuarterBagChange = New QuarterBagChange()
    qbc.CollectBagChangeDates()

But this is only the beginning of the problems: QuarterBagChange is actually a Windows Form which must have Show or ShowDialog called to display it and Dispose called to free it. BTW, Dispose on Windows Forms objects is NOT optional; Windows Forms in NETCF do not support finalizers so Dispose() must be called or you will leak native window objects.

I suspect some architectural work (like factoring out service functions from your forms and moving them into non-forms utility classes) will be needed to untangle your business requirements from the current forms hierarchy.

The reason I don’t know why you can do what you are doing is that it should not be possible to call an instance method without an instance variable; perhaps the rules for Visual Basic are looser than for C#. I know the C# compiler would choke on equivalent syntax. Please let me know if this resolves your issue.
--------------------------------------------------------------------

He then updated with a new email that said:

--------------------------------------------------------------------
P.S. I just realized why you didn’t get an error: VB.NET operates like VB 6.0 did – there is a hidden QuarterBagChange object created as soon as you reference QuarterBagChange and it is named QuarterBagChange. Unfortunately, this is fact is hidden from the VB programmer just as it was in VB 6 and in VB.NET it can wreak the havoc you are currently experiencing. You need to eliminate the hidden object by never using the class name to qualify an instance method call.
--------------------------------------------------------------------

NOW ... what I need to see is VB.NET code that CORRECTLY handles multiple forms in the CF framework (2.0 on WM5). I have taken several courses in OOP, so I understand the concepts well. Unfortunately, however, my self taught knowledge in VB from the old days (5 and 6) have caused me to code in .NET the way I use to. So I am not accustomed to dealing with forms in any other way than I am now.

Should I somehow be using a publicly scoped set of routines and functions that essentially "puppet" the forms I created in the application? If so, how do I do this? Can anyone point me to some good examples?

Thank you,

Mike Sims
0
Comment
Question by:mikesims10670
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 20288618
is QuarterBagChange a form?
0
 
LVL 1

Author Comment

by:mikesims10670
ID: 20289631
Yes
0
 
LVL 48

Accepted Solution

by:
Mikal613 earned 2000 total points
ID: 20289711
so you should initaiate QuarterBagChange

dim fun as  new QuarterBagChange
QuarterBagChange.Show

On the form_load of QuarterBagChange
you call
CollectBagChangeDates()
0

Featured Post

Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With Windows Embedded Handheld, called Windows Mobile, Microsoft re-designed the user interface. The Start Icon moved down to the bottom, inside the menu bar area.   If you need to hide the Start Icon and/or the SIP (soft input panel, softwar…
Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question