c#


get access to written properties of a class


I'm still not practiced in oop.. now I know the importantness of it :)
I have many methods and now I like to save collected strings in public variables to have the possiblity to access them from another function.
normaly I would make just public or private variables with get and set.
But this I think it's not so clean because this propertys are in "every intellisense" visible.
I think to do this into a class may be "testClass" and define the properties there.
But now, how I access to the values which I have written into the propertys of this class? To write them in I have to create a new instance of the class, but how access to the created instance?
// edit
protected void GetValues()
{
// Access to the public variable town.
string myNewtown = publictown;
string myNewName = publicname;
// How to acces to the values which I saved in the class informations?
// I like anything like that
string myNewtown = informations.publictown;
string myNewName = informations.publicname;
// or
string myNewtown = myinfo.publictown;
string myNewName = myinfo.publicname;
}
protected void Setvalues()
{
informations myinfo = new informations()
{
publicname = "leo",
publictown = "london"
};
}
private string publicname { get; set; }
private string publictown { get; set; }
private class informations
{
public string publicname { get; set; }
public string publictown { get; set; }
}
Thanks
If you want your properties to be accessible without creating an instance, use the static keyword.
EDIT: In your example, you would replace
public string publicname { get; set; }
with
public static string publicname { get; set; }
which allows you to read the field as
string myNewname = informations.publicname;
and set it with
informations.publicname = "whatever";
Of course, this means that you can only have one instance of publicname in your application -- in particular, in an ASP.NET application, this might not be what you want!
If you want access a created object, you need to store reference to it after creating.
Having look at your sample, I can offer you following change:
protected void GetValues()
{
// Access to the public variable town.
string myNewtown = publictown;
string myNewName = publicname;
// or
string myNewtown = myinfo.publictown;
string myNewName = myinfo.publicname;
}
protected void Setvalues()
{
publicname = "leo";
publictown = "london";
}
// we store reference to internal object
informations myinfo = new informations();
// and delegate property access to its properties.
public string publicname
{
get{ return informations.publicname;}
set{ informations.publicname = value; }
}
public string publictown
{
get{ return informations.publictown;}
set{ informations.publictown = value; }
}
private class informations
{
public string publicname { get; set; }
public string publictown { get; set; }
}

Related Links

Session user not register using membership provider of asp.net mvc c#
GetDynamicJsonObject exception
combobox not displaying items
How to cast List<object> to List<SomethingElse>
Major difference between 'for each' and 'for' loop in .NET [duplicate]
Improving recursion method in C#
How use single Log4net configuration (separate file or inside app.config) for all my project?
Enterprise Service Bus (.NET flavors) - Consuming a batch of messages at a time
How To Write To Console From Background Thread's Event Loop?
XML comments in MonoDevelop
How to let the user to enter primary keys manually
Razor ViewEngine Temporary compilation .cs files
Mono for Android Location using C# & VS 2010
Event Bubbling and MVP: ASP.NET
websocket successfully handshaking, but not sending receiving messages correctly (C# Server)
Why can't I use my dll that is referenced config file

Categories

HOME
spring-boot
twitter-bootstrap
alasql
cxf
azure-eventhub
android-ndk
glsl
azure-resource-manager
pandoc
ms-access-2010
jshell
swap
richtextbox
opacity
segmentation-fault
cracking
oms
select2
angular-universal
urllib2
camunda
swift3.1
infopath
template10
aiml
caldav
glibc
react-jsx
xna
typedef
gnu-screen
javaagents
nsopenpanel
traveling-salesman
jopendocument
utc
azure-table-storage
folder
incapsula
minitest
sampling
extern
strategy-pattern
var
linkurious
file-conversion
google-chrome-console
amazon-rds-aurora
text-extraction
easyquery
serialversionuid
riemann
enet
fileopendialog
directinput
gevent
normalisation
kik
std
askbot
opendolphin
overlapping
imageicon
bioconductor
maven-antrun-plugin
webservices-client
gitk
pushwoosh
tooltwist
pagecontrol
vectordrawable
genome
firebase-tools
service-broker
chrome-mobile
teamstudio-unplugged
crtdbg.h
diffmerge
nsmutableurlrequest
oxygenxml
serializer
dateadd
satisfiability
email-headers
xml-validation
8-puzzle
dylan
ojb
violation
wpa
qgraphicsscene
extensibility
wpd
codeigniter-form-helper
phone-state-listener
database-restore
custom-cursor
aspmenu-control
towerjs
flexbuilder
mismatch
sipdroid
functional-specifications
chromebug
dojox.gfx
focus-stealing
wtsapi32
hotfix

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App