473,382 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

trouble with scope in C#

daJunkCollector
Hey,

My code is posted below. I am working on a small webform. I have tinkered around with the 'totalPrice' variable a few different ways now. Obviously, I am new to C#. As is, I receive the following error "An object reference is required for the nonstatic field, method, or property 'polo.totalPrice' " Polo.aspx is the name of the webform page.

To save you time: The error msg refers to line 67 of the below code. Furthermore, I believe that only the following lines should be under scrutiny: 17, 19, 67, 154, 155.

Thank you for your time.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.OleDb;
  12.  
  13. using System.Collections.Generic;
  14.  
  15. public partial class polo : System.Web.UI.Page
  16. {
  17.     string totalPrice;
  18.  
  19.     static string calculatePrice(string quantity, string size)
  20.     {
  21.         const double avgSizePrice = 20.40;
  22.         const double firstLrgSizePrice = 23.90;
  23.         const double secondLrgSizePrice = 27.40;
  24.         const double thirdLrgSizePrice = 30.90;
  25.         string usersQuantity = quantity;
  26.         string usersSize = size;
  27.         double pricePerUnit = 0;
  28.  
  29.         switch (usersSize)
  30.         {
  31.             case "XS":
  32.                 pricePerUnit = avgSizePrice;
  33.                 break;
  34.  
  35.             case "S":
  36.                 pricePerUnit = avgSizePrice;
  37.                 break;
  38.  
  39.             case "M":
  40.                 pricePerUnit = avgSizePrice;
  41.                 break;
  42.  
  43.             case "L":
  44.                 pricePerUnit = avgSizePrice;
  45.                 break;
  46.  
  47.             case "XL":
  48.                 pricePerUnit = avgSizePrice;
  49.                 break;
  50.  
  51.             case "XXL":
  52.                 pricePerUnit = firstLrgSizePrice;
  53.                 break;
  54.  
  55.             case "3XL":
  56.                 pricePerUnit = secondLrgSizePrice;
  57.                 break;
  58.  
  59.             case "4XL":
  60.                 pricePerUnit = thirdLrgSizePrice;
  61.                 break;
  62.         }
  63.  
  64.         double quantityTotal = double.Parse(usersQuantity);
  65.         double basePrice = quantityTotal * pricePerUnit;
  66.  
  67.         totalPrice = basePrice.ToString("#######0.00");
  68.     }
  69.  
  70.  
  71.     public String nameVar
  72.     {
  73.         get{ return nameTextBox.Text; }
  74.     }
  75.     public String branchVar
  76.     {
  77.         get{ return branchDropdown.SelectedValue; }
  78.     }
  79.     public String productVar
  80.     {
  81.         get{ return productTextBox.Text; }
  82.     }
  83.     public String genderVar
  84.     {
  85.         get { return genderDropdown.SelectedValue; }
  86.     }
  87.     public String sizeVar
  88.     {
  89.         get { return sizeDropdown.SelectedValue; }
  90.     }
  91.     public String colorVar
  92.     {
  93.         get { return colorDropdown.SelectedValue; }
  94.     }
  95.     public String quanVar
  96.     {
  97.         get { return quanTextBox.Text; }
  98.     }
  99.  
  100.     private Dictionary<string, ListItem[]> genderData;
  101.  
  102.     protected override void OnInit(EventArgs e)
  103.     {
  104.         base.OnInit(e);
  105.  
  106.         genderData = new Dictionary<string, ListItem[]>();
  107.  
  108.         genderData["Male"] = new ListItem[] {
  109.             new ListItem("XS"),
  110.             new ListItem("S"),
  111.             new ListItem("M"),
  112.             new ListItem("L"),
  113.             new ListItem("XL"),
  114.             new ListItem("XXL"),
  115.             new ListItem("3XL"),
  116.             new ListItem("4XL")
  117.         };
  118.  
  119.         genderData["Female"] = new ListItem[] {
  120.             new ListItem("XS"),
  121.             new ListItem("S"),
  122.             new ListItem("M"),
  123.             new ListItem("L"),
  124.             new ListItem("XL"),
  125.             new ListItem("XXL"),
  126.             new ListItem("3XL")
  127.         };
  128.     }
  129.  
  130.     protected void Page_Load(object sender, EventArgs e)
  131.     {
  132.         if (!IsPostBack)
  133.         {
  134.             genderDropdown.Items.Add("--Please Select--");
  135.             foreach (string gender in genderData.Keys)
  136.             {
  137.                 genderDropdown.Items.Add(gender);
  138.             }
  139.  
  140.             sizeDropdown.Items.Add("--Please Select--");
  141.         }
  142.     }
  143.     protected void genderDropdown_SelectedIndexChanged(object sender, EventArgs e)
  144.     {
  145.         string gender = genderDropdown.SelectedValue;
  146.  
  147.         sizeDropdown.Items.Clear();
  148.         sizeDropdown.Items.Add("--Please Select--");
  149.         sizeDropdown.Items.AddRange(genderData[gender]);
  150.     }
  151.  
  152.     protected void priceRefresh_Click(object sender, EventArgs e)
  153.     {
  154.         calculatePrice(quanTextBox.Text, sizeDropdown.SelectedValue);
  155.         price.Text = totalPrice;
  156.     }
  157.     protected void submitBtn_Click(object sender, EventArgs e)
  158.     {
  159.         if (Page.IsValid == true)
  160.         {
  161.             Server.Transfer("Confirmation_Polo.aspx");
  162.         }
  163.  
  164.     }
  165. }
Sep 10 '07 #1
3 965
Holy Hannah! I figured it out.......

Line 19: public string calculatePrice(string quantity, string size)

after line 67 I added:

return totalPrice;





BOOOOYAKASHA!
Sep 10 '07 #2
"totalprice" is declared a (by default) non-static string, but the only place in the code you posted where the value is assigned is inside a method declared to be static. I suggest a review of the concept of static fields (e.g. http://msdn2.microsoft.com/en-us/library/98f28cdx(VS.71).aspx) .

Your choice is to delcare totalprice static, the method non-static or create a instance of polo and then call that object's totalprice property.
Sep 10 '07 #3
Hrm...Thanks Dan. I will review that site you posted. Furthermore, I will try to make both the var and the method static and see if it still works. I believe I would prefer them static.

Thanks
Sep 14 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
9
by: Darabos Daniel | last post by:
Hi! I was doing something like this: >>> def p( x ): .... print x .... >>> l = >>> for i in range( 5 ): .... l.append( lambda: p( i ) )
1
by: Frans Englich | last post by:
Hello all, I have a couple of questions related to module importing. 1) When I start my Python program with `python foo.py` instead of simply adding a interpreter comment on the first line and...
6
by: stever | last post by:
Hi- I'm setting a session var in global ASA like this: Session("Scope") = GetScopeList( Session("docRootPath") ) Where GetScopeList is a function in global.asa that will return a string array...
4
by: Jacek Dziedzic | last post by:
Hi! First of all, I hope my problem is not too loosely tied to the "standard C++" that is the topic of this group. I have some code that exhibits a strange behaviour: on one computer, where I...
4
by: zmcelrath87 | last post by:
I am having a problem involving the scope of timeouts and intervals. Since timeouts and intervals execute in the global scope, dynamically generated local interval/timeout declarations do not work,...
1
by: Carl Griffin | last post by:
I am working an an application to enumerate users and permissions they have to read certain fax queues on a fax server. The problem I am having is that I can't figure out a way to let the admin...
6
by: idar.douglas.hillgaar | last post by:
Hi - the following code compiled easily in VS.Net however using g++'s there are several errors relating to template (I'm using a list()) - using the latest ver. of g++ Code: list <intmy_list2;...
7
by: petermichaux | last post by:
Hi, I have tried the following based on suggestions of the best way to insert JavaScript into a page. This is instead of using eval(). Unfortunately IE says "unexpected call to property or...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.