|
-
Feb 2nd, 2005, 04:57 AM
#1
Thread Starter
Frenzied Member
No Longer "Quick Question"........Accessing controls on the main form from a CLASS,
Hi guys,
I have a problem .....
In my module,
VB Code:
Imports System.IO
Module mod_Maths_Functions
Public CRSC As CRS_Creator.frmMain
Function DeviationCalculation()
Dim sWriter As StreamWriter
Dim strTemp As String
Dim arrTemp(19) As Double
Dim arrDev As Array
Dim i, j As Integer
'This bit is to create two file in the temp directory for use, then store the name of the file for
'furture ADO.Net Use.
tempFileBaseLoad = CreateTempFile() 'create a new temp file for use....
FileBaseLoadname = tempFileBaseLoad.Substring(tempPathString.Length, tempFileBaseLoad.Length - tempPathString.Length)
tempFileMSG = CreateTempFile() 'create a new temp file for use....
FileMSGname = tempFileMSG.Substring(tempPathString.Length, tempFileMSG.Length - tempPathString.Length)
'Need to delete the old files if they exist
Try
If IO.File.Exists(tempFileBaseLoad) = True Then
File.Delete(tempFileBaseLoad)
End If
If IO.File.Exists(tempFileMSG) = True Then
File.Delete(tempFileMSG)
End If
Catch ex As Exception
End Try
sWriter = IO.File.CreateText(tempFileBaseLoad)
sWriter.WriteLine("Time,GTload,Dev1,Dev2,Dev3,Dev4,Dev5,Dev6,Dev7,Dev8,Dev9,Dev10,Dev11,Dev12,Dev13,Dev14,Dev15,Dev16,Dev17,Dev18")
sWriter.Close()
sWriter = IO.File.CreateText(tempFileMSG)
sWriter.WriteLine("Time,GTload,Dev1,Dev2,Dev3,Dev4,Dev5,Dev6,Dev7,Dev8,Dev9,Dev10,Dev11,Dev12,Dev13,Dev14,Dev15,Dev16,Dev17,Dev18")
sWriter.Close()
Dim progressCounter, progressIncrement As Single
progressIncrement = 100 / dt.Rows.Count
Dim prog As ProgressBar
prog = CRSC.progBar1
For j = 1 To dt.Rows.Count - 1
Application.DoEvents()
For i = 0 To 19
arrTemp(i) = dt.Rows(j)(i)
Next
strTemp = CanDeviation(arrTemp)
If arrTemp(1) >= 200 Then 'checks if it is at baseload
sWriter = IO.File.AppendText(tempFileBaseLoad)
Else
sWriter = IO.File.AppendText(tempFileMSG)
End If
sWriter.WriteLine(strTemp)
sWriter.Close()
progressCounter += progressIncrement
prog.Value = progressCounter
Next
End Function
Function CanDeviation(ByRef arrInput As Array) As String
Dim i As Integer
Dim arrOutput(19) As Single
Dim sngCanTempSum As Single
Dim sngCanAverage As Single
Dim strTemp As String
For i = 2 To 19
sngCanTempSum += arrInput(i)
Next
'Now we have the total can Temp
sngCanAverage = sngCanTempSum / 18
'Divide by 18 to get can average temp
For i = 0 To 1 'Put the Time and GTload back in
arrOutput(i) = arrInput(i)
Next
For i = 2 To 19 'Now get the deviation from mean
arrOutput(i) = ((arrInput(i) - sngCanAverage) / sngCanAverage) * 100
Next
For i = 0 To arrOutput.Length - 1
strTemp &= arrOutput(i) & ","
Next
Return strTemp
End Function
End Module
I have a progressBar which is called progBar1 on the frmMain and i need to access it from my module. However, it throws up the following error,
VB Code:
An unhandled exception of type 'System.NullReferenceException' occurred in CRS Creator.exe
Additional information: Object reference not set to an instance of an object.
Any ideas? I know i can pass the control from the main form when i call up the function ..
Last edited by dinosaur_uk; Feb 2nd, 2005 at 12:01 PM.
-
Feb 2nd, 2005, 05:10 AM
#2
Member
Re: Quick Question
ummm the is a problem i wand help you real but i have 1 script that only is to use you progbar sorry
-
Feb 2nd, 2005, 05:17 AM
#3
Thread Starter
Frenzied Member
Re: Quick Question
No probs buddy
Thanks for the reply anyway
-
Feb 2nd, 2005, 05:19 AM
#4
Member
Re: Quick Question
np
-
Feb 2nd, 2005, 06:12 AM
#5
Re: Quick Question
Hi Dino
You forgot the new on your DeviationCalculation function.
Regards
Jorge
"The dark side clouds everything. Impossible to see the future is."
-
Feb 2nd, 2005, 06:35 AM
#6
Thread Starter
Frenzied Member
Re: Quick Question
Yeha that is on the main form...
i have no problems with progBar ....just with progBar1 coz i am calling it from the module...
-
Feb 2nd, 2005, 07:11 AM
#7
New Member
Re: Quick Question
haven't you forgotten to set CRSC before calling the sub in this module. Anyways wouldn't it be better to move all this into a class instead of a module?
-
Feb 2nd, 2005, 08:46 AM
#8
Thread Starter
Frenzied Member
Re: Quick Question
Why would it be better in a class?
-
Feb 2nd, 2005, 10:15 AM
#9
Re: No Longer "Quick Question".........dragging on now..
if you put it in a class , you can add a reference to your Form ( because it's a null object at present ) , eg:
VB Code:
Public Class mod_Maths_Functions
Public CRSC As CRS_Creator.frmMain
Public Sub New(frm As Form)
CRSC = DirectCast( frm , CRS_Creator.frmMain )
'/// Now CRSC is an Active Reference to CRS_Creator.frmMain
End Sub
'/// Rest of Your Code Here ...
End Class
to access from a Form / Class ...
VB Code:
Dim cls As New mod_Maths_Functions( Me ) '/// Where Me = the name of the Calling Form ( ie: CRS_Creator.frmMain )
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Feb 2nd, 2005, 10:27 AM
#10
Hyperactive Member
Re: No Longer "Quick Question".........dragging on now..
I believe, although not sure if it is considered good coding practice, you can pass the Form through your function and access all of its members (much like a class) through a function/sub in a module.
Code:
Public Sub ClearComboBox(frm As Form1)
frm.ComboBox1.Items.Clear()
End Sub
Not at a machine to test but I am pretty sure it will work.
EDIT: You might need to pass the frm parameter ByRef rather than ByVal for it to work but I am not sure. If the default (ByVal) doesn't work you can always change it to ByRef and try
-
Feb 2nd, 2005, 10:34 AM
#11
Thread Starter
Frenzied Member
Re: No Longer "Quick Question".........dragging on now..
So a module is essentially a classed which is shared, so why does it have to be a class and not a module?
it seems more complicated....
 Originally Posted by dynamic_sysop
if you put it in a class , you can add a reference to your Form ( because it's a null object at present ) , eg:
VB Code:
Public Class mod_Maths_Functions
Public CRSC As CRS_Creator.frmMain
Public Sub New(frm As Form)
CRSC = DirectCast( frm , CRS_Creator.frmMain )
'/// Now CRSC is an Active Reference to CRS_Creator.frmMain
End Sub
'/// Rest of Your Code Here ...
End Class
to access from a Form / Class ...
VB Code:
Dim cls As New mod_Maths_Functions( Me ) '/// Where Me = the name of the Calling Form ( ie: CRS_Creator.frmMain )
-
Feb 2nd, 2005, 10:36 AM
#12
Thread Starter
Frenzied Member
Re: No Longer "Quick Question".........dragging on now..
Why does a class accept a sub but not a module???
-
Feb 2nd, 2005, 10:40 AM
#13
Hyperactive Member
Re: No Longer "Quick Question".........dragging on now..
 Originally Posted by dinosaur_uk
Why does a class accept a sub but not a module???
What do you mean?
-
Feb 2nd, 2005, 11:03 AM
#14
Re: No Longer "Quick Question".........dragging on now..
A module can have a sub just not a Sub New().
modules were put into vb.net to keep the vb6ers happy... as such they may be removed in future versions. Classes are more powerful and you can use them in the same way if you add shared to your property/function/sub/variable
VB.NET is a fully OOP (Object Oriented Programing) language. you should be using OOP properly.
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
-
Feb 2nd, 2005, 11:20 AM
#15
Thread Starter
Frenzied Member
Re: No Longer "Quick Question".........dragging on now..
Cheers guys....
I will start using classes from now onwards....
Cheers
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|