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

Need help with Reflection from static method and child-class

Hi,

Suppose I have the following pseudo-class-ish code:

class A
{
member1;
member2;

public static Method() { return member1; }
}

class B : A
{
public static string Xxstring = "XXXXX";
B()
{
member1 = Xxstring;
member2 = Xxstring + Xxstring;
}
}

Now if I do the following:
B.Method() it will not work (return null) because the B's constructor was
not called as a result of the operation.

So I was hoping if there was some way i can determine from inside A.Method
that it was called from B and then I could get the type in B, and then
replace the member1 call with the Xxstring.

I have 80 or so child-classes of type A in my code and was hoping i could
refactor by doing this. Thanks.
Apr 5 '06 #1
2 1282
So I was hoping if there was some way i can determine from inside A.Method
that it was called from B
You can't, because it will actually be called on A. You really need an
instance members (or a redesign) to do what you want.

I have 80 or so child-classes of type A in my code and was hoping i could
refactor by doing this. Thanks.


There will still only be one copy of the static members in A, so you
can't give them different values for each subclass.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 5 '06 #2
I just ended up passing a string representing class type I would like to use
to my method in A.

I messed up on original code. A members were not static, B's were and
depending on which subclass of A (which would be which class of B), I set A's
member accordingly.

"Mattias Sjögren" wrote:
So I was hoping if there was some way i can determine from inside A.Method
that it was called from B


You can't, because it will actually be called on A. You really need an
instance members (or a redesign) to do what you want.

I have 80 or so child-classes of type A in my code and was hoping i could
refactor by doing this. Thanks.


There will still only be one copy of the static members in A, so you
can't give them different values for each subclass.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Apr 5 '06 #3

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

Similar topics

4
by: Savas Ates | last post by:
i have an web page. it use frame set .. but main frame is one server the other frame is another server. when i want to set a cookie or session from child frame.. it doesnt work... i research...
5
by: De Roeck | last post by:
Hi, I've want to launch an application by reflection and get his handle back. But when I run this code in C# then it's handled correctly. The application(-path) is started on a new thread and...
6
by: Ron | last post by:
I am building a utility that needs to extract all Windows forms control names from a compiled assembly for documentation purposes. I am currently using reflection, but not all of the controls are...
9
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the...
0
by: Gustavo Arriola | last post by:
Hello everybody! I'm trying to execute a method using Reflection. The code is as follows: public static void SoapHandler(Exception Error) { try { Type assemblyType;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.