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

Modifying private variables from outside the containing class?

Hey,

Is it possible to set a value for a private variable from outside the
class in which it was declared using perhaps reflection?

Something like

class A {
private string s;
}

class B {
private void Foo (string str)
{
A a = new A ();

// Here I need a way to set the value of A.s, without making it
public
}
}

Is something like this possible? I dont mind if its in a hacky
fashion :-)

Lukas

May 8 '07 #1
2 1144
On May 8, 8:46 am, lukasli...@gmail.com wrote:
Hey,

Is it possible to set a value for a private variable from outside the
class in which it was declared using perhaps reflection?

Something like

class A {
private string s;

}

class B {
private void Foo (string str)
{
A a = new A ();

// Here I need a way to set the value of A.s, without making it
public
}

}

Is something like this possible? I dont mind if its in a hacky
fashion :-)

Lukas
It is, but it wouldn't recommend it at all. When you do this, your
breaking encapsulation. If the implmentation of A changes, your code
very well may break. If you need to be able to change the value and
the class A is under your control, expose it as a property.

May 8 '07 #2
class B {
private void Foo (string str)
{
A a = new A ();

// Here I need a way to set the value of A.s, without making it
}
}
yes you can, as long as you run in Full Trust environment:

the code should look sth like that:

Type t = typeoof(B);
FiledInfo fi = t.GetField("a", BindingFlags.Instance | BindingFlags.Private)
fi.SetValue(a,value); //where a is an instance of A type

May 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: IHateSuperman | last post by:
public class StaticField2{ public static void main(String args){ private int x, y; // <<== error 1 for ( y = 0 ; y < 100 ; y++){ x = StaticMethod(); System.out.println(" x = "+x); } } public...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
9
by: John | last post by:
If a value type is immutable, I guess it's threadsafe to read it? But not threadsafe to assign a new value to it (can any value type be truely immutable? Isn't assigning a totally new value to it,...
86
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
63
by: time.swift | last post by:
Coming from a C++ / C# background, the lack of emphasis on private data seems weird to me. I've often found wrapping private data useful to prevent bugs and enforce error checking.. It appears...
7
by: Victor | last post by:
I just tried a test comparing a Function to a Private Function with this code: <% Option Explicit dim X1 X1 = 9 Private Function RealTest(here) RealTest = here + X1 End Function
14
by: jehugaleahsa | last post by:
I have a rather complex need. I have a class that parses web pages and extracts all relevant file addresses. It allows me to download every pdf on a web page, for instance. I would like to...
9
by: koschwitz | last post by:
Hi, I hope you guys can help me make this simple application work. I'm trying to create a form displaying 3 circles, which independently change colors 3 times after a random time period has...
14
by: Karsten Dambekalns | last post by:
Hi. Thomas Mlynarczyk wrote: Why do you want them to be private in the first place? I have yet to see code where this really makes sense... And if it does, then the external public API is...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.