Results 1 to 9 of 9

Thread: Putitng calculated object into variable

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    7

    Putitng calculated object into variable

    Hi, im trying to get the "aObject" into the"aVar" variable so that it can then be put into a label but I am unsure of how to do this. I cant get the data from the "calculationSub" into the "aVar" variable. I'm pretty new to vb, can anyone help?




    Imports System.Data.SqlClient
    Imports System.Data

    Partial Class CIT_Project_pages_Default
    Inherits System.Web.UI.Page

    'Declaring variable
    Dim aVar As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    aLabel.Text = aVar

    End Sub

    Private Sub calculationSub(ByVal dataSet As DataSet)

    'Create datatable from module
    Dim aTable As DataTable
    aTable = dataSet.Tables("module")


    'Computing the average of the column "grades" from the datatable
    Dim aObject As Object
    aObject = aTable.Compute("Avg(grades)", "module_ID = 2031")

    'changing the aObject into a string so that it can be displayed within a label
    aVar = CType(minObject, String)


    End Sub


    End Class

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Putitng calculated object into variable

    Well, you change the sub to a function then you call the function and assign the returned value to your variable.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    7

    Re: Putitng calculated object into variable

    How do i return the value from the function to be used in the variable?

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Putitng calculated object into variable

    Here's an example
    Code:
    Private Function GetSum(byval num1 as double, byval num2 as double) as double
       Dim result as Double = num1 + num2
       Return result
    End Function
    
    'Usage:
    Label1.Text = GetSum(123.5, 345.7).ToString()
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    7

    Re: Putitng calculated object into variable

    The function needs to automatically calculate the values based on data from the database therefore I'm not supposed to enter absolute values to be calculated. I need to find the average, maximum and minimum grades of students on a module based on the data already contained in the database. I only want to put the calculated data into a label but I can't seem to do it.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Putitng calculated object into variable

    Doesn't matter what the sub does or doesn't do, if you never call it. Also, VB is NOT case sensitive, so dataSet and DataSet are the same thing... so try to avoid type names as variable names as it will only confuse things. c) it's pointless to pass in a dataset if all you are going to use is jsut one table... just pass in the dataTable to begin with. 4) if you want something converted to a string, use the .ToString method instead... #5) aVar should really be typed to the same data type that the .Compute method returns.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    7

    Re: Putitng calculated object into variable

    How to you pass in a datatable without using a dataset?

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Putitng calculated object into variable

    Just use datatable in place of the dataset. You created that method, you created the arguments to it, so you are the reason it takes a dataset. Just change that to datatable.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    7

    Re: Putitng calculated object into variable

    Sorry I dont quite know what you mean, I'm terrible at vb, could you give me an example of how it works?

    I have been trying to use something like this now

    Dim gradeSet As New DataSet
    'Creating datatable that can be used to compute average, max and min grades
    Dim gradeTable As DataTable
    gradeTable = DataSet.Tables("completed_module")

    (all of this and the calculations are now just inside the page load sub and i have taken away the calculation sub altogether)

    I just want the datatable to be populated with the data from "completed_module" table from the datbase or if there is any way to just reference the completed module table directly, thanks.

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