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

how to use List Class in Base class?

phl
Hi, I have a class, which inherits a class that inherits a List class.
How do I pass this base List Class to another function by reference? At
the moment I just create a new List for doing this but I don't want to
do this unless I have to.

cheers
here's some code:
classA : List<string>
{

}

classB: classA
{
function B()
{
//how do I pass the List collection to somefunction here???
VVVVV
somefunction(????, string svar)
}

}

Class SomeClass
{
function somefunction(ref List<string> mylist, string svar)
{
mylist.add(svar);
}
}

May 12 '06 #1
9 1670
this is a list .. so just pass "this"

somefunction(this, string svar)

btw from the code shown you don't need ref ...

Cheers,

Greg Young
MVP - C#
"phl" <ki************@hotmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
Hi, I have a class, which inherits a class that inherits a List class.
How do I pass this base List Class to another function by reference? At
the moment I just create a new List for doing this but I don't want to
do this unless I have to.

cheers
here's some code:
classA : List<string>
{

}

classB: classA
{
function B()
{
//how do I pass the List collection to somefunction here???
VVVVV
somefunction(????, string svar)
}

}

Class SomeClass
{
function somefunction(ref List<string> mylist, string svar)
{
mylist.add(svar);
}
}

May 12 '06 #2
> //how do I pass the List collection to somefunction here???

Since your claqss inherits List<string>, it *is* a List<string>. So, you
pass your class instance to the method.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"phl" <ki************@hotmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
Hi, I have a class, which inherits a class that inherits a List class.
How do I pass this base List Class to another function by reference? At
the moment I just create a new List for doing this but I don't want to
do this unless I have to.

cheers
here's some code:
classA : List<string>
{

}

classB: classA
{
function B()
{
//how do I pass the List collection to somefunction here???
VVVVV
somefunction(????, string svar)
}

}

Class SomeClass
{
function somefunction(ref List<string> mylist, string svar)
{
mylist.add(svar);
}
}

May 12 '06 #3
"phl" <ki************@hotmail.com> wrote:
Hi, I have a class, which inherits a class that inherits a List class.
How do I pass this base List Class to another function by reference? At
the moment I just create a new List for doing this but I don't want to
do this unless I have to.


You cannot pass a variable declared as a descendant type (or 'this', for
that matter) to a method which requires a ref parameter, because doing
so would violate type safety. Consider:

class A {}
class B : A {}
class C : A {}

class X
{
static void F(ref A a)
{
a = new C();
}
}

How could you possibly pass a variable of type B to X.F()?

Reference types (i.e. types defined with the 'class' keyword in C#) are
reference types already, and don't require a 'ref' keyword.

-- Barry
May 12 '06 #4
phl
Hi, thanks for the help I got it now.

I want to be efficient, so I don't want to pass by val, hence i try to
use ref. If I don't pass by ref and pass the instance of the class to
the function, would there be alot of overhead? What would be the
overhead of creating a new List everytime I need to call the fucntion
and pass by ref? With the base List having 2 possible scenarios in
mind:

1. Base List has 100s of elements already
2. Base List is empty

May 12 '06 #5
"phl" <ki************@hotmail.com> wrote:
Hi, thanks for the help I got it now.

I want to be efficient, so I don't want to pass by val, hence i try to
use ref. If I don't pass by ref and pass the instance of the class to
the function, would there be alot of overhead?
Reference ('class') types are already a reference to an object allocated
on the garbage collected heap. Passing a reference type as is (i.e.
without 'ref') is simply copying the native pointer size for the system
(4 bytes for 32-bit systems).
What would be the
overhead of creating a new List everytime I need to call the fucntion
and pass by ref? With the base List having 2 possible scenarios in
mind:


Don't pass by reference at all - you don't need to, unless the function
you're calling needs to actually assign a *different* class instance in
the parameter.

All .NET 'class' types are already passed by reference, and there's
nothing you can do about it to make it less efficient, unless you use a
value type (i.e. a type declared with 'struct' in C#).

When you apply 'ref' to a reference type argument, you're actually
passing a reference to a reference. If you want to think of it at the
machine level, you'd be passing a pointer to a pointer to an instance.

-- Barry
May 12 '06 #6
Barry Kelly <ba***********@gmail.com> wrote:

<snip>
All .NET 'class' types are already passed by reference, and there's
nothing you can do about it to make it less efficient, unless you use a
value type (i.e. a type declared with 'struct' in C#).


While you clearly understand the actual semantics, calling the default
behaviour "pass by reference" is incorrect and can be confusing. The
reference is passed by value, which is different to a class instance
being passed by reference.
See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 12 '06 #7
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
While you clearly understand the actual semantics, calling the default
behaviour "pass by reference" is incorrect and can be confusing. The
reference is passed by value, which is different to a class instance
being passed by reference.


At the machine level, all parameters are passed by value. To implement
pass by reference, a reference must be passed by value, usually in the
form of a pointer.

So the only difference is the level of abstraction you or I am thinking
at. I'm thinking at the machine level.

-- Barry
May 12 '06 #8
Barry Kelly <ba***********@gmail.com> wrote:
While you clearly understand the actual semantics, calling the default
behaviour "pass by reference" is incorrect and can be confusing. The
reference is passed by value, which is different to a class instance
being passed by reference.


At the machine level, all parameters are passed by value. To implement
pass by reference, a reference must be passed by value, usually in the
form of a pointer.

So the only difference is the level of abstraction you or I am thinking
at. I'm thinking at the machine level.


Whereas I thought we were talking at the language level. At the machine
level there's no such concept as "pass by reference" in the first
place, so if you're thinking at the machine level, why are you saying
that anything is passed by reference?

All I can say is that I've seen plenty of people get confused by the
description of reference type parameters being passed by reference by
default. Their confusion is cleared up when you explain that they're
references which are passed by value. It also makes more sense of the
situation where a reference type parameter really *is* passed by
reference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 13 '06 #9
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
All I can say is that I've seen plenty of people get confused by the
description of reference type parameters being passed by reference by
default.


I agree. I'll be more clear in future.

-- Barry
May 13 '06 #10

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

Similar topics

5
by: Allen | last post by:
Hi all, I have a derived class that inherits from two base classes. One of the base class constructors takes as input a pointer that is initialized in the other base class. How can I make sure...
1
by: Vikas | last post by:
I have a template class called Base with child classes called Child1 and Child2 as follows: template <typename T> class Base { … }; class Child1 : Base<Concrete1>
3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
14
by: mshetty | last post by:
Hi, I get an error "Warning: b::a_method hides the virtual function a::a_method()." on compiling the following code.. #include <iostream.h> class a {
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
7
by: Rob Meade | last post by:
Hi all, Ok - I'm going around in circles and would appreciate some help. I have written a windows service, which up until I tried to be clever was working well :o) I have a main class which...
11
by: anongroupaccount | last post by:
What measures should be taken to avoid this sort of thing? class Base { }; class Derived1 : public Base { private: int i, j, k;
5
by: EqDev | last post by:
I have a class that is a control derived from UserControl. I want to use serialization and deserialization with this calss but I get an exception "Cannot serialize member...
6
by: MattWilson.6185 | last post by:
Hi, I'm trying to find out if something is possible, I have a few diffrent lists that I add objects to and I would like to be able to have a wrapper class that won't affect the internal object, for...
0
by: SaviorX | last post by:
So, I have created some Collision Detection classes that all derive from a base class known as CollisionBase. I have a List<CollisionBase> in which I will be going through and calling a method...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.