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

Beginner question on classes

Why does the following code generate the error:
//The type or namespace name 'c' could not be found (are you missing a using
directive or an assembly reference?)
static void Main()
{
Application.Run(new Form1());
class1 c = new class1();
}

private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(c.x.ToString());
}

class class1
{
public int x;
}
Nov 16 '05 #1
3 1022
c defined in Main() is local to that method only. It cannot be accessed from
anywhere (in this case button1_click). Declaring c at the class level will
give the output you are looking for.

"Dave" <da*******@newsgroup.nospam> wrote in message
news:uI*************@TK2MSFTNGP11.phx.gbl...
Why does the following code generate the error:
//The type or namespace name 'c' could not be found (are you missing a using
directive or an assembly reference?)
static void Main()
{
Application.Run(new Form1());
class1 c = new class1();
}

private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(c.x.ToString());
}

class class1
{
public int x;
}

Nov 16 '05 #2
Yes. Thank you for your help and patience.
"Shiva" <sh******@online.excite.com> wrote in message
news:ui*************@TK2MSFTNGP09.phx.gbl...
c defined in Main() is local to that method only. It cannot be accessed
from
anywhere (in this case button1_click). Declaring c at the class level will
give the output you are looking for.

"Dave" <da*******@newsgroup.nospam> wrote in message
news:uI*************@TK2MSFTNGP11.phx.gbl...
Why does the following code generate the error:
//The type or namespace name 'c' could not be found (are you missing a
using
directive or an assembly reference?)
static void Main()
{
Application.Run(new Form1());
class1 c = new class1();
}

private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(c.x.ToString());
}

class class1
{
public int x;
}

Nov 16 '05 #3
"Shiva" <sh******@online.excite.com> wrote in message
news:ui*************@TK2MSFTNGP09.phx.gbl...
c defined in Main() is local to that method only. It cannot be
accessed from
anywhere (in this case button1_click). Declaring c at the class
level will give the output you are looking for.


Btw: if you declare "c" in Main _after_ Application.Run(), the class would
be instantiated _after_ the application was closed anyway ...

Regards,

--
Frank Eller
www.frankeller.de
Nov 16 '05 #4

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

Similar topics

16
by: Rod Carrol | last post by:
Hello all, As a beginner I've been exeperiencing lots of errors while building my website, (I'm currently attempting to implement a member login/registration piece for my site using mySQL and...
15
by: PhilB | last post by:
Hello experts, I am a complete beginner in C++ (although I know C). I am trying to compile the code below, and I get the following error. Can anyone explain to me my mistake? Thanks! PhilB ...
3
by: pestul | last post by:
I'm quite new with Access so please bare with me. I'm pretty sure what I'm looking for will require the integration with VB to function properly. Here's what I have: Portfolios for inviduals...
8
by: ^MisterJingo^ | last post by:
Hi all, I have a question regarding inheritance. I'll use the following code for an example (its been stripped down to the minimum): // code start using System; class Animal {
4
by: Chris | last post by:
Hi all, I can serialize and deserialize a single class, but I'm wondering if it's possible to serialize/deserialize different class types in the same file? For example, I have a base class, and...
0
by: Paul | last post by:
Hi all, I know how to serialize and deserialize a single class, but is it possible to serialize multiple classes in one file? For example, I have a baseclass, and 3 derived classes. These...
4
by: Johs | last post by:
I am looking for a good C++ book for beginners. I have some experience with C and a lot of experience with Java. I am currently reading Bjarne Stroustrups C++ Programming Language but it starts off...
9
by: Daniel | last post by:
Looking to see if anyone can offer some suggestions on some good VB.net books? looking for beginner to intermidiate and to expert.. Any suggestions? -- ASP, SQL2005, DW8 VBScript
2
by: Daniel | last post by:
Looking for a good resource for beginners? books, online anything that will get me started.. what im trying to focus on is building a small program in c# to look thru a few files in a folder...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.