PC Review


Reply
Thread Tools Rate Thread

Coding Problem, Help Needed

 
 
batista
Guest
Posts: n/a
 
      19th Sep 2005
Hello to ALL,

I'll explain my problem by pseudo code

class A
{
int x;
char y;
string z;
}

class B
{
GetVariable(int i)
{
A a = new A();
//Now I want to return the ith varaible
//of class A
}
}

class C
{
GetVar()
{
B b = new B();
b.GetVariable(0) //it shud return x
b.GetVariable(1) //it shud return y
b.GetVariable(2) //it shud return z
}
}

Now how do I implement the GetVariable(int i), in C#
The reason I have class B is because there are lot's of classes
like class A, and i don't want to edit them all.

Any Suggestions...

Thanks In advance...
Cheers...

Bye

 
Reply With Quote
 
 
 
 
Pipo
Guest
Posts: n/a
 
      19th Sep 2005
You can use an interface.

"batista" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello to ALL,
>
> I'll explain my problem by pseudo code
>
> class A
> {
> int x;
> char y;
> string z;
> }
>
> class B
> {
> GetVariable(int i)
> {
> A a = new A();
> //Now I want to return the ith varaible
> //of class A
> }
> }
>
> class C
> {
> GetVar()
> {
> B b = new B();
> b.GetVariable(0) //it shud return x
> b.GetVariable(1) //it shud return y
> b.GetVariable(2) //it shud return z
> }
> }
>
> Now how do I implement the GetVariable(int i), in C#
> The reason I have class B is because there are lot's of classes
> like class A, and i don't want to edit them all.
>
> Any Suggestions...
>
> Thanks In advance...
> Cheers...
>
> Bye
>



 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      19th Sep 2005
batista,

You will have to make sure that the original class declares GetVariable
as virtual, so that it can be overridden by derived classes. If it is not,
then there is little you could do (you could shadow it with the new keyword,
but that is evil).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"batista" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello to ALL,
>
> I'll explain my problem by pseudo code
>
> class A
> {
> int x;
> char y;
> string z;
> }
>
> class B
> {
> GetVariable(int i)
> {
> A a = new A();
> //Now I want to return the ith varaible
> //of class A
> }
> }
>
> class C
> {
> GetVar()
> {
> B b = new B();
> b.GetVariable(0) //it shud return x
> b.GetVariable(1) //it shud return y
> b.GetVariable(2) //it shud return z
> }
> }
>
> Now how do I implement the GetVariable(int i), in C#
> The reason I have class B is because there are lot's of classes
> like class A, and i don't want to edit them all.
>
> Any Suggestions...
>
> Thanks In advance...
> Cheers...
>
> Bye
>



 
Reply With Quote
 
batista
Guest
Posts: n/a
 
      20th Sep 2005
Thanks For Replies,

Pipo and Nicholas Paldino,

Nicholas Paldino, I understand that I'll need to declare GetVariable()
virtual in all classes,
but how do i implement a function like this.

I mean how do i number the variables of a class, so that i can ask for
ith variable in the class.

Plz help me on that...
Thanks In Advance...
Cheers...

Bye

 
Reply With Quote
 
=?Utf-8?B?RGFuIE5lZWx5?=
Guest
Posts: n/a
 
      20th Sep 2005


"batista" wrote:
> I mean how do i number the variables of a class, so that i can ask for
> ith variable in the class.


AFAIK you'll have to hardcode it. The compiler is free to reorder
variables inside a data structure to optimize performance. ie putting 4
chars sequentially so they all fell in the same 32bit chunk when on the
memory bus.
 
Reply With Quote
 
Deviant
Guest
Posts: n/a
 
      20th Sep 2005
You could use Reflection to enumerate the fields in the class (although
they would have to be public, which seems to defeat the object).

The easiest solution would be to replace all the variables in Class A
with an object array..


batista wrote:
> Thanks For Replies,
>
> Pipo and Nicholas Paldino,
>
> Nicholas Paldino, I understand that I'll need to declare GetVariable()
> virtual in all classes,
> but how do i implement a function like this.
>
> I mean how do i number the variables of a class, so that i can ask for
> ith variable in the class.
>
> Plz help me on that...
> Thanks In Advance...
> Cheers...
>
> Bye


 
Reply With Quote
 
justncase80
Guest
Posts: n/a
 
      20th Sep 2005
this is ugly but in desperation you could just do somethign like this:

object GetVariable(int x)
{
switch(x)
{
case(0):
return x;
case(1):
return y;
case(2):
return z;
default: return null;
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Coding Problem, Help Needed batista Microsoft VC .NET 3 20th Sep 2005 11:27 AM
Coding Problem, Help Needed batista Microsoft VC .NET 0 19th Sep 2005 05:19 PM
Help with coding needed =?Utf-8?B?SmFt?= Microsoft Access Form Coding 2 5th May 2005 02:09 AM
Coding help needed JMay Microsoft Excel Programming 2 9th Jan 2004 11:11 AM
Coding help needed. Angelina Microsoft ADO .NET 3 7th Dec 2003 05:47 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:36 AM.