|
-
Mar 20th, 2013, 07:00 AM
#1
Thread Starter
New Member
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
-
Mar 20th, 2013, 07:20 AM
#2
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 -
-
Mar 20th, 2013, 07:33 AM
#3
Thread Starter
New Member
Re: Putitng calculated object into variable
How do i return the value from the function to be used in the variable?
-
Mar 20th, 2013, 07:52 AM
#4
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 -
-
Mar 20th, 2013, 07:59 AM
#5
Thread Starter
New Member
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.
-
Mar 20th, 2013, 10:12 AM
#6
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
-
Apr 4th, 2013, 09:41 AM
#7
Thread Starter
New Member
Re: Putitng calculated object into variable
How to you pass in a datatable without using a dataset?
-
Apr 4th, 2013, 09:47 AM
#8
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
 
-
Apr 4th, 2013, 10:01 AM
#9
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|