471,337 Members | 1,169 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Class Fields

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"

}
Sep 9 '08 #1
6 1712
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.

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"

}


Sep 9 '08 #2
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

"pagerintas pritupimas" <or*@com.netwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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.

>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"

}



Sep 9 '08 #3
On Tue, 09 Sep 2008 09:58:26 -0700, DaveL <dv*****@sbcglobal.netwrote:
if u notice in my example the class that created the 2
Classes Class1 and class2 ,has no idea of which is which
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
Sep 9 '08 #4
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

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Tue, 09 Sep 2008 09:58:26 -0700, DaveL <dv*****@sbcglobal.netwrote:
>if u notice in my example the class that created the 2
Classes Class1 and class2 ,has no idea of which is which

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

Sep 9 '08 #5
Just 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" <dv*****@sbcglobal.netwrote in message
news:0b*******************@nlpi068.nbdc.sbc.com...
>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

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
>On Tue, 09 Sep 2008 09:58:26 -0700, DaveL <dv*****@sbcglobal.netwrote:
>>if u notice in my example the class that created the 2
Classes Class1 and class2 ,has no idea of which is which

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


Sep 9 '08 #6
thanks 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" <Me***********@Insdirect.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Just 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" <dv*****@sbcglobal.netwrote in message
news:0b*******************@nlpi068.nbdc.sbc.com...
>>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

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
>>On Tue, 09 Sep 2008 09:58:26 -0700, DaveL <dv*****@sbcglobal.netwrote:

if u notice in my example the class that created the 2
Classes Class1 and class2 ,has no idea of which is which

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



Sep 9 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

42 posts views Thread by WindAndWaves | last post: by
3 posts views Thread by J.J. Feminella | last post: by
reply views Thread by Ed West | last post: by
2 posts views Thread by Torben Laursen | last post: by
4 posts views Thread by =?Utf-8?B?YmlsbCB0aWU=?= | last post: by

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.