CSharp/C# >> Class Fields

by DaveL » Tue, 09 Sep 2008 23:38:07 GMT

How Can i Determine if a class Contains a Particular
Field Variable

Example
public class AbastractClass
{
public int Field1
public int Field2
}
public Class Class1:AbstractClass
{
public int Specificfield
}
public Class Class2:AbstractClass
{
public int SpecificField
public int NewField
}
public class MainClass
{
int condition = 1

AbstractClass oJob
if (condition==1)
{
oJob = new Class1()
}
else
{
oJob = new Class2()
}
///how to find if ojob has a field "NewFiled"

}



CSharp/C# >> Class Fields

by pagerintas pritupimas » Wed, 10 Sep 2008 00:05:50 GMT


Something like that:

Class2 class2 = oJob as Class2;
if (class2 != null)
{
Console.WriteLine(class2.NewField);
}

Or you could use reflection. Although these are solutions to the wrong
problem. The real problem is design.

CSharp/C# >> Class Fields

by DaveL » Wed, 10 Sep 2008 00:58:26 GMT

if u notice in my example the class that created the 2
Classes Class1 and class2 ,has no idea of which is which
if (condition==1) then ojob is class1
else then ojob is class2

//processing code
if ojob.NewField==10) //Crash if ojob is Class1


Thanks Dave

CSharp/C# >> Class Fields

by Peter Duniho » Wed, 10 Sep 2008 01:02:44 GMT


First: as long as the variable "condition" doesn't change (and in the
design you've presented, it shouldn't), the MainClass class can just check
that variable and cast the instance as needed when it needs access to that
field:

if (condition == 1)
{
int value = ((Class2)ojob).NewField;
}

Second: if for some reason you can't rely on the "condition" variable,
then the previous reply to your question will work fine, just as it was
given. Repeating the example:

Class2 class2 = ojob as Class2;

if (class2 != null)
{
int value = class2.NewField;
}

And alternative to the "as" operator would be to use the "is" operator:

if (ojob is Class2)
{
int value = ((Class2)ojob).NewField;
}

Finally: the other thing that the previous reply was exactly correct about
is that the design you've presented really doesn't look all that sound in
the first place. We can't say for sure, because the code is obviously
just demonstration code, and we have no idea what the larger picture is.
But conditionally processing an instance based on its type is often better
handled in some other way (polymorphism being the most common superior
alternative).

Pete

CSharp/C# >> Class Fields

by DaveL » Wed, 10 Sep 2008 04:45:15 GMT

I undestand all that you are telling me, but back to the original question
how to check if a field exists <

lets say from my previous posts....that when the inherited classes are
created , they then
are passed to a Method that only expects the type
Now One of the inherited classes exposed a New field that is not in Class1

//generic method
AbstractClass oJob
if (condition=1)
{
oJob=new Class1()
}
else
{
oJob=Class2()
}
this.RunJob(oJob)

private void RunJob(AbstractClass oJob)
{
// in here we have No Clue if a Field Exists becase the oJob comes From
the Same Abastract/base Class and Class1 does not have the exposed Field
NewField
//i can do one of 2 things here try/Catch or i think Reflection.Propertyget
or somthing

any help is appriciated

}
all comments good or bad are welcome, some programmers inherit good and bad
code
so i need answers , when i find bad coding practices and the best result i
can impliment

Thanks
DaveL

CSharp/C# >> Class Fields

by Mel Weaver » Wed, 10 Sep 2008 05:20:21 GMT

ust cast it like Jon said

private void RunJob(AbstractClass oJob)
{
Class2 c = oJob as Class2;
if (c != null)
{
// then set your value;
}
}

// or using reflection
using System.Reflection;
private void RunJob(AbstractClass oJob)
{
PropertyInfo propertyInfo =
oJob.GetType().GetProperty("EnterFieldName");
if (propertyInfo != null)
{
// then set your value;
}
}


"DaveL" < XXXX@XXXXX.COM > wrote in message
news:0bBxk.25330$ XXXX@XXXXX.COM ...



CSharp/C# >> Class Fields

by DaveL » Wed, 10 Sep 2008 06:33:45 GMT

hanks Mel, thats what i need cause the code im repairing
the method involved dont know about the missing value
but must use it if present

Thanks Alot
Dave

"Mel Weaver" < XXXX@XXXXX.COM > wrote in message
news:% XXXX@XXXXX.COM ...



Similar Threads

1. Restricting access to a class' field to only one other class

Is there a way with C# to allow one class access to a method or field of
another class, without making that method or field visible to all other
classes, as would be the case when making the method or field public?

Thanks,

Dennis


2. class fields vs class properties - Microsoft .NET Framework

3. TextBox Class field highlighting question

I have noticed a "different" behavior in a .NET application I have written in 
C#.  When I tab between textbox controls, the contents of the control I am 
tabbing into is typically highlighted.  However, if I move the cursor to a 
different location within a text box control, tab away, and then tab back, 
the control seems to "remember" the location of the cursor.  Rather than 
highlight this control, it places the cursor at the location it was last at 
when leaving the control.

Is this a behavior that I can turn off?  I could not find any property that 
seemed to address this.

Thanks!

4. Access base class fields using reflection - CSharp/C#

5. looping through a classes fields

Is there a way to loop through a classes fields without first knowing what
fields are there?  Trying to write a generic class that could build a query
based on a classes fields and types.  This would save me having to write a
method for each class I write to handle its database access and could have a
single function to handle all classes.

Not thinking it can be done but figured I'd ask...

Thanks,

glenn


6. Access to base class fields - CSharp/C#

7. Class Field in list.

Hi,
I am creating an address book example. For the purpose i have created
2 classes
one is
ContactInfo
	name
	department

PhoneInfo
	OfficePhone
	Mobile

Now I want to design the class in such a way that when I type
ContactInfo.PhoneInfo.mobile
it should take the list of mobile numbers

im using dotnet 2.0 and know little about generics.
can we implement generics for the same.

8. How to Populate Base + Child Class Fields - CSharp/C#