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

Extra lines appear when EdgeMode.Aliased option is used in WPF
exec not opening up .exe file
Field property is required. All field web controls require the field name to be set
Must be Placed Inside a Form Tag With runat=server
How to get 2 columns from datatable in linq [duplicate]
Mapping Properties of type of Complex Type to DB Columns in EF 6 using code first and custom EntityTypeConfiguration implementation
The data has been cleared after post
How to generate DDL using PetaPoco or NPoco?
Regex pattern for getting specific string
How to force another control to validate when a drop down's choice changes
Fill DataGridView with DataGridViewCheckBoxColumn using Linq
Why I can't use Template.FindName()
How do I launch the VBA Editor?
How to escape [ in app.config file?
Explicit conversion from int -> sbyte -> sbyte?
IL: how to generate specific C# function?

Categories

HOME
pact
botframework
dronekit
file-upload
opencv4android
query-string
esxi
mingw
sugarcrm
comments
vsixmanifest
select2
solidity
expo
aggregate
google-closure-compiler
versioning
wallet
google-calendar-api3
webgl-earth
pumping-lemma
chartist.js
checkmarx
emr
cqlsh
r-factor
postgresql-9.6
machine-code
dropdownbox
geocomplete
border-layout
armadillo
bzip2
head
angular-fullstack
hierarchical-clustering
google-cloud-shell
openidm
selection-sort
jtextarea
overhead
chain
blitz3d
fileopendialog
stripes
computed-properties
php-java-bridge
transaction-isolation
jde
database-administration
instabug
gnu-sort
commercetools
android-togglebutton
musicbrainz
pagecontrol
tokudb
web-standards
glog
autopep8
method-overloading
tigase
worklight-appcenter
behance-api
code-smell
satisfiability
jsmpp
8-puzzle
mencoder
infinity.js
google-checkout
servicemanager
linear-interpolation
derived-class
django-admin-tools
django-1.4
cherokee
recess
opendir
service-layer
designated-initializer
nbehave
viewdata
nscharacterset
carbon-emacs
morfik
scala-designer

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