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

Class Properties

thav
6
I just need a better explanation for dummies you can say. I'm reading a book and its making no sense to me. And I keep getting a:

An unhandled exception of type 'System.StackOverflowException' occurred


Properties are for encapsulating correct? Using the set, get, return.

An example is this;


The Person class should be abstract and have a private field named myName


so I wrote this.

public abstract class Person
{
private string myName;

//create property
public string Name
{
get
{
return Name;
}

set
{
Name = value;
}
}
}


and in my Main( )

I wrote this;

Student s = new Student();
s.Name = "Tweety Bird";

to get that. Any help or explain to me properties a little better. Thanks!
Mar 29 '07 #1
2 1452
Ganon11
3,652 Expert 2GB
Is this in C#? It doesn't look like any C or C++ code to me.

Regardless, I believe your function is declared improperly. Get and Set are terms that programmers use to name similar functions, but they are not keywords in most languages. Instead of writing get {...} and set {...} inside the same function, you should write two separate functions - one called getName (returning a String) and one called setName (to set the String).
Mar 29 '07 #2
nmadct
83 Expert
This is clearly C#, not C/C++. The get/set keywords are used to declare properties in C#. I see a problem with this code:

Expand|Select|Wrap|Line Numbers
  1.  set
  2. {
  3.     Name = value;
  4. }
  5.  
This is invalid since Name is the name of the property. The property itself cannot have a value, in reality it is just a pair of functions that are tied together by a common name and data type. You need to declare another variable to store the value. Usually this is done like this:

Expand|Select|Wrap|Line Numbers
  1. private string _name;
  2. public string Name {
  3.     get { return _name; }
  4.     set { _name = value; }
  5. }
  6.  
The get/set methods act as "gatekeepers" for the private value of _name. The strength of this approach is that you can add extra functionality into those methods to do anything you want when the property is fetched or changed.
Mar 29 '07 #3

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

Similar topics

21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
5
by: Keith Bannister | last post by:
I'm new to .net so here goes. I'm tying to deserialize a class that is associated with an XML schema. I created the C# class with xsd.exe as below: xsd.exe /c /n:somenamespace...
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
6
by: Shimon Sim | last post by:
Hi I am working on application that need to hold custom user information - Last and first name, email, some other domain related information. I used to create Base class for all my pages. The base...
16
by: Dennis | last post by:
I have a class named "myclass" and an arraylist containing elements of type "MyClass". I want to get the value of a property of "MyClass" (a string type) for one of the arraylist elements. I...
9
by: David A. Osborn | last post by:
I have a set of classes that each have an enumeration in them, and based on dynamic input I need to access a different enumeration. For example Three classes Class_A, Class_B, and Class_C that...
2
by: mgoold2002 | last post by:
Hello. I've just begun programming in VB .NET, and I'm trying to turn all my modules into classes. In order to retrieve/exchange values from one class to another, I initiated New instances of the...
3
by: Simon Hart | last post by:
Hi, I am trying to implement some functionality as seen in MS CRM 3.0 whereby a basic Xml is deserialized into an object which contains properties. What I want to do from here is; cast the basic...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
5
by: Rainer Queck | last post by:
Hello NG, Is it possible to share the settings of an application with a class libreary? In my case I have a application and a set of different reports (home made) put into a class library. The...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...
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...

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.