473,385 Members | 1,908 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,385 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 1850
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: titancipher | last post by:
I have a container that I wish to allow the user to specify a custom comparison method very similar to std::less. However, I want it to function more like memcmp (returning -1 0 1), and I want to...
42
by: WindAndWaves | last post by:
Dear All Can you tell me why you use a class module??? Thank you Nicolaas ---
5
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
0
by: Ed West | last post by:
Hello, I am wondering about best practices for class hierarchies and using ADO.Net, especially the DataSet update/delete commands and the data relations... this needs to package/unpackage to xml...
3
by: Ross McLean | last post by:
Hi all, I've been teaching myself C# for a new project at work. I have a bit of a background in c++ and java but never been what you could call a guru. I'm having some strange things happening...
3
by: Jordan | last post by:
Suppose I have a system that keeps track of 5 different types of "People". My intent is to have a base Person class, then 5 derived classes for each of the specific person types (e.g., Patient,...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
2
by: Torben Laursen | last post by:
I have a complicated class that I use to store a lot of values Now I need a copy of that class and I have been trying to find a way to do this automatic so I don't have to assign all properties and...
4
by: =?Utf-8?B?YmlsbCB0aWU=?= | last post by:
I'm perusing a class that seems to utilize a nested class for private fields. In a nutshell, it looks as follows: public class Foo { public Property1 { get/set Bar.field1 }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.