Results 1 to 8 of 8

Thread: Use dataset in module public read only error

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    92

    Use dataset in module public read only error

    I have a VB.Net Windows forms application with a Dataset call LaserMaintLogDataSet. In the dataset there is a table called "Printers". I am trying to move some of the common validation routines into a Module. The module is below. The problem is I cant access the Dataset form this module. The error on dr = LaserMaintLogDataSet.Printers.FindByPrinterID(PrinterId) is "Reference to non-shared member requires object reference"

    Code:
           Public Function Max_Images_Check(ByVal PrinterId As String, BegMeter As Int32, EndMeter As Int32,
                                          Login As DateTime, Logout As DateTime) As Boolean
    
            Dim TotalImages As Int32
            Dim decHours As Decimal
            Dim ImagesHour As Decimal
            Dim MaxImages As Integer
    
            Dim dr As LaserMaintLogDataSet.PrintersRow
            dr = LaserMaintLogDataSet.Printers.FindByPrinterID(PrinterId)
          MaxImages = dr.MaxImages
    
            If IsNumeric(BegMeter) And IsNumeric(EndMeter) Then
                If Int(EndMeter) > 0 Then
                    TotalImages = CInt(EndMeter) - CInt(BegMeter)
                Else
                    TotalImages = 0
                End If
            End If
    
            Dim Total_Hours As TimeSpan
            If IsDate(Login) And IsDate(Logout) Then
                Total_Hours = Logout.Subtract(Login)
            Else
                Total_Hours = TimeSpan.Zero
            End If
    
            If TotalImages > 0 And Total_Hours > TimeSpan.Zero Then
                decHours = Total_Hours.Hours + Total_Hours.Minutes / 60
                ImagesHour = TotalImages / decHours
                If ImagesHour > 10000 Then
                    If (MsgBox("Total images is above the shift normal! " & vbCrLf &
                        "    Please check the meter readings.    " & vbCrLf &
                        "  Press OK to keep Cancel to revise!    ",
                        MsgBoxStyle.OkCancel, "Confirmation") = MsgBoxResult.Cancel) Then
                        Return False
                    End If
                End If
            End If
    
            Return True
    
        End Function

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Use dataset in module public read only error

    Your completely ignoring the OO part of OOP. Could you tell me the number of the first page that starts with the word "the" in book? Of course you couldn't, because it doesn't make sense. Book isn't a thing, it is a type of thing. I would have to tell you exactly which book I meant for you to be able do as I asked. Get the idea? LaserMaintLogDataSet is a type, not an object. In order to access instance members, you need an instance of that type, i.e. a LaserMaintLogDataSet object.

    Of course, if you respected OOP even more and, instead of creating a separate module, you extended the LaserMaintLogDataSet class and put your code inside it, you'd then be able to use Me to refer to the current instance. If you insist on using a module though, you'll need to pass an instance in and reference that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Use dataset in module public read only error

    By the way, why on Earth are you doing this:
    vb.net Code:
    1. If IsNumeric(BegMeter) And IsNumeric(EndMeter) Then
    when those two parameters are declared as type Int32? How could they possibly not be numeric when they're declared as a numeric type? The same goes here:
    vb.net Code:
    1. If IsDate(Login) And IsDate(Logout) Then
    Why do you need to check whether a DateTime is a date?

    Even if you did need to do validation, Don't use IsNumeric or IsDate anyway. Int32 and DateTime both have TryParse methods that will validate and convert. The original TryParse method, i.e. Double.TryParse, was specifically written because the original implementation of IsNumeric was so slow. If you call IsNumeric then you're calling a TryParse method anyway, so just call the right one yourself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    92

    Re: Use dataset in module public read only error

    jmcilhinney, Thanks for your response. I am not sure I understand your first response. The code works in the form as is. Let me explain, I have two data entry forms Shift_Log and Admin_Shift_log. They have a lot of common code. I was hoping to move all the duplicated code to a common module so I am not duplicating work every time I make a change. Your second response is correct. I need to clean up this code. It makes more sense in the form as the begin and end meter controls are text boxes that can be blank or zero or they could fat finger a alfa character. These users are not data entry people this is probably the most redundant part of their job. So can you suggest a better way or point me to a document that will help me achieve my goal. Thank you.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Use dataset in module public read only error

    Quote Originally Posted by wjburke2 View Post
    I am not sure I understand your first response. The code works in the form as is. Let me explain, I have two data entry forms Shift_Log and Admin_Shift_log. They have a lot of common code. I was hoping to move all the duplicated code to a common module so I am not duplicating work every time I make a change.
    You say that you have TextBoxes for data entry. Let's say that you wanted to move the code that relates to those into a module. Would you write code like this:
    vb.net Code:
    1. Dim data As String = TextBox.Text
    Of course you wouldn't, because that would make no sense. You can't the specific text you want from the TextBox type. You have to get it from the specific TextBox object that contains it. Why should DataSets be any different? You can't get a specific Printers table from the LaserMaintLogDataSet type. You have to get it from the specific LaserMaintLogDataSet object that contains it. You have created multiple such objects in multiple forms but you're expecting your module to magically know which one of those objects to get data from. That's not happening. If the module has to work on a specific LaserMaintLogDataSet then the form that contains that object is going to have to pass it in. You already know how to pass data into the module because your already doing it. Every one of those parameters is data passed in from the form that calls the method.
    Quote Originally Posted by wjburke2 View Post
    Your second response is correct. I need to clean up this code. It makes more sense in the form as the begin and end meter controls are text boxes that can be blank or zero or they could fat finger a alfa character. These users are not data entry people this is probably the most redundant part of their job. So can you suggest a better way or point me to a document that will help me achieve my goal.
    As I have already said, if the user is inputting data that will be used as Integers into a TextBox then you can use Integer.TryParse to both validate and convert the data in one go. If you choose to do your validation on the Validating event then you can use Integer.TryParse for validation and Integer.Parse for conversion later, safe in the knowledge that no exception will be thrown because the data is guaranteed to be valid.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    92

    Re: Use dataset in module public read only error

    I get it, sorry I was so dense. So you are saying I cant access a instance of a dataset created in a form from a module. I would tell you why I was thinking I could but now its sounds ridiculous. I have to pass a copy of the DS to the module. I did some research on passing datasets. My though was I would pass it by ref. The first article I read of course said they never pass by ref. Do you have a outline pseudo code of what your solution would look like? Perhaps a example you could point me too. Thank you for your time and patience with me.

  7. #7
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: Use dataset in module public read only error

    I believe that datasets are reference type objects. So just pass the dataset to the sub/function.

    Another solution here would be to just create a PUBLIC dataset in the module. If your goal is to use this one dataset throughout the whole program then that makes life easier. That said, it's usually not a good idea just to make a bunch of Global variables/objects.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Use dataset in module public read only error

    You're trying to make this complicated when it's not. There's nothing special about a DataSet. An object is an object. You already know how to call a method and pass an object as an argument because you're already doing it. The method you have already has parameters of type String, Int32 and DateTime and you're having no trouble passing those values as arguments when you call the method so why is this a problem? Just add a parameter of type DataSet (or your specific typed DataSet if you have one) and pass the DataSet as a parameter when you call the method. There's no reason to declare the parameter ByRef so don't. For reference types (classes), the only reason to declare a parameter ByRef is if you want to pass a different object out of the method than you pass in. Are you doing that? No, so why are you even considering ByRef?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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