Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I'm 20% through writing an application to communicate with, and take readings from, an external device over a serial cable. The Comms is fine, the Office component AxOWC10.AxSpreadsheet control is a piece of cake but I'm completely stuck on Structures and/or scope.

 

Coding the data-manipulation begins with making preset templates for different uses of the external device. It's a bit like your clothes-washing machine, set the programme according to the kind of clothes you're putting into it. In this case, choosing a preset template sets column headings, numbers of columns, which equations to run and which columns they go in, how many cycles the data-acquisition goes through and which order the data are acquired.

 

I have 43 of these presets and I thought I could use a Structure to hold them. As a legacy from VB6, I use Module1 to put as many of my declarations as possible. As a legacy from even older BASICs, I still don't understand scope so I try to make all my variables visible from all forms. This isn't happening with my structure and I'm totally stuck.

 

Just now, I read on here about how Classes were the way to go so I had a (another) go at doing that. Then I realised that, in code view, all forms are Classes, so what kind of Class should I be doing... A Class definition in module1? A Class within my main Form's Class? I stopped that line of enquiry.

 

The World has gone mad; I must have been away that day.

 

I'll summarise what I've got here; please tell me what I have to do to make it work.

 

Module1 Declarations - seemed ok at first

    Public Structure Preset                         'filled from BALOPT.INI
       Public Opt As Integer                                'miscellaneous options flag
       Public Description As String                        'plain english preset desc.
       Public SetupNumber As Integer                   'which formulae-suite to apply
       Public TotalColumns As Integer
       Public TotalSessions As Integer
       Public ColumnHeadings() As String
       Public ColumnWeighSequence() As String      'which columns get filled in which cycle of weighing
       Public ColumnCompareSequence() As Integer 'which columns get equated when.
   End Structure

 

Form1 (AKA frmMain) - had to use ReDim statements to get arrays within "myPresets". This works fine, loads up 43 different templates into myPresets() so you can probably skip it.

    Public Sub LoadPresets()
       ...
       sr = IO.File.OpenText("BALOPT.INI")  'in the application folder

       ...
       Dim myPresets(Val(loadBuffer + 1)) As Preset
       Dim maxPresets = Val(loadBuffer)
       i = 1 'preset counter

       While sr.Peek <> -1      'loop until end of file
               ...
               'Option flag
               myPresets(i).Opt = Val(splitArray(0))

               'Description
               myPresets(i).Description = splitArray(1)

               'Setup # - affects calcs and # of cols
               myPresets(i).SetupNumber = Val(splitArray(2))

               '# cols
               myPresets(i).TotalColumns = Val(splitArray(3))

               '# sessions 
               myPresets(i).TotalSessions = Val(splitArray(4))

               'titles for each of the cols in .TotalColumns #
               m = myPresets(i).TotalColumns
               ReDim myPresets(i).ColumnHeadings(m)
               For j = 1 To myPresets(i).TotalColumns
                   myPresets(i).ColumnHeadings(j) = splitArray(5 + (j - 1))
               Next

               'weighing order
               ReDim myPresets(i).ColumnWeighSequence(m)
               For k = 1 To myPresets(i).TotalColumns
                   stringDatum = splitArray((6 + (m - 1)))
                   myPresets(i).ColumnWeighSequence(k) = Mid(stringDatum, k, 1)
               Next

               'compare order
               ReDim myPresets(i).ColumnCompareSequence(m)
               For l = 1 To myPresets(i).TotalColumns
                   stringDatum = splitArray((7 + (m - 1)))
                   myPresets(i).ColumnCompareSequence(l) = Val(Mid(stringDatum, l, 1))
               Next
           End If

       End While
       sr.Close()
       ...
   End Sub

 

The problem is that I can now only access it from within the sub that made it. As you can see, I tried to make it as available as possible with gratuitous (horrendous!) use of "Public". Even the Class of Form1 ("frmMain") is Public.

 

One place I need access to this data is in a separate form called frmConfigPresets. I just can't get it.

 

I'll happily accept any solution at all including ditching Module1 for a better way. Please bear in mind that I cannot follow statements which sound like: "All you gotta do is instantiate the class object in the function modifier". The last technical manual I understood was Forth Programming on the Jupiter Ace in 1980/81, when I was 10 years old. O'Reilly's "in a Nutshell" books are the closest I get to that 1980 experience.

 

Anyway...

 

TIA for any help.

 

Rgds,

David

Edited by David
  • Leaders
Posted

The type declaration is Public, that means you can declare it from anywhere in your project. The type declaration in the module is accessible from everywhere in your project... therefore, you can declare it everywhere.

 

However, the variable declaration that you have in the sub LoadPresets() is procedure-scope (Dim)... this means that only the procedure can use it.

 

If you want every procedure in the form to be able to use it, then you want the variable to be of form-scope... you would declare the Preset variable in the Form Declarations.

 

Private or Public in the Form Declarations depends on what you will be doing on your form. For the most part, everything in the form can be Private... however, if you will be passing references to the form, then anything that you may want to access from outside of the form / another form should be declared as Public.

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted (edited)

ummm. . . your structure is making thisharder than it has to be.

 

the serializable attribute will take care of everything for you.

 

look at this. . . I know you will think that this is more complicated but most of it was generated from the IDE automatically.

 

Trust me. . . it is ALOT less complicated that what you are doing. . .

Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO

<Serializable()> _
Public Class PresetColumn
Implements ICloneable
Private _header As String
Private _weight As String
Private _compare As Integer
Friend Sub New(ByVal header As String, ByVal weight As String, ByVal compare As Integer)
	_header = header
	_weight = weight
	_compare = compare
End Sub
Public Property Compare() As Integer
	Get
		Return _compare
	End Get
	Set(ByVal Value As Integer)
		_compare = Value
	End Set
End Property
Public Property Header() As Integer
	Get
		Return _header
	End Get
	Set(ByVal Value As Integer)
		_header = Value
	End Set
End Property
Public Property Weight() As Integer
	Get
		Return _weight
	End Get
	Set(ByVal Value As Integer)
		_weight = Value
	End Set
End Property
Public Function Clone() As Object Implements System.ICloneable.Clone
	Return New PresetColumn(_header, _weight, Compare)
End Function
End Class
<Serializable()> _
Public Class Preset
Implements IList
Implements ICloneable
Implements ICollection
Implements IEnumerable

Private _Columns As ArrayList = New ArrayList
Public Opt As Integer
Public Description As String
Public SetupNumber As Integer
Public TotalSessions As Integer
Public Sub CopyTo(ByVal array As System.Array, ByVal index As Integer) Implements System.Collections.ICollection.CopyTo
	_Columns.CopyTo(array, index)
End Sub
Public ReadOnly Property Count() As Integer Implements System.Collections.ICollection.Count
	Get
		Return _Columns.Count
	End Get
End Property
Public ReadOnly Property IsSynchronized() As Boolean Implements System.Collections.ICollection.IsSynchronized
	Get
		Return _Columns.IsSynchronized
	End Get
End Property
Public ReadOnly Property SyncRoot() As Object Implements System.Collections.ICollection.SyncRoot
	Get
		Return _Columns.SyncRoot
	End Get
End Property
Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
	Return _Columns.GetEnumerator
End Function
Public Function Add(ByVal value As Object) As Integer Implements System.Collections.IList.Add
	If Not (TypeOf value Is PresetColumn) Then Throw New ArgumentException("Invalid Argument")
	Return _Columns.Add(value)
End Function
Public Function Add(ByVal header As String, ByVal weight As String, ByVal compare As Integer) As Integer
	Return _Columns.Add(New PresetColumn(header, weight, compare))
End Function
Public Sub Clear() Implements System.Collections.IList.Clear
	_Columns.Clear()
End Sub
Public Function Contains(ByVal value As Object) As Boolean Implements System.Collections.IList.Contains
	Return _Columns.Contains(value)
End Function
Public Function IndexOf(ByVal value As Object) As Integer Implements System.Collections.IList.IndexOf
	Return _Columns.IndexOf(value)
End Function
Public Sub Insert(ByVal index As Integer, ByVal value As Object) Implements System.Collections.IList.Insert
	If Not (TypeOf value Is PresetColumn) Then Throw New ArgumentException("Invalid Argument")
	_Columns.Insert(index, value)
End Sub
Public ReadOnly Property IsFixedSize() As Boolean Implements System.Collections.IList.IsFixedSize
	Get
		Return False
	End Get
End Property
Public ReadOnly Property IsReadOnly() As Boolean Implements System.Collections.IList.IsReadOnly
	Get
		Return False
	End Get
End Property
Default Public Property Item(ByVal index As Integer) As Object Implements System.Collections.IList.Item
	Get
		Return _Columns(index)
	End Get
	Set(ByVal Value As Object)
		_Columns(index) = Value
	End Set
End Property
Public Sub Remove(ByVal value As Object) Implements System.Collections.IList.Remove
	_Columns.Remove(value)
End Sub
Public Sub RemoveAt(ByVal index As Integer) Implements System.Collections.IList.RemoveAt
	_Columns.RemoveAt(index)
End Sub
Public Function Clone() As Object Implements System.ICloneable.Clone
	Dim NewPreset As Preset = New Preset
	NewPreset.Opt = Me.Opt
	NewPreset.SetupNumber = Me.SetupNumber
	NewPreset.Description = Me.Description
	NewPreset.TotalSessions = Me.TotalSessions
	For Each presetcol As PresetColumn In _Columns
		NewPreset.Add(presetcol.Clone())
	Next
End Function
End Class
<Serializable()> _
Public Class PresetColumns
Inherits ArrayList
Private Sub New()
End Sub
Public Shared ReadOnly Property Empty() As PresetColumns
	Get
		Return New PresetColumns
	End Get
End Property
Public Shared Function GetPresets() As PresetColumns
	Dim result As PresetColumns
	result = Load()
	If result Is Nothing Then result = PresetColumns.Empty
	Return result
End Function
Private Shared Function Load() As PresetColumns
	Dim formatter As BinaryFormatter = New BinaryFormatter
	Dim stream As Stream = New FileStream("Presets.bin", FileMode.OpenOrCreate)
	Try
		If stream.Length = 0 Then Return Nothing
		Return DirectCast(formatter.Deserialize(stream), PresetColumns)
	Finally
		stream.Close()
	End Try
End Function
Public Sub Save()
	Dim formatter As BinaryFormatter = New BinaryFormatter
	Dim stream As Stream = New FileStream("Presets.bin", FileMode.Create)
	Try
		formatter.Serialize(stream, Me)
	Finally
		stream.Close()
	End Try
End Sub
End Class

Edited by Joe Mamma

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted
take a look at this attachment.

Presets.zip

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted (edited)

It occured to me that this is exactly what the XML/xsd is for.

 

Ok, work with me here.

 

  • Create a new VB class library project name it PresetLib and delete the class1 file that is automatically created.
  • In Project Explorer, right click the project and select add -> Add Class. Select Dataset and name it PresetDataSet.xsd.
  • In PresetDataSet.xsd, right click the XML designer workspace and click view XML and replace all the XML with the following:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="PresetDataSet" targetNamespace="[url="http://tempuri.org/PresetDataSet.xsd"]http://tempuri.org/PresetDataSet.xsd[/url]" elementFormDefault="qualified"
attributeFormDefault="qualified" xmlns="[url="http://tempuri.org/PresetDataSet.xsd"]http://tempuri.org/PresetDataSet.xsd[/url]" xmlns:mstns="[url="http://tempuri.org/PresetDataSet.xsd"]http://tempuri.org/PresetDataSet.xsd[/url]"
xmlns:xs="[url="http://www.w3.org/2001/XMLSchema"]http://www.w3.org/2001/XMLSchema[/url]" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="PresetDataSet" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Preset">
 <xs:complexType>
 <xs:sequence>
 <xs:element name="Description" type="xs:string" minOccurs="0" />
 <xs:element name="Setup" type="xs:int" minOccurs="0" />
 <xs:element name="Option" type="xs:int" minOccurs="0" />
 <xs:element name="TotalSessions" type="xs:int" minOccurs="0" />
 <xs:element name="Columns" type="PresetColumns" minOccurs="0" />
 </xs:sequence>
 </xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:complexType name="PresetColumns">
<xs:sequence>
<xs:element name="PresetColumn" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
 <xs:sequence>
 <xs:element name="Header" type="xs:string" />
 <xs:element name="Compare" type="xs:int"></xs:element>
 <xs:element name="Weight" type="xs:string" />
 </xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>

 

  • compile the library and add the library to the toolbox

now are you ready for the application???

  • Ok, add a new Windows Appplication project to the solution and call it TestPresets.
  • Drag a PresetDataset from the toolbox and drop it on your form, call it thePresets.

are you still with me??? Good!

  • Now, need drop two Datagrids on the form and call them presetGrid and presetColumnGrid. also add add four buttons and call them addPreset, deletePreset, addColumn and deleteColumn
  • set the following properties:

presetGrid

CaptionText = "Presets"

AllowNavigation = False

DataSource = thePresets

DataMember = "Preset"

DataSource = thePresets

 

presetColumnGrid

CaptionText = "Preset Columns"

AllowNavigation = False

DataSource thePresets

DataMember = "Preset.Preset_Columns.Columns_PresetColumn"

DataSource thePresets

 

addPreset

Text = "Add Preset"

 

deletePreset

Text = "Delete Preset"

Enabled = false

 

addColumn

Text = "Add Column"

 

deleteColumn

Text = "Delete Column"

Enabled = false

 

now for the code. . .

 

  • Add the following:

Private ReadOnly Property CurrentPreset() As PresetLib.PresetDataSet.PresetRow
	Get
		Dim cm As CurrencyManager = Me.BindingContext(presetGrid.DataSource, presetGrid.DataMember)
		If cm.Count = 0 Then Return Nothing
		Dim dv As DataRowView = cm.Current
		If dv Is Nothing Then Return Nothing
		Return DirectCast(dv.Row, PresetLib.PresetDataSet.PresetRow)
	End Get
End Property
Private ReadOnly Property CurrentPresetColumn() As PresetLib.PresetDataSet.PresetColumnRow
	Get
		Dim cm As CurrencyManager = Me.BindingContext(presetColumnGrid.DataSource, presetColumnGrid.DataMember)
		If cm.Count = 0 Then Return Nothing
		Dim dv As DataRowView = cm.Current
		If dv Is Nothing Then Return Nothing
		Return DirectCast(dv.Row, PresetLib.PresetDataSet.PresetColumnRow)
	End Get
End Property
Private Sub Preset_rowChanged(ByVal sender As Object, ByVal e As PresetLib.PresetDataSet.PresetRowChangeEvent) _
		Handles thePresets.Preset.PresetRowChanged
	If Not e.Action = DataRowAction.Add Then Return
	Dim row As PresetLib.PresetDataSet.PresetRow = _
		DirectCast(e.Row, PresetLib.PresetDataSet.PresetRow)
	thePresets.Columns.AddColumnsRow(row)
End Sub
Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs) _
		Handles addPreset.Click, deletePreset.Click, addColumn.Click, deleteColumn.Click
	If sender Is addPreset Then _
			thePresets.Preset.AddPresetRow("", 0, 0, 0)
	If sender Is addColumn Then _
			thePresets.PresetColumn.AddPresetColumnRow("", 0, "", CurrentPreset.GetChildRows(thePresets.Columns.ParentRelations(0))(0))
	If sender Is deletePreset Then _
			thePresets.Preset.RemovePresetRow(CurrentPreset)
	If sender Is deleteColumn Then _
			thePresets.PresetColumn.RemovePresetColumnRow(CurrentPresetColumn)
	deletePreset.Enabled = thePresets.Preset.Rows.Count > 0
	addColumn.Enabled = deletePreset.Enabled
	deleteColumn.Enabled = Not CurrentPreset Is Nothing
	If Not deleteColumn.Enabled Then Return
	Dim cols() As System.Data.DataRow = CurrentPreset.GetChildRows(thePresets.Columns.ParentRelations(0))
	If cols.Length > 0 Then
		Dim presetCol() As System.Data.DataRow = cols(0).GetChildRows(thePresets.PresetColumn.ParentRelations(0))
		deleteColumn.Enabled = presetCol.Length > 0
	Else
		deleteColumn.Enabled = False
	End If
End Sub
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
	If System.IO.File.Exists("presets.xml") Then thePresets.ReadXml("presets.xml")
	Button_Click(Nothing, Nothing)
End Sub
Protected Overrides Sub OnClosed(ByVal e As System.EventArgs)
	thePresets.AcceptChanges()
	thePresets.WriteXml("presets.xml")
End Sub

 

compile and run!

 

see attached solution

 

Any comments are welcome!!!

PresetLib.zip

Edited by PlausiblyDamp

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

That all looks very interesting; I'm amazed at the effort you put into all that- thanks very much.

 

In the intervening time I played around with classes (classPreset.vb) and got a bit further during the day. That was after reading Classes Lesson 1 on this board.

 

However, this stuff looks much more interesting so I'll experiment with it today. It does look rather daunting although, as you say, some sections are auto-generated.

 

Thanks again for the Hope!

 

Rgds,

David

Posted

Iceplug, I found (but didn't test) that if one sub of a Form is still Private, the Public ones won't be properly available to other Forms. The suggestion I read was to make all the subs Public and any that need privacy should be marked Protected.

 

I'm going to check out Joe Momma's ideas first, though, because they open up new areas of the language (Framework?) to me.

 

Thanks & Rgds,

David

  • Administrators
Posted
Changing the scope of a single method of a form to private wouldn't prevent the public methods from being available. Protected also has a specific meaning and really is only used with inheritance, it shouldn't have any effect on your code.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Ah yes, I forgot, I was looking at inheritance. Did I mention how confused I was ;)? Inheritance seemed to work within my scrappy code but I didn't really understand the implications of it.

 

My head hurts!

 

Rgds,

David

Posted

I couldn't load the zipped solutions nor successfully splice them into new ones so I was relegated to reading through. I think saw where it was going but couldn't make it happen on my VB.NET Enterprise 2002. I'll keep the printouts handy, anyway, for when i have more time.

 

So, because time waits for no man, I'm going back to what I had. It must be made to work :rolleyes: ! It actually works less than it used to. Now, the presets won't load, saying my class is not instantiated. ("Object reference not set to an instance of an object").

 

ClassPresets.vb is a Class description.

Here it is cut down to one variable.

Public Class ClassPreset

   Private _Opt As Integer

   Public Property Opt() As Integer
       Get
           Return _Opt
       End Get
       Set(ByVal Value As Integer)
           _Opt = Value
       End Set
   End Property

End Class

frmMain.vb declaration

Public Class frmMain
   Inherits System.Windows.Forms.Form

'<Windows Form Designer Generated Code> goes here

    Public myPresets() As ClassPreset

frmMain.vb loadPresets

    Public Sub LoadPresets()
       Dim loadBuffer As String
       Dim splitArray() As String
       Dim sr As IO.StreamReader

       sr = IO.File.OpenText("BALOPT.INI")  'ascii csv file
       'same file as used in 10yr old QBASIC "application"

       loadBuffer = sr.ReadLine() '1st line contains number
       '1 line per preset using comma-separated variables
       Dim maxPresets = Val(loadBuffer)

       Dim myPresets(maxPresets + 1) '= nothing 'As ClassPreset
       myPresets(maxPresets + 1) = New ClassPreset()

       'myPresets(maxPresets).Opt = New Integer()
       i = 0 'preset counter

       While sr.Peek <> -1

           i += 1 'I miss out zero on arrays, leaving it empty
           loadBuffer = sr.ReadLine
           splitArray = Split(loadBuffer, ",")

           If splitArray.Length > 0 Then
               'Opt is the special Option-flag variable for use later

               'myPresets(i).Opt = New Integer() 'this didn't work
               myPresets(i).Opt = Val(splitArray(0)) '***ERROR***
           End If

       End While

Why oh why isn't that working?

 

Rgds,

David

Posted

Try this:

 

Public Sub LoadPresets()
       Dim loadBuffer As String
       Dim splitArray() As String
       Dim sr As IO.StreamReader

       sr = IO.File.OpenText("BALOPT.INI")  'ascii csv file
       'same file as used in 10yr old QBASIC "application"

       loadBuffer = sr.ReadLine() '1st line contains number
       '1 line per preset using comma-separated variables
       Dim maxPresets = Val(loadBuffer)

       Dim myPresets(maxPresets + 1) '= nothing 'As ClassPreset
       'remove this as it only instantiates the last element of the array
       'myPresets(maxPresets + 1) = New ClassPreset()

       'myPresets(maxPresets).Opt = New Integer( ) 
       i = 0 'preset counter

       While sr.Peek <> -1

           i += 1 'I miss out zero on arrays, leaving it empty

           'note this added line
           myPresets(i) = New  ClassPreset()

           loadBuffer = sr.ReadLine
           splitArray = Split(loadBuffer, ",")

           If splitArray.Length > 0 Then
               'Opt is the special Option-flag variable for use later

               'myPresets(i).Opt = New Integer() 'this didn't work
               myPresets(i).Opt = Val(splitArray(0)) '***ERROR***
           End If

       End While 

Here's what I'm up to.
Posted

I will back up a little here and describe the impetus for my approach. There are three issues that are in play here.

 

First, you have the issue of 'persistance'.

Second, there is the issue of object model.

Third, is the issue of user interface.

Lastly you have the issue of scope.

 

Your inital post was due to problems you were having with scope. . . scope in my opinion is the easiest to fix. so I didnt even address it.

 

My first reply dealt with issues one and two. .NET makes persistance easy via the Serializable attribute. apply the Serializable attribute and an object is easily saved to and read from a file eliminating your LoadPresets method.

 

My first reply also suggested that what you have is a list of Presets. and each preset has a list of presetcolumn. My inital code is three classes - PresetColumn, Preset, and PresetColumns. Granted, PresetColumns should have been called PresetList.

 

All three classes are marked with the serializable attribute and PresetList has a standard method Save that uses a BinaryFormatter to write itself to disk. It also has static method, Load, which returns a PresetList loaded with the presets as last saved.

 

The problem with this set of classes is it isnt bindable to controls which means a whole slew of methods must be written to manage the list via a GUI. This is where I said 'wait a second, thats what datasets are for!' and decided to implement via an xsd.

 

In closing, unless you are dealing with legacy apps, .NET handles persistance for you. Don't rebuild the wheel. Apply the serializable attribute and use a binary formatter or define typed datasets and use the ReadXML, WriteXML methods. This leaves you to concentrate on getting your object model and functional arch correct (the fun stuff :) ), instead of spinning wheels trying to track down elusive bugs due to a mistake 'walking' your data structure.

 

Hope that helps.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

when I posted my last reply I saw a comment that was missing from your initial post. . .

 

'same file as used in 10yr old QBASIC "application"

My posts would have gone in a differenct direction :)

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

Machaira, thanks, that fix allowed me to load myPresets <phew!>.

 

Still, how do I get access to myPresets elsewhere in the Form and in another Form? I've already sprinkled as many "Public" declarations around the code as I can fit into it :rolleyes: .

 

frmMain's code (the first bit, anyway):-

Public Class frmMain
   Inherits System.Windows.Forms.Form

'Windows Form Designer generated code here  - omitted from this listing

   Public myPresets() As ClassPreset

   Public Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       LoadPresets()
       MsgBox(myPresets(15).Opt) '***NULL REFERENCE ERROR!!!
   End Sub

 

Joe Mamma, I really appreciate your extra clarifying notes, especially the "PresetColumns should have been called PresetList" ;). Until then, I thought you'd slightly misinterpreted my requirements. Also (alas!), I didn't mention it was an upgrade project I was doing because I wasn't expecting the World's Most Complete Answer!

 

Normally, I program leisurely as a sideline in my "Technical IT Officer" role but things are afoot in the organisation (University) and I'm a couple of weeks away from being reorganised to a central department - I want this thing done by then for all sorts of reasons.

 

I have so many creative ideas trapped inside me since the invention of OOP and awful manual-writing styles. I look forward to getting through my mental blocks on this.

 

Thanks for letting me test your patience, folks!

 

Rgds,

David

Posted

Additional

 

I thought I'd show my current class description of presets; it's a lot different from the original, having taken some hints from Joe Mamma's code.

 

ClassPreset.vb (complete file):-

Public Class ClassPreset

   Private _Opt As Integer '= 0
   Private _Description As String = " "   'don't think this assignment is req'd
   Private _SetupNumber As Integer = 0
   Private _TotalColumns As Integer = 0
   Private _TotalSessions As Integer = 0
   Private _ColumnHeadings(702) As String '702 is column ZZ in Excel control
   Private _ColumnWeighSequence(702) As String
   Private _ColumnCompareSequence(702) As Integer

   Public Property Opt() As Integer
       Get
           Return _Opt
       End Get
       Set(ByVal Value As Integer)
           _Opt = Value
       End Set
   End Property

   Public Property Description() As String
       Get
           Return _Description
       End Get
       Set(ByVal Value As String)
           _Description = Value
       End Set
   End Property

   Public Property SetupNumber() As Integer
       Get
           Return _SetupNumber
       End Get
       Set(ByVal Value As Integer)
           _SetupNumber = Value
       End Set
   End Property

   Public Property TotalColumns() As Integer
       Get
           Return _TotalColumns
       End Get
       Set(ByVal Value As Integer)
           _TotalColumns = Value
       End Set
   End Property

   Public Property TotalSessions() As Integer
       Get
           Return _TotalSessions
       End Get
       Set(ByVal Value As Integer)
           _TotalSessions = Value
       End Set
   End Property

   Public Property ColumnHeadings(ByVal myIndex As Integer) As String
       Get
           Return _ColumnHeadings(myIndex)
       End Get
       Set(ByVal Value As String)
           _ColumnHeadings(myIndex) = Value
       End Set
   End Property

   Public Property ColumnWeighSequence(ByVal myIndex As Integer) As String
       Get
           Return _ColumnWeighSequence(myIndex)
       End Get
       Set(ByVal Value As String)
           _ColumnWeighSequence(myIndex) = Value
       End Set
   End Property

   Public Property ColumnCompareSequence(ByVal myIndex As Integer) As String
       Get
           Return _ColumnCompareSequence(myIndex)
       End Get
       Set(ByVal Value As String)
           _ColumnCompareSequence(myIndex) = Value
       End Set
   End Property

End Class

 

Rgds,

David

Posted

I've tried many, many things from the depths of parameter-passing to functions to the heady heights of Namespaces without success.

 

You'll be relieved to hear that I have circumvented the problem.

 

:)

 

Thanks for all the advice and suggestions along the way. I do wish someone had been honest and said it's impossible to share the contents of an instance of a class globally. That the trouble with hope: it can waste a lot of time and effort hehe.

 

Rgds,

David

Posted
I do wish someone had been honest and said it's impossible to share the contents of an instance of a class globally.

Umm, it is possible, unless I'm misunderstanding. That's what modules are for.

Here's what I'm up to.
Posted
I do wish someone had been honest and said it's impossible to share the contents of an instance of a class globally.

but that wouldn't be honest :) . . .

 

Caution - Religious issues ahead!!!! -> Global Class Instance

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

Thanks for the link Joe.

 

Machaira, I couldn't declare it in a module because the # of dimensions was unknown and the amount of the dimensions of its sub-arrays was also unknown. I've been through a lot since I tried it but I think the main problem was trying to get those sub-arrays to work.

 

I'm just glad I'm through it for now.

 

Cheers,

David

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...