Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 9th, 2008, 08:50 AM
Authorized User
 
Join Date: Apr 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reference errors in classes and methods

Okay...I'm trying to figure out what I'm doing wrong with a method within a class that I wrote. I think it's just the logistics of it that I'm missing.
I have a program that's supposed to add some numbers the user enters in text boxes using a class and a method. I defined a class with a bunch of properties for the variables. And within the class there's a method that's supposed to do the calculations based on what the user enters and print the answer out. But when I run the program I get reference errors. (can't get to the exact error at the moment but if it will help I'll post it later)

I think I just have something out of order. I guess the first question is where am I supposed to declare the instance of the class so that I can pass all the variables from the textboxes to the class and hence the method? I've seen examples of calling a class with just one variable, but not with multiple variables so I'm not sure if that's the problem.

 
Old May 9th, 2008, 09:12 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

It's always helpful to post exact error messages.

You need to create an instance wherever you plan to use the class. This could be in the button click handler of your web form or elsewhere. That's up to you.

-Peter
compiledthoughts.com
 
Old May 9th, 2008, 06:34 PM
Authorized User
 
Join Date: Apr 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay I messed with it more and got rid of the reference error but now it says my class type isn't declared. I'm pasting my code below...I might have confused myself but I think I've got all the parts...it may just need to be rearranged?

<%@ Page Language="VB" %>
<script runat="server">

    Public Class AddingMachine
             Public Sub New()
             End Sub

             Public _FirstNumber as Integer
             Public _SecondNumber as Integer
             Public _ThirdNumber as Integer
             Public _FinalValue as Integer

             Public Property FirstNumber as Integer
                 Get
                     Return _FirstNumber
                 End Get
             Set (ByVal Value as Integer)
                 _FirstNumber = Value
                 End Set
             End Property

             Public Property SecondNumber as Integer
                 Get
                     Return _SecondNumber
                 End Get
             Set (ByVal Value as Integer)
                 _SecondNumber = Value
                 End Set
             End Property

             Public Property ThirdNumber as Integer
                 Get
                     Return _ThirdNumber
                 End Get
             Set (ByVal Value as Integer)
                 _ThirdNumber = Value
                 End Set
             End Property

             Public Property FinalValue as Integer
                 Get
                     Return _FinalValue
                 End Get
             Set (ByVal Value as Integer)
                 _FinalValue = Value
                 End Set
             End Property


         sub AddNumbers()
    Dim MyNumbers As New AddingMachine ()
           MyNumbers.FinalValue = MyNumbers.FirstNumber+MyNumbers.SecondNumber+MyNum bers.ThirdNumber

         End Sub
    End Class
         sub Page_Load (sender As Object, e As EventArgs)

           MyNumbers.FirstNumber = CInt(txtFirst_Number.text)
           MyNumbers.SecondNumber = CInt(txtSecond_Number.text)
           MyNumbers.ThirdNumber = CInt(txtThird_Number.text)


        Response.Write ("Your total is: " & MyNumbers.FinalValue)
         End Sub

</script>
<html>
<head>
</head>
<body>
    <h3>Adding Machine
    </h3>
    <form runat="server">
        Please enter your first number:
        <asp:TextBox id="txtFirst_Number" runat="server" width="80px"></asp:TextBox>
        <br />
        Please enter your second number:
        <asp:TextBox id="txtSecond_Number" runat="server" width="80px"></asp:TextBox>
        <br />
        Please enter your third number:
        <asp:TextBox id="txtThird_Number" runat="server" width="80px"></asp:TextBox>
        <br />
        <asp:button id="Button1" onclick="AddingMachine" runat="server" text="Submit"></asp:button>


        <br />
        <br />
    </form>
</body>
</html>


 
Old May 10th, 2008, 08:15 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I see your problem now.

Generally, in order to use a class you need to create an instance of one, and you don't create an instance of a class from within itself. There are times when you do both of these things, but that's a more advanced discussion.

In your page load, you are attempting to assign values to the properties of your class. However you do not yet have an instance of that class. However, you are creating an instance in the AddNumbers() method. So you have it backwards. Move the instance declaration to beginning of the page load method and you will be all set.

-Peter
compiledthoughts.com
 
Old May 11th, 2008, 07:54 PM
Authorized User
 
Join Date: Apr 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay...I moved the instance declaration to the beginning of the Page_Load sub and out of the AddNumbers method. However, it still gave me an error for the line:
MyNumbers.FinalValue = MyNumbers.FirstNumber+MyNumbers.SecondNumber+MyNum bers.ThirdNumber
saying that MyNumbers is not defined...which I understand because it's living in Page_Load now.
The problem is the way the task was given is that there has to be a subroutine within the class that performs all the calculations, which I think goes back to the more complicated problem you were talking about. The book confused the heck out of me when it was talking about having an instance of the class within itself.

 
Old May 11th, 2008, 08:54 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Given that this sounds like an assignment I'll hesitate to give away an exact code solution. Perhaps this will help:

Here's an simple example of using object oriented code:

- Create an instance of a class
- Assign value(s) to the instance of the class
- Call method(s) on the instance of the class

This is an example of working with a class instance.

You can do this outside of the class or inside of the class, it doesn't matter. A class can create an instance of itself, and barring any infinitely recursive calls there are not problems. (If an instance method creates another instance and calls the same method, you'd never return from the method call, it would recurse until you ran out of memory).

If you want to use methods on a class without having an instance, then it is said that you are working with the class type instead of a class instance. A public class member that is modified with the 'Shared' keyword in VB.NET is accessible on the class type without requiring an instance. Perhaps this is what is being asked of you. If you do this however, you have to pass in the values that will eventually be assigned to the class properties as method arguments because the outer consuming code (i.e. the page) doesn't work with an instance of the class.

This seems like a complicated assignment given that is sounds like you don't yet have the basics of object oriented programming down. I recall my first OO class in school and I'm sure I would not have had any idea what I just described (type versus instance use) at that point in my programming education. I hope for your sake that is not what is being asked of you.

-Peter
compiledthoughts.com
 
Old May 11th, 2008, 09:48 PM
Authorized User
 
Join Date: Apr 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yeah I'm not sure I'm too thrilled with this class (the class I'm taking, not the class I wrote...well I'm not thrilled with that either obviously). They said no prior object oriented programming experience was required for this course and I'm not so sure that an introductory object oriented programming class wouldn't have been a good idea. Thank god it's just for on the job training and not for a degree. :D Nevertheless it's been a huge headache.
I'll try poking at it again with what you gave me...maybe I need to set it up as shared...that might be the problem.
Thankfully the next assignment is hooking ASP up to a database which is what I actually have some self-taught experience doing.

 
Old May 12th, 2008, 07:06 PM
Authorized User
 
Join Date: Apr 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, I need some help understanding how the Shared thing works because the explanation in the Wrox book just isn't helping me with this. I changed the subroutine to this:

public Shared sub AddNumbers(sender As Object, e As EventArgs)
 Dim FinalValue as Integer
 Dim MyNumbers As New AddingMachine ()
 MyNumbers.FirstNumber = CInt(txtFirst_Number.text)
 MyNumbers.SecondNumber = CInt(txtSecond_Number.text)
 MyNumbers.ThirdNumber = CInt(txtThird_Number.text)

 FinalValue = MyNumbers.FirstNumber+MyNumbers.SecondNumber+MyNum bers.ThirdNumber
               lblResult.text = "Your final value is " & FinalValue & ".<br />"

 End Sub
End Class

And I've got the button doing this:
<asp:button id="Button1" onclick="AddingMachine.AddNumbers()" runat="server" text="Submit"></asp:button>

I tried to Google for references on how to use Shared properly with no luck so I just started moving things around including how I was passing things with the button and I still keep getting: Reference to a non-shared member requires an object reference.


 
Old May 12th, 2008, 08:49 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I'm also not impressed by the class you are taking either. I'm getting the impression they are sending you down the road without any kind of map.

A very important area you need to understand in object oriented programming is "scope". "Scope" is the visibility of members or variables from the point of perspective of the executing code.

The keywords Public, Private, Protected and Friend (possibly others - my VB is rusty) modify the scope of class members. They can apply to every member (methods, fields/variables, properties, etc).

Variables defined in methods are always local scope - visible only within the context of a method.

When you create a shared member, the code of that member can not see ANY of the class' non-shared members without creating an instance of the class.


In your most recent code, you are trying to reference the textboxes that are defined as members of the class for the ASPX page. This doesn't work. The page controls are (by default) only visible from within the page class itself, as it should be. Also, you have changed the AddNumbers method signature to look like a button event handler. This is not necessary. Instead, you need to call the AddNumbers method from the button click handler, passing the values from the text boxes into it.

Based on what you have posted here I'll put together how I think this should be done. As I mentioned before, I usually try to avoid given explicit solutions to assignments, but I'd hate to see you spend more time on something that should be a trivial task. I just implore you to study it and understand how it works.

First we have the class' add method:
Code:
Public Class AddingMachine
  ...
  Sub AddNumbers()
    _FinalValue = _FirstNumber + _SecondNumber + _ThirdNumber
  End Sub
End Class
See how the method uses the internal variables of the class?

Then we have the button:
Code:
  <asp:button id="Button1" onclick="Button1_onClick" runat="server" text="Submit"></asp:button>
Next, the button handler which creates the class, assigns values and calls the method:
Code:
  Sub Button1_onClick(sender As Object, e As EventArgs)
    Dim MyNumbers As New AddingMachine()

    MyNumbers.FirstNumber = CInt(txtFirst_Number.text)
    MyNumbers.SecondNumber = CInt(txtSecond_Number.text)
    MyNumbers.ThirdNumber = CInt(txtThird_Number.text)

    MyNumbers.AddNumbers()

    Response.Write ("Your total is: " & MyNumbers.FinalValue)
  End Sub


Now, if you want to use a shared method the scenario is a bit different. You need to pass in everything you need and get back what you want because you don't have an instance of the class to work with. Here's what changes:

First the class method:
Code:
Public Class AddingMachine
  ...
  Shared Function AddNumbers(firstNum As Int, secondNum as Int, thirdNum As Int) As Int
    Dim MyNumbers As New AddingMachine()

    MyNumbers.FirstNumber = firstNum 
    MyNumbers.SecondNumber = secondNum
    MyNumbers.ThirdNumber = thirdNum

    MyNumbers.FinalValue = MyNumbers.FirstNumber + MyNumbers.SecondNumber + MyNumbers.ThirdNumber

    Return MyNumbers.FinalValue
  End Function
End Class
Then the button click handler:
Code:
  Sub Button1_onClick(sender As Object, e As EventArgs)
    'using local vars for forum readability
    'I'm intentionally using different var names to
    'eliminate the confusion between the variables here
    'and those in the AddNumbers method
    Dim number1, number2, number3, answer As Int

    number1 = CInt(txtFirst_Number.text)
    number2 = CInt(txtSecond_Number.text)
    number3 = CInt(txtThird_Number.text)

    answer = MyNumbers.AddNumbers(number1, number2, number3)

    'You need to call ToString() on the result or the string
    'concatenation (&) will fail
    Response.Write ("Your total is: " & answer.ToString())
  End Sub
See the differences?

I would redo the add numbers method to greatly simplify it. The above is rather excessive:
Code:
Public Class AddingMachine
  ...
  Shared Function AddNumbers(firstNum As Int, secondNum as Int, thirdNum As Int) As Int
    Return firstNum + secondNum + thirdNum
  End Function
End Class
Hopefully this will help you understand the different ways to implement class methods and get you on the right track.

-Peter
compiledthoughts.com
 
Old May 13th, 2008, 10:40 AM
Authorized User
 
Join Date: Apr 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's a lot easier when you explain it that way...of course I also generally learn better when I can see a concrete example of something and pick it apart to figure out what it does and why. I did email the instructor last night to strongly suggest that an intro to object oriented programming be a pre-requisite for this course, because saying "No prior programming experience required" was obviously grossly misleading.

On a far more positive note, I got the exercise where I bind to a database to run without any hiccups...of course there were actually some fairly extensive class notes on that section as opposed to the rest of the course, so hopefully this will be the last my ASP woes for awhile.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Helper Methods & Wrapper Classes? crazy ASP.NET 2.0 Basics 0 February 9th, 2007 12:31 PM
Classes and Methods lists? zoltac007 ASP.NET 2.0 Basics 0 May 25th, 2006 02:28 PM
methods and properties reference rmoguel Crystal Reports 0 January 31st, 2006 07:38 PM
Can't get errors to display with <html:errors> michaeldill JSP Basics 0 August 2nd, 2004 01:47 PM
Errors Errors DB Errors Ljhopkins VS.NET 2002/2003 0 July 15th, 2003 12:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.