473,408 Members | 2,813 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,408 software developers and data experts.

Class Placement - stupid question

I'm using visual studio to create a web application, but I'm having trouble
using a few classes that I've defined:

public class tank
{
public tank()
{
}
public float depth;
public float maxMudLvl;
public int agitatorQty;
public int impellerQty;
public float mudVolume;
public float turnoverRatio;
public float GPM;
}
public class roundTank : tank
{
public roundTank()
{
}
public float diameter;
}
public class squareTank : tank
{
public squareTank()
{
}
public float length;
public float width;
}

I tried putting them within the application namespace, but that didn't work.
Then I tried putting them in a class file, and that still didn't work. The
intellitype seems to work when I declare a new instance of the class, but
when I try to access the members, it doesn't.

I've searched and flipped many pages, but I can't find anything that says
simply: here's where you define classes or global variables in an ASP.NET
visual studio project.

Someone please forgive my ignorance and let me know. Thanks.
Nov 17 '05 #1
8 1387
Hi,

In the project explorer right click in the project name. Add New / Add
Class

It will create a new file with a class on it in the correct namespace

Just include your classes there

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"StillStuckOnJava" <St**************@discussions.microsoft.com> wrote in
message news:02**********************************@microsof t.com...
I'm using visual studio to create a web application, but I'm having
trouble
using a few classes that I've defined:

public class tank
{
public tank()
{
}
public float depth;
public float maxMudLvl;
public int agitatorQty;
public int impellerQty;
public float mudVolume;
public float turnoverRatio;
public float GPM;
}
public class roundTank : tank
{
public roundTank()
{
}
public float diameter;
}
public class squareTank : tank
{
public squareTank()
{
}
public float length;
public float width;
}

I tried putting them within the application namespace, but that didn't
work.
Then I tried putting them in a class file, and that still didn't work. The
intellitype seems to work when I declare a new instance of the class, but
when I try to access the members, it doesn't.

I've searched and flipped many pages, but I can't find anything that says
simply: here's where you define classes or global variables in an ASP.NET
visual studio project.

Someone please forgive my ignorance and let me know. Thanks.

Nov 17 '05 #2
That's exactly what I did and this is what I get when I try to instantiate
(rountTank rt = new roundTank(); OR tank rt = new roundTank();)

ERROR:
The type or namespace name 'rt' could not be found (are you missing a using
directive or an assembly reference?)
"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

In the project explorer right click in the project name. Add New / Add
Class

It will create a new file with a class on it in the correct namespace

Just include your classes there

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"StillStuckOnJava" <St**************@discussions.microsoft.com> wrote in
message news:02**********************************@microsof t.com...
I'm using visual studio to create a web application, but I'm having
trouble
using a few classes that I've defined:

public class tank
{
public tank()
{
}
public float depth;
public float maxMudLvl;
public int agitatorQty;
public int impellerQty;
public float mudVolume;
public float turnoverRatio;
public float GPM;
}
public class roundTank : tank
{
public roundTank()
{
}
public float diameter;
}
public class squareTank : tank
{
public squareTank()
{
}
public float length;
public float width;
}

I tried putting them within the application namespace, but that didn't
work.
Then I tried putting them in a class file, and that still didn't work. The
intellitype seems to work when I declare a new instance of the class, but
when I try to access the members, it doesn't.

I've searched and flipped many pages, but I can't find anything that says
simply: here's where you define classes or global variables in an ASP.NET
visual studio project.

Someone please forgive my ignorance and let me know. Thanks.


Nov 17 '05 #3
Hi,
I just did it with the code you posted and worked, make sure that both files
( the one with the classes definition and where you use them) have the same
namespace declared.
Are they in the same project?

I think that the error should be somewhere else, if the compiler does not
find roundTank it should say :

The type or namespace name 'roundTank'

but definetly no 'rt'

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"StillStuckOnJava" <St**************@discussions.microsoft.com> wrote in
message news:F6**********************************@microsof t.com...
That's exactly what I did and this is what I get when I try to instantiate
(rountTank rt = new roundTank(); OR tank rt = new roundTank();)

ERROR:
The type or namespace name 'rt' could not be found (are you missing a
using
directive or an assembly reference?)
"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

In the project explorer right click in the project name. Add New / Add
Class

It will create a new file with a class on it in the correct namespace

Just include your classes there

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"StillStuckOnJava" <St**************@discussions.microsoft.com> wrote in
message news:02**********************************@microsof t.com...
> I'm using visual studio to create a web application, but I'm having
> trouble
> using a few classes that I've defined:
>
> public class tank
> {
> public tank()
> {
> }
> public float depth;
> public float maxMudLvl;
> public int agitatorQty;
> public int impellerQty;
> public float mudVolume;
> public float turnoverRatio;
> public float GPM;
> }
> public class roundTank : tank
> {
> public roundTank()
> {
> }
> public float diameter;
> }
> public class squareTank : tank
> {
> public squareTank()
> {
> }
> public float length;
> public float width;
> }
>
> I tried putting them within the application namespace, but that didn't
> work.
> Then I tried putting them in a class file, and that still didn't work.
> The
> intellitype seems to work when I declare a new instance of the class,
> but
> when I try to access the members, it doesn't.
>
> I've searched and flipped many pages, but I can't find anything that
> says
> simply: here's where you define classes or global variables in an
> ASP.NET
> visual studio project.
>
> Someone please forgive my ignorance and let me know. Thanks.


Nov 17 '05 #4
Here is my web form code and my class code:

-------------------------------------
Web Form (main_frm.aspx)
-------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace agitatorSz
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class main_frm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label title_lbl;
protected System.Web.UI.WebControls.Label tank_lbl;
protected System.Web.UI.WebControls.DropDownList tank_cbo;
protected System.Web.UI.WebControls.TextBox length_txt;
protected System.Web.UI.WebControls.TextBox width_txt;
protected System.Web.UI.WebControls.TextBox depth_txt;
protected System.Web.UI.WebControls.TextBox mudLvl_txt;
protected System.Web.UI.WebControls.TextBox diameter_txt;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label diameter_lbl;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Button tank_btn;
protected System.Web.UI.WebControls.Button dimension_btn;
protected System.Web.UI.WebControls.Label Logo_lbl;
protected System.Web.UI.WebControls.Label dimensionErr_lbl;

//Public variable for hoding the tank type selected
string tankType;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dimension_btn.Click += new
System.EventHandler(this.dimension_btn_Click);
this.tank_btn.Click += new System.EventHandler(this.tank_btn_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void tank_btn_Click(object sender, System.EventArgs e)
{
tankType=tank_cbo.SelectedItem.Value;

//get the tank type selection and instantiate the "tank" class
//initialize the next group of data entry controls (text boxes)
switch(tank_cbo.SelectedItem.Value)
{
case "1":
{
tank rt = new roundTank();
length_txt.Enabled = false;
width_txt.Enabled = false;
depth_txt.Enabled = true;
diameter_txt.Enabled = true;
mudLvl_txt.Enabled = true;
//declare class round tank and initialize

break;
}
case "2":
{
tank st = new squareTank();
length_txt.Enabled = true;
width_txt.Enabled = true;
depth_txt.Enabled = true;
diameter_txt.Enabled = false;
mudLvl_txt.Enabled = true;
break;
}
}
}

private void dimension_btn_Click(object sender, System.EventArgs e)
{
//validate data entry
switch(tankType)
{
case "1":
{
if(diameter_txt.Text=="" || depth_txt.Text=="" || mudLvl_txt.Text=="")
{
dimensionErr_lbl.Text="All appropriate dimensions must be entered";
}
else
{
rt.depth=2;
depth_txt.Text=rt.depth;
}
break;
}
case "2":
{
if(length_txt.Text=="")
{
dimensionErr_lbl.Text="All appropriate dimensions must be entered";
}
else
{
st.depth=2;
depth_txt.Text=st.depth;
}
break;
}
}
}
}
}

-------------------------------------
class (tank.cs)
-------------------------------------
using System;

namespace agitatorSz
{
/// <summary>
/// Summary description for tank.
/// </summary>

public class tank
{
public tank()
{
}
public float depth;
public float maxMudLvl;
public int agitatorQty;
public int impellerQty;
public float mudVolume;
public float turnoverRatio;
public float GPM;
}
public class roundTank : tank
{
public roundTank()
{
}
public float diameter;
}
public class squareTank : tank
{
public squareTank()
{
}
public float length;
public float width;
}
}
"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,
I just did it with the code you posted and worked, make sure that both files
( the one with the classes definition and where you use them) have the same
namespace declared.
Are they in the same project?

I think that the error should be somewhere else, if the compiler does not
find roundTank it should say :

The type or namespace name 'roundTank'

but definetly no 'rt'

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"StillStuckOnJava" <St**************@discussions.microsoft.com> wrote in
message news:F6**********************************@microsof t.com...
That's exactly what I did and this is what I get when I try to instantiate
(rountTank rt = new roundTank(); OR tank rt = new roundTank();)

ERROR:
The type or namespace name 'rt' could not be found (are you missing a
using
directive or an assembly reference?)
"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

In the project explorer right click in the project name. Add New / Add
Class

It will create a new file with a class on it in the correct namespace

Just include your classes there

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"StillStuckOnJava" <St**************@discussions.microsoft.com> wrote in
message news:02**********************************@microsof t.com...
> I'm using visual studio to create a web application, but I'm having
> trouble
> using a few classes that I've defined:
>
> public class tank
> {
> public tank()
> {
> }
> public float depth;
> public float maxMudLvl;
> public int agitatorQty;
> public int impellerQty;
> public float mudVolume;
> public float turnoverRatio;
> public float GPM;
> }
> public class roundTank : tank
> {
> public roundTank()
> {
> }
> public float diameter;
> }
> public class squareTank : tank
> {
> public squareTank()
> {
> }
> public float length;
> public float width;
> }
>
> I tried putting them within the application namespace, but that didn't
> work.
> Then I tried putting them in a class file, and that still didn't work.
> The
> intellitype seems to work when I declare a new instance of the class,
> but
> when I try to access the members, it doesn't.
>
> I've searched and flipped many pages, but I can't find anything that
> says
> simply: here's where you define classes or global variables in an
> ASP.NET
> visual studio project.
>
> Someone please forgive my ignorance and let me know. Thanks.


Nov 17 '05 #5
ABC
You are using the instances of the tank class which are not accessible in
the place you want to work with them. The only changes you should make is to
declare "rt" and "st" in a place that make them have a wider accessibility.
I have marked the changes to you code with // <-----------------------------
Look at the following - (hope this help)

/*File: main_frm.aspx
//----------------------------------------------------------CODE
TRUNCATED---------------------------------------------------------
*/

tank rt ; // <-----------------------------
tank st ; // <-----------------------------

private void tank_btn_Click(object sender, System.EventArgs e)
{
tankType=tank_cbo.SelectedItem.Value;

//get the tank type selection and instantiate the "tank" class
//initialize the next group of data entry controls (text boxes)

switch(tank_cbo.SelectedItem.Value)
{
case "1":
{
rt= new roundTank(); // <-----------------------------
length_txt.Enabled = false;
width_txt.Enabled = false;
depth_txt.Enabled = true;
diameter_txt.Enabled = true;
mudLvl_txt.Enabled = true;
//declare class round tank and initialize

break;
}
case "2":
{
st = new squareTank(); // <-----------------------------
length_txt.Enabled = true;
width_txt.Enabled = true;
depth_txt.Enabled = true;
diameter_txt.Enabled = false;
mudLvl_txt.Enabled = true;
break;
}
}
}
private void dimension_btn_Click(object sender, System.EventArgs e)
{
//validate data entry
switch(tankType)
{
case "1":
{
if(diameter_txt.Text=="" || depth_txt.Text=="" || mudLvl_txt.Text=="")
{
dimensionErr_lbl.Text="All appropriate dimensions must be entered";
}
else
{
rt.depth=2;
depth_txt.Text=rt.depth.ToString(); // <-----------------------------
}
break;
}
case "2":
{
if(length_txt.Text=="")
{
dimensionErr_lbl.Text="All appropriate dimensions must be entered";
}
else
{
st.depth=2;
depth_txt.Text=st.depth.ToString(); // <-----------------------------
}
break;
}
}
}

/*
---------------------------------------------------------END OF-CODE
TRUNCATED--------------------------------------*/

Nov 17 '05 #6
Thank you very much, I appreciate your help. This, however, creates a couple
of questions:

1. Do I have to have the tank.cs class file, or can I just place it in the
application namespace?

2. Shouldn't I be able to instantiate classes anywhere in my event code? If
not, how can I?

"ABC" wrote:
You are using the instances of the tank class which are not accessible in
the place you want to work with them. The only changes you should make is to
declare "rt" and "st" in a place that make them have a wider accessibility.
I have marked the changes to you code with // <-----------------------------
Look at the following - (hope this help)

/*File: main_frm.aspx
//----------------------------------------------------------CODE
TRUNCATED---------------------------------------------------------
*/

tank rt ; // <-----------------------------
tank st ; // <-----------------------------

private void tank_btn_Click(object sender, System.EventArgs e)
{
tankType=tank_cbo.SelectedItem.Value;

//get the tank type selection and instantiate the "tank" class
//initialize the next group of data entry controls (text boxes)

switch(tank_cbo.SelectedItem.Value)
{
case "1":
{
rt= new roundTank(); // <-----------------------------
length_txt.Enabled = false;
width_txt.Enabled = false;
depth_txt.Enabled = true;
diameter_txt.Enabled = true;
mudLvl_txt.Enabled = true;
//declare class round tank and initialize

break;
}
case "2":
{
st = new squareTank(); // <-----------------------------
length_txt.Enabled = true;
width_txt.Enabled = true;
depth_txt.Enabled = true;
diameter_txt.Enabled = false;
mudLvl_txt.Enabled = true;
break;
}
}
}
private void dimension_btn_Click(object sender, System.EventArgs e)
{
//validate data entry
switch(tankType)
{
case "1":
{
if(diameter_txt.Text=="" || depth_txt.Text=="" || mudLvl_txt.Text=="")
{
dimensionErr_lbl.Text="All appropriate dimensions must be entered";
}
else
{
rt.depth=2;
depth_txt.Text=rt.depth.ToString(); // <-----------------------------
}
break;
}
case "2":
{
if(length_txt.Text=="")
{
dimensionErr_lbl.Text="All appropriate dimensions must be entered";
}
else
{
st.depth=2;
depth_txt.Text=st.depth.ToString(); // <-----------------------------
}
break;
}
}
}

/*
---------------------------------------------------------END OF-CODE
TRUNCATED--------------------------------------*/


Nov 17 '05 #7
Hi,
1. Do I have to have the tank.cs class file, or can I just place it in the
application namespace?
dont really understand whta you mean, you can have the class in its own
file. as long it belong to the same project & is part of the same namespace
you will be ok

ITOH this has nothing to do with the problem in hand
2. Shouldn't I be able to instantiate classes anywhere in my event code?
If
not, how can I?


You can instantiate them anywhere, what you cannot is declare them
anywhere, with the proposed structure they are declared once and are
assigned as needed

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 17 '05 #8
ABC
> 1. Do I have to have the tank.cs class file, or can I just place it in the
application namespace?
It doesn't matter in which file (*.cs) your class is. You can put your
class anywhere you like, as far as you specify the proper namespace for it.
(Class are put into different files usually for team works or for some other
purposes. It is not a "must" to do this in your applications.)
2. Shouldn't I be able to instantiate classes anywhere in my event code?
If
not, how can I?


Your classes are not instantiate as far as you have not used the "new +
className". This means that you can instantiate your class in your event
code. Sth like this will not instantiate the class unless you invoke the
Click method:

//--------------------------------------------------------

MyClass cls; // < - This is where you set the type of your identifier (or
object)

public void Click()
{
cls = new MyClass(); // <- This is where the class is instantiated.
cls.DoSth(); //< This is where you can use the instance members of the
class
}

//-------------------------------------------------------------

Also, you should make sure that your classes are instantiated before you can
use their instance (non-static) members. If in another method in your code
you try to use the class while it has not been instantiated, you will face
errors. As a result, you should try to whether instantiate all you classes
in the application's Main() entry (which is not so common) or instantiate
them in your methods wherever you like and "then" use its members,

Nov 17 '05 #9

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

Similar topics

20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
8
by: elviin | last post by:
Hello. I tried a sample programm using placement new. I am not sure if i understand this technique correctly. I do not mean usage but a real world application. Could anyone to describe some...
2
by: Zardoz | last post by:
I've created a new header file in which one will find : #using <mscorlib.dll> using namespace System; __gc class G { public: int k; int sum(int){return 0;}
7
by: Ole Nielsby | last post by:
I want to create a class with a variably-sized array inlined. The array size will be known at each instantiation but not at compile time. Once created, arrays won't grow or shrink and their...
10
by: imutate | last post by:
Some questions about ctors and class members Is v private in the following ? If it is why put the declaration at the top ? Is there any difference to putting it in the private section ? The...
5
by: Lagarde Sébastien | last post by:
Hello, I write code to debug new call with following macro: #define new (MemoryManager::Get().setOwner (__FILE__, __LINE__, _FUNCTION-), FALSE) ? NULL : new The setOwner allow to save the...
29
by: Brad Pears | last post by:
Here is a simple OO design question... I have a Contract class. The user can either save an existing contract or they start off fresh with a blank contract, fill in the data and then save a...
9
by: karthikbalaguru | last post by:
Hi, I find that articles stating that 'placement new' constructs an object on a pre-allocated buffer and so takes less time. Actually, we have to consider the allocation of the buffer and then...
8
by: Kislay | last post by:
Can malloc be used to create/allocate memory to an object of a class . We use new , but can we use malloc ?
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: 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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.