Results 1 to 15 of 15

Thread: No Longer "Quick Question"........Accessing controls on the main form from a CLASS,

  1. #1

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Resolved No Longer "Quick Question"........Accessing controls on the main form from a CLASS,

    Hi guys,

    I have a problem .....

    In my module,
    VB Code:
    1. Imports System.IO
    2.  
    3. Module mod_Maths_Functions
    4.     Public CRSC As CRS_Creator.frmMain
    5.  
    6.     Function DeviationCalculation()
    7.         Dim sWriter As StreamWriter
    8.         Dim strTemp As String
    9.         Dim arrTemp(19) As Double
    10.         Dim arrDev As Array
    11.         Dim i, j As Integer
    12.  
    13.         'This bit is to create two file in the temp directory for use, then store the name of the file for
    14.         'furture ADO.Net Use.
    15.         tempFileBaseLoad = CreateTempFile()  'create a new temp file for use....
    16.         FileBaseLoadname = tempFileBaseLoad.Substring(tempPathString.Length, tempFileBaseLoad.Length - tempPathString.Length)
    17.         tempFileMSG = CreateTempFile() 'create a new temp file for use....
    18.         FileMSGname = tempFileMSG.Substring(tempPathString.Length, tempFileMSG.Length - tempPathString.Length)
    19.  
    20.  
    21.         'Need to delete the old files if they exist
    22.         Try
    23.             If IO.File.Exists(tempFileBaseLoad) = True Then
    24.                 File.Delete(tempFileBaseLoad)
    25.             End If
    26.             If IO.File.Exists(tempFileMSG) = True Then
    27.                 File.Delete(tempFileMSG)
    28.             End If
    29.         Catch ex As Exception
    30.         End Try
    31.         sWriter = IO.File.CreateText(tempFileBaseLoad)
    32.         sWriter.WriteLine("Time,GTload,Dev1,Dev2,Dev3,Dev4,Dev5,Dev6,Dev7,Dev8,Dev9,Dev10,Dev11,Dev12,Dev13,Dev14,Dev15,Dev16,Dev17,Dev18")
    33.         sWriter.Close()
    34.         sWriter = IO.File.CreateText(tempFileMSG)
    35.         sWriter.WriteLine("Time,GTload,Dev1,Dev2,Dev3,Dev4,Dev5,Dev6,Dev7,Dev8,Dev9,Dev10,Dev11,Dev12,Dev13,Dev14,Dev15,Dev16,Dev17,Dev18")
    36.         sWriter.Close()
    37.         Dim progressCounter, progressIncrement As Single
    38.         progressIncrement = 100 / dt.Rows.Count
    39.         Dim prog As ProgressBar
    40.         prog = CRSC.progBar1
    41.  
    42.  
    43.         For j = 1 To dt.Rows.Count - 1
    44.             Application.DoEvents()
    45.             For i = 0 To 19
    46.                 arrTemp(i) = dt.Rows(j)(i)
    47.             Next
    48.             strTemp = CanDeviation(arrTemp)
    49.             If arrTemp(1) >= 200 Then    'checks if it is at baseload
    50.                 sWriter = IO.File.AppendText(tempFileBaseLoad)
    51.             Else
    52.                 sWriter = IO.File.AppendText(tempFileMSG)
    53.             End If
    54.             sWriter.WriteLine(strTemp)
    55.             sWriter.Close()
    56.             progressCounter += progressIncrement
    57.             prog.Value = progressCounter
    58.         Next
    59.  
    60.     End Function
    61.     Function CanDeviation(ByRef arrInput As Array) As String
    62.         Dim i As Integer
    63.         Dim arrOutput(19) As Single
    64.         Dim sngCanTempSum As Single
    65.         Dim sngCanAverage As Single
    66.         Dim strTemp As String
    67.  
    68.  
    69.         For i = 2 To 19
    70.             sngCanTempSum += arrInput(i)
    71.         Next
    72.  
    73.         'Now we have the total can Temp
    74.         sngCanAverage = sngCanTempSum / 18
    75.         'Divide by 18 to get can average temp
    76.  
    77.         For i = 0 To 1  'Put the Time and GTload back in
    78.             arrOutput(i) = arrInput(i)
    79.         Next
    80.  
    81.         For i = 2 To 19 'Now get the deviation from mean
    82.             arrOutput(i) = ((arrInput(i) - sngCanAverage) / sngCanAverage) * 100
    83.         Next
    84.  
    85.         For i = 0 To arrOutput.Length - 1
    86.             strTemp &= arrOutput(i) & ","
    87.         Next
    88.         Return strTemp
    89.     End Function
    90. 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:
    1. An unhandled exception of type 'System.NullReferenceException' occurred in CRS Creator.exe
    2.  
    3. 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.

  2. #2
    Member
    Join Date
    Jan 2005
    Location
    in the nederlands
    Posts
    37

    Thumbs up 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
    what is xml ???

    help my please on this link for xml
    http://www.vbforums.com/showthread.php?t=322815



  3. #3

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Quick Question

    No probs buddy

    Thanks for the reply anyway

  4. #4
    Member
    Join Date
    Jan 2005
    Location
    in the nederlands
    Posts
    37

    Talking Re: Quick Question

    np
    what is xml ???

    help my please on this link for xml
    http://www.vbforums.com/showthread.php?t=322815



  5. #5
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Quick Question

    Hi Dino
    You forgot the new on your DeviationCalculation function.
    VB Code:
    1. Dim prog As ProgressBar


    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  6. #6

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    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...


  7. #7
    New Member
    Join Date
    Jan 2005
    Posts
    7

    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?

  8. #8

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Quick Question

    Why would it be better in a class?

  9. #9
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    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:
    1. Public Class mod_Maths_Functions
    2.  
    3.     Public CRSC As CRS_Creator.frmMain
    4.    
    5.     Public Sub New(frm As Form)
    6.         CRSC = DirectCast( frm , CRS_Creator.frmMain )
    7.         '/// Now CRSC is an Active Reference to CRS_Creator.frmMain
    8.     End Sub
    9.  
    10. '/// Rest of Your Code Here ...
    11.  
    12. End Class
    to access from a Form / Class ...
    VB Code:
    1. 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]

  10. #10
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    midewest u.s.
    Posts
    275

    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

  11. #11

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    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....


    Quote 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:
    1. Public Class mod_Maths_Functions
    2.  
    3.     Public CRSC As CRS_Creator.frmMain
    4.    
    5.     Public Sub New(frm As Form)
    6.         CRSC = DirectCast( frm , CRS_Creator.frmMain )
    7.         '/// Now CRSC is an Active Reference to CRS_Creator.frmMain
    8.     End Sub
    9.  
    10. '/// Rest of Your Code Here ...
    11.  
    12. End Class
    to access from a Form / Class ...
    VB Code:
    1. Dim cls As New mod_Maths_Functions( Me ) '/// Where Me = the name of the Calling Form ( ie: CRS_Creator.frmMain )

  12. #12

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: No Longer "Quick Question".........dragging on now..

    Why does a class accept a sub but not a module???

  13. #13
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    midewest u.s.
    Posts
    275

    Re: No Longer "Quick Question".........dragging on now..

    Quote Originally Posted by dinosaur_uk
    Why does a class accept a sub but not a module???

    What do you mean?

  14. #14
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    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

  15. #15

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    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
  •  



Click Here to Expand Forum to Full Width