The Artima Developer Community
Sponsored Link

C# Answers Forum
Using set and get in C Sharp

1 reply on 1 page. Most recent reply: Jan 15, 2009 10:17 PM by Amit Mitra

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
William Thompson

Posts: 11
Nickname: xarzu
Registered: Nov, 2007

Using set and get in C Sharp Posted: Jan 13, 2009 10:02 AM
Reply to this message Reply
Advertisement
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"


Amit Mitra

Posts: 2
Nickname: mitraamit
Registered: Jan, 2009

Re: Using set and get in C Sharp Posted: Jan 15, 2009 10:17 PM
Reply to this message Reply
Hi Williams,

Your peice of code will never compile, as it violates a basic concept.

You are trying to use a instace variable in a static method. whch is not logical and not even permissible.

Properties in C# are nothing more than preparing getters and setters of a private variable. please see the code below to understand further

C++ :

Class Test{
private int someLocalVariable;

public int getSomeLocalVraible(){
return someLocalVariable;
}

public setSomeLocalVariable(int value){
someLocalVariable = value;
}
}


C#:

Class Test{
private int someLocalVariable;

public int SomeLocalVraible{
get{
return someLocalVariable;
}

set{
someLocalVariable = value;
}
}
}

Flat View: This topic has 1 reply on 1 page
Topic: How to get whole DLL content? Previous Topic   Next Topic Topic: how to implement single sign on in c#.net

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use