Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 12th, 2009, 09:57 PM
Authorized User
Points: 566, Level: 8
Points: 566, Level: 8 Points: 566, Level: 8 Points: 566, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2005
Location: St. Louis, , .
Posts: 96
Thanks: 0
Thanked 1 Time in 1 Post
Default using members of a C Sharp class

In C Sharp, just like in C++ you can have classes with "methods" and "members" but the syntax is different.

Does anyone know how to declare a varable in a C# class so that it is accessable to other classes and code?

After I have declared a class, I have accessed the methods of the class as it appears in intellisense type as I type the class name and then a dot. But the declared members of the class does not appear.
Reply With Quote
  #2 (permalink)  
Old January 13th, 2009, 01:43 AM
Imar's Avatar
Wrox Author
Points: 66,617, Level: 100
Points: 66,617, Level: 100 Points: 66,617, Level: 100 Points: 66,617, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,123
Thanks: 65
Thanked 1,386 Times in 1,366 Posts
Default

It needs to be marked as public (or internal):

Code:
 
public class TestClass
{
  public string HelloWorld()
  {
    return "Helllo World";
  }
}
Then in another class:

Console.WriteLine (new TestClass().HelloWorld());

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old January 13th, 2009, 04:21 PM
Authorized User
Points: 566, Level: 8
Points: 566, Level: 8 Points: 566, Level: 8 Points: 566, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2005
Location: St. Louis, , .
Posts: 96
Thanks: 0
Thanked 1 Time in 1 Post
Default

What is the point of using set and get in C Sharp?
It seems variables are used differently in this language than in C++.
For some reason, you have to have a static variable defined like this:
public static uint Somenum
{
set { m_somenum = value; }
get { return m_somenum; }
}
and prior to this declaration, you need to have this:
public uint m_sumenum;
This seems to be the only way to expose a member of a class to other classes in C#.
The problem is that I seem to be doing this improperly because I get a compile error:
An object reference is required for the non-static field, metod, or property '.......m_somenum"
I think I see the problem. The problem is that I cannot use a static varable like this.
So you have to instantiate the class in order to set these members of the class.
So how would you do the equivalent of a global class in C Sharp?
Would I do it something like this:
public clase SomeClass
{
SomeClass someclass = new SomeClass();

public static uint Somenum
{
set { m_somenum = value; }
get { return m_somenum; }
}
}


Or perhaps this "new" needs to be outside of the class in order to work. So my next question is this. How and where would that command be such that it the internal set methods could be accessed by the other classes in the code?
Reply With Quote
  #4 (permalink)  
Old January 13th, 2009, 04:38 PM
Imar's Avatar
Wrox Author
Points: 66,617, Level: 100
Points: 66,617, Level: 100 Points: 66,617, Level: 100 Points: 66,617, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,123
Thanks: 65
Thanked 1,386 Times in 1,366 Posts
Default

No no no, you're mixing things up.... Static members of a class are members of a class you can access without an instance of that class. That is, they are shared by *all* instances. For example:

Code:
 
public class Whatever
{
  private static int _counter = 0;
  public static string GetStuff
  {
    get
    {
      _counter++;
      return "Hello World " + _counter.ToString();
    }
  }
}
When you access this code like this:

Code:
Console.WriteLine (Whatever.GetStuf);
you'll get an ever increasing number each time you call this for the life time of the application.

Instance properties work on instances like this:

Code:
 
public class Whatever
{
  private int _counter = 0;
  public string GetStuff
  {
    get
    {
      _counter++;
      return "Hello World " + _counter.ToString();
    }
  }
}
First, note how _counter is private, but accessible in the public GetStuff property. There's no need to make it public as GetStuff is now non-shared as well.

Calling code like this repeatedly:

Code:
 
for (i = 0; i < 10; i++)
{
  Whatever whatever = new Whatever()
  Console.WriteLine (Whatever.GetStuf);
}
will give you a new counter with each instance. Since the counter is instance based, it gets a value of 0 every time you create a new instance. Calling the GetStuff method repeatedly on a single instance does increase the counter.

You seem to missing a few very important basics of C#. May I suggest you get a good book like Wrox's Beginning C# or Professional C#? Learning this stuff just from trying things out and comparing them with C++ may get you in troubles later. Getting the basics from a good book will definitely help you in the long run.

Edit: My example may not be the best as I am using a read-only GetStuff property to return non-related data. Typically you would use this to hide a directly related "backing variable":

Quote:
private string _firstName = string.Empty;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
For a discussion on why properties are useful, take a look here: I have a question re: Oject Oriented Programming

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; January 13th, 2009 at 04:43 PM.
Reply With Quote
  #5 (permalink)  
Old January 13th, 2009, 05:04 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Bill Thompson wrote
Quote:
you have to have a static variable defined like this:
No, no, no! Not at all true! You do not *HAVE* to use get/set property accessors if you make the data member public. It's just good OO programming technique. Many many many C++ programmer utilize similar accessors. It's just that the C++ language doesn't provide the property keyword, and so the C++ accessors don't look as "pretty". (Most C++ programmers end up using get_Name( ) and set_Name(value) methods, or similar, to make up for the lack of property keyword.)

But if a data member really is meant to be public and you can't conceive of any reason to handle it as a property, then just make it public and be done.

But *DO* read that link Imar pointed you to. We both chimed in on that one, too. LOL.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
schedule C-Sharp process Derf ASP.NET 2.0 Professional 9 April 18th, 2008 10:21 PM
Members list happygv Forum and Wrox.com Feedback 9 April 10th, 2007 11:33 AM
HOW TO EDIT THE MAC WITH C SHARP? Ϸ C# 0 June 23rd, 2006 02:38 AM
Listing members quiksilverhg Excel VBA 1 October 6th, 2005 05:09 AM
Best way to secure a members section tdaustin Classic ASP Basics 2 May 18th, 2004 07:30 AM



All times are GMT -4. The time now is 01:12 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.