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

Datagridview event not firing
How do I redistribute Microsoft.AnalysisServices.SPClient.Interfaces
Get Standard Touchpad Swipe
How to calculate the sum of the datatable column in c#
System.ArgumentException “Parameter is not valid” when comparing image dimensions
Set field via class
how to use ModelState.IsValid in ASP.NET?
protobuf-net : v2 deserialize v1 objects with inheritance
Getting data on ListView Select event and setting it in a div in C#
c# Oledb add list of parameters in a foreach loop
dotnet ef not works with ef tools 1.1.0-preview4-final
Check permission of a PDF using itext [duplicate]
Is there an event fired when WebBrowser content is redrawn?
How can I filter a DataTable without brute-forcing it?
Creating objects using an older version of Google Protobuf
WPF Combo box not calling setter method when item changed

Categories

HOME
algolia
marie
ide
virtual-machine
network-programming
pelican
blob
jquery-plugins
datetimepicker
glympse
travis-ci-cli
annyang
box-api
subprocess
adroitlogic
email-attachments
jinja2
reportlab
web-parts
legend-properties
accordion
summernote
gomobile
cherrypy
v4l2
drombler-fx
dynamic-memory-allocation
csproj
osgi-bundle
blockly
luhn
wit-ai
ansible-inventory
ragel
extern
idea-plugin
sknode
aspnetboilerplate
quartz
amazon-rds-aurora
cakephp-3.2
spring-webflow-2
rivets.js
evaluation
jenkins-docker
htop
sharpssh
aot
access-violation
recurring
hfp
bbedit
android-progressbar
std
controlpanel
gnu-sort
personality-insights
cloud-storage
tess4j
p4python
stereotype
vspackage
system.diagnostics
pymol
sony-xperia
unityvs
mind-manager
powerpoint-2010
fscanf
traceur
db2-connect
thucydides
google-mirror-api
xcode5.1
ojb
ncml
freeimage
hypertable
databus
jquery-ui-map
bindinglist
reflexil
datasheet
code-organization
file-processing
user-preferences

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