473,385 Members | 1,564 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,385 software developers and data experts.

asp.net, C#, constructor in new class

I am learning C# and asp.net and am trying to create a new utility
class.

The default code you get from vwd express is:

/// <summary>
/// Summary description for ddlCode
/// </summary>
public class someClass
{
public someClass()
{
//
// TODO: Add constructor logic here
//

}
}

If I have a method, for example txtBoxSubmit, that assigns a variable
(string txtData) from txtBox.text, where do I put that code in the
above scaffold? Can it have an empty constructor?

Thanks!

May 12 '06 #1
5 3292
You may have a constructor which accepts a string as parameter to make it
reusable on other places (compatible for exposing read-only property of the
string so others can't change it once the object is created), or have a
empty constructor and a property that let you change that value. It's
basically a choice of designing how you want to make it work.

Empty constructor is legal of course, just meaning this class won't do any
custom initialization when it's created.

And I don't prefer to have control names explicitly coded into classes other
than the webform itself, to preserve reusability and code cleaness.

"Ranginald" <da*******@gmail.com>
???????:11**********************@i39g2000cwa.googl egroups.com...
I am learning C# and asp.net and am trying to create a new utility
class.

The default code you get from vwd express is:

/// <summary>
/// Summary description for ddlCode
/// </summary>
public class someClass
{
public someClass()
{
//
// TODO: Add constructor logic here
//

}
}

If I have a method, for example txtBoxSubmit, that assigns a variable
(string txtData) from txtBox.text, where do I put that code in the
above scaffold? Can it have an empty constructor?

Thanks!

May 12 '06 #2
On that note, I was hoping you or someone could assist me with the
following small problem:

I have a .ascx user control that declares a dropdownlist, ddlCategory,
and populates it from a sqlserver database. That works fine.

I am trying to create a common class for a method, runCategory, that
executes at the OnSelectedIndexChanged of the above user control
dropdown list. If I put this runCategory() in the control's own
codebehind file, along with the instantiation code, all is well..

BUT.......I want to use this situation as to learn how to create a
simple asp.net C# class. l know that I could just put this
ddlCategory() method into the .ascx code file without any trouble and
it will work but that defeats the learning purpose for me here.

I am doing nothing in design view and I want to have it all in code.

So when I try to put the OnSelectedIndexChanged functionality into its
own class, I get a "the name 'ddlCategory; does not exist in the
current context". I also get a "the name 'response' does not exist in
the current context..."

Could someone please explain why this behavior is occuring and how to
fix it?
============================
Here is the code (it's really simple):
/**** file = ddlCode.cs ********
/

/// <summary>
/// Summary description for ddlCode
/// </summary>
public class ddlCode
{
public ddlCode()
{
//
// TODO: Add constructor logic here
//

}

public void runCategory(object sender, EventArgs e)
{
string category;
category = ddlCategory.SelectedValue;
int index;
index = ddlCategory.SelectedIndex;
if (index != 0)
{
Response.Redirect("./sculpture2.aspx?catID=" + category);
}
}
I am not declaring a new namespace in the .ascx.cs file (if that helps
at all).
Thanks,
David

May 12 '06 #3
I acutally get the same message in if I combine it all into one file
(the ddlCategory error).
The partial class is public, the page_load is public, and runcategory
is public.

Thanks.

May 12 '06 #4
So you want to create a generic static method for runCategory?

But unfortunately, static method (or external objects) don't have direct
access to the current webform, so some workarounds are needed.

Something like this (Disclaimer: I wrote this in .NET v1.1, although it
should work in v2.0 too):

public static void runCategory(object sender, EventArgs e)
{
string category;
// This will look for value of "ddlCategory" in the current
request stream
category = HttpContext.Current.Request.Form["ddlCategory"];
index = getIndexZeroItem();
if (!category.Equals(getIndexZeroItem()))
{
HttpContext.Current.Response.Redirect("./sculpture2.aspx?catID="
+ category);
}
}

public static string getIndexZeroItem()
{
// implements this yourself to retrieve the first item in
dropdownlist, you may
// cheat by pushing this value in Session when you create the
dropdownlist
}
"Ranginald" <da*******@gmail.com>
???????:11*********************@u72g2000cwu.google groups.com...
On that note, I was hoping you or someone could assist me with the
following small problem:

I have a .ascx user control that declares a dropdownlist, ddlCategory,
and populates it from a sqlserver database. That works fine.

I am trying to create a common class for a method, runCategory, that
executes at the OnSelectedIndexChanged of the above user control
dropdown list. If I put this runCategory() in the control's own
codebehind file, along with the instantiation code, all is well..

BUT.......I want to use this situation as to learn how to create a
simple asp.net C# class. l know that I could just put this
ddlCategory() method into the .ascx code file without any trouble and
it will work but that defeats the learning purpose for me here.

I am doing nothing in design view and I want to have it all in code.

So when I try to put the OnSelectedIndexChanged functionality into its
own class, I get a "the name 'ddlCategory; does not exist in the
current context". I also get a "the name 'response' does not exist in
the current context..."

Could someone please explain why this behavior is occuring and how to
fix it?
[Code removed]

May 12 '06 #5
Opps... forgotten to remove the following line...
index = getIndexZeroItem();
"Lau Lei Cheong" <le****@yehoo.com.hk> ¼¶¼g©ó¶l¥ó·s»D:uj**************@TK2MSFTNGP03.phx.g bl...
So you want to create a generic static method for runCategory?

But unfortunately, static method (or external objects) don't have direct
access to the current webform, so some workarounds are needed.

Something like this (Disclaimer: I wrote this in .NET v1.1, although it
should work in v2.0 too):

public static void runCategory(object sender, EventArgs e)
{
string category;
// This will look for value of "ddlCategory" in the current
request stream
category = HttpContext.Current.Request.Form["ddlCategory"];
index = getIndexZeroItem();
if (!category.Equals(getIndexZeroItem()))
{

HttpContext.Current.Response.Redirect("./sculpture2.aspx?catID=" +
category);
}
}

public static string getIndexZeroItem()
{
// implements this yourself to retrieve the first item in
dropdownlist, you may
// cheat by pushing this value in Session when you create the
dropdownlist
}
"Ranginald" <da*******@gmail.com>
???????:11*********************@u72g2000cwu.google groups.com...
On that note, I was hoping you or someone could assist me with the
following small problem:

I have a .ascx user control that declares a dropdownlist, ddlCategory,
and populates it from a sqlserver database. That works fine.

I am trying to create a common class for a method, runCategory, that
executes at the OnSelectedIndexChanged of the above user control
dropdown list. If I put this runCategory() in the control's own
codebehind file, along with the instantiation code, all is well..

BUT.......I want to use this situation as to learn how to create a
simple asp.net C# class. l know that I could just put this
ddlCategory() method into the .ascx code file without any trouble and
it will work but that defeats the learning purpose for me here.

I am doing nothing in design view and I want to have it all in code.

So when I try to put the OnSelectedIndexChanged functionality into its
own class, I get a "the name 'ddlCategory; does not exist in the
current context". I also get a "the name 'response' does not exist in
the current context..."

Could someone please explain why this behavior is occuring and how to
fix it?
[Code removed]


May 12 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lefevre | last post by:
Hello I recently had troubles with a class inheritance hierarchy. I solved it, but it didn't satisfied me. I found the solution using this forum :) Actualy i found the following message...
3
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
6
by: Nafai | last post by:
Hello. I want to do something like this: class A { // It's virtual protected: float* data; int n; public: A(int a); virtual float* createData(); //...
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
19
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do...
10
by: JosephLee | last post by:
In Inside C++ object Model, Lippman said there are four cases in which compile will sythesize a default constructor to initialize the member variables if the constructor is absent: 1. there is a...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.