473,385 Members | 1,610 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.

Conversion from a base class

I have a class:
Private Class ClientList
Inherits StringCollection

Public InternalUser As Boolean = False

Public Shadows Function Contains(ByVal ClientNumber As String) As
Boolean
If InternalUser Then Return True
Return MyBase.Contains(ClientNumber)
End Function
End Class

I need to do the following:

dim mClientList as ClientList
dim mStringCollection as new StringCollection

mClientList = mStringCollection.

What do I do to my class definition to allow this? Option Strict On flags
this as an illegal implicit conversion. I'm open to using

mClientList = new ClientList(mStringCollection) as well.

Thanks,
Mike Ober.
Jan 23 '07 #1
10 1230
I guess the standard question is "why?" You'll agree that a
StringCollection <ClientList, so what creates the "need?"
"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:u2**************@TK2MSFTNGP02.phx.gbl...
>I have a class:
Private Class ClientList
Inherits StringCollection

Public InternalUser As Boolean = False

Public Shadows Function Contains(ByVal ClientNumber As String) As
Boolean
If InternalUser Then Return True
Return MyBase.Contains(ClientNumber)
End Function
End Class

I need to do the following:

dim mClientList as ClientList
dim mStringCollection as new StringCollection

mClientList = mStringCollection.

What do I do to my class definition to allow this? Option Strict On flags
this as an illegal implicit conversion. I'm open to using

mClientList = new ClientList(mStringCollection) as well.

Thanks,
Mike Ober.

Jan 23 '07 #2
Create a constructor in the derived class that accepts an instance of
the base class.

On Jan 23, 2:21 pm, "Michael D. Ober" <obermd.@.alum.mit.edu.nospam>
wrote:
I have a class:

Private Class ClientList
Inherits StringCollection

Public InternalUser As Boolean = False

Public Shadows Function Contains(ByVal ClientNumber As String) As
Boolean
If InternalUser Then Return True
Return MyBase.Contains(ClientNumber)
End Function
End Class

I need to do the following:

dim mClientList as ClientList
dim mStringCollection as new StringCollection

mClientList = mStringCollection.

What do I do to my class definition to allow this? Option Strict On flags
this as an illegal implicit conversion. I'm open to using

mClientList = new ClientList(mStringCollection) as well.

Thanks,
Mike Ober.
Jan 24 '07 #3

"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:u2**************@TK2MSFTNGP02.phx.gbl...
:
: I have a class:
:
:
: Private Class ClientList
: Inherits StringCollection
:
: Public InternalUser As Boolean = False
:
: Public Shadows Function Contains(ByVal ClientNumber As String) As
: Boolean
: If InternalUser Then Return True
: Return MyBase.Contains(ClientNumber)
: End Function
: End Class
:
: I need to do the following:
:
: dim mClientList as ClientList
: dim mStringCollection as new StringCollection
:
: mClientList = mStringCollection.
:
: What do I do to my class definition to allow this? Option Strict On
: flags this as an illegal implicit conversion. I'm open to using
: mClientList = new ClientList(mStringCollection) as well.
:
: Thanks,
: Mike Ober.
Use this syntax:

mClientList = CType(mStringCollection, ClientList)

That said, don't actually try this. What you are doing here doesn't
make sense. Your new derived class ClientList exposes a new bit of
functionality (the InternalUser field) not found in the base
StringCollection Class. If you assign an instance of a
StringCollection type to the mClientList variable, your code would end
up choking if it ever tried to access that property. The following
compiles fine, but throws an error when run:
Dim mClientList As ClientList
Dim mStringCollection As New StringCollection

mClientList = CType(mStringCollection, ClientList)

'oops: can't get to the InternalUser field!
Console.WriteLine(mClientList.InternalUser)

The CType() method gets rid of that pesky compiler error, but the code
still bombs when you get to the WriteLine statement because while the
mClientList type (ClientList) exposes the InternalUser property, the
instance you are referencing (StringCollection) does not.

Now, it does work in reverse:

mStringCollection = mClientList

This compiles and runs without issue because the ClientList type
inherits from the StringCollection type and therefore exposes all of
the functionality of that base type (the functionality may be
overridden, but it's there). You'll never be able to access the
InternalUser property as long as the code think's it's working with an
instance of the StringCollection type, but that isn't an issue - there
are ways around that. For example, you can do the following:
Dim mClientList As New ClientList
Dim mStringCollection As StringCollection

mStringCollection = mClientList

Dim intUser As Boolean
intUser = CType(mStringCollection, ClientList).InternalUser

Console.WriteLine(intUser)
Ralf
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Jan 24 '07 #4
OK - I tried and came up blank before I posted my original message. How do
I assign the base class object?

public sub New(ByVal sc as StringCollection)
' What goes here? I can't use "mybase = sc"
' Do I actually need to loop through sc and add each element to mybase
mybase.new()
for each s as String in sc
mybase.Add(s)
next s
end sub

Thanks,
Mike.
<wf****@gmail.comwrote in message
news:11**********************@13g2000cwe.googlegro ups.com...
Create a constructor in the derived class that accepts an instance of
the base class.

On Jan 23, 2:21 pm, "Michael D. Ober" <obermd.@.alum.mit.edu.nospam>
wrote:
>I have a class:

Private Class ClientList
Inherits StringCollection

Public InternalUser As Boolean = False

Public Shadows Function Contains(ByVal ClientNumber As String) As
Boolean
If InternalUser Then Return True
Return MyBase.Contains(ClientNumber)
End Function
End Class

I need to do the following:

dim mClientList as ClientList
dim mStringCollection as new StringCollection

mClientList = mStringCollection.

What do I do to my class definition to allow this? Option Strict On
flags
this as an illegal implicit conversion. I'm open to using

mClientList = new ClientList(mStringCollection) as well.

Thanks,
Mike Ober.



Jan 24 '07 #5
Business Rules create the need.

Mike.

"Tom Leylan" <tl*****@nospam.netwrote in message
news:eK**************@TK2MSFTNGP06.phx.gbl...
>I guess the standard question is "why?" You'll agree that a
StringCollection <ClientList, so what creates the "need?"
"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:u2**************@TK2MSFTNGP02.phx.gbl...
>>I have a class:
Private Class ClientList
Inherits StringCollection

Public InternalUser As Boolean = False

Public Shadows Function Contains(ByVal ClientNumber As String) As
Boolean
If InternalUser Then Return True
Return MyBase.Contains(ClientNumber)
End Function
End Class

I need to do the following:

dim mClientList as ClientList
dim mStringCollection as new StringCollection

mClientList = mStringCollection.

What do I do to my class definition to allow this? Option Strict On
flags this as an illegal implicit conversion. I'm open to using

mClientList = new ClientList(mStringCollection) as well.

Thanks,
Mike Ober.




Jan 24 '07 #6
I suppose I was a bit too cryptic. A StringCollection isn't a ClientList so
unless something changes in the way OOP is implemented in DotNet I don't
think you'll just pass a reference and have it magically connect up.

You can copy the elements in the StringCollection into the ClientList and
probably fairly quickly using CopyTo and AddRange(). I could be mistaken
but I don't think you'll find syntax that will permit what you are hoping to
do.

Tom
"Michael D. Ober" <obermd.@.alum.mit.edu.no.spamwrote in message
news:za******************@newsread1.news.pas.earth link.net...
Business Rules create the need.

Mike.

"Tom Leylan" <tl*****@nospam.netwrote in message
news:eK**************@TK2MSFTNGP06.phx.gbl...
>>I guess the standard question is "why?" You'll agree that a
StringCollection <ClientList, so what creates the "need?"
"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:u2**************@TK2MSFTNGP02.phx.gbl...
>>>I have a class:
Private Class ClientList
Inherits StringCollection

Public InternalUser As Boolean = False

Public Shadows Function Contains(ByVal ClientNumber As String) As
Boolean
If InternalUser Then Return True
Return MyBase.Contains(ClientNumber)
End Function
End Class

I need to do the following:

dim mClientList as ClientList
dim mStringCollection as new StringCollection

mClientList = mStringCollection.

What do I do to my class definition to allow this? Option Strict On
flags this as an illegal implicit conversion. I'm open to using

mClientList = new ClientList(mStringCollection) as well.

Thanks,
Mike Ober.

Jan 24 '07 #7
Business Rules create the need.
>
Mike.
Mike,

I cannot resist on this, this kind of answers let me think about

Is there to a Business Rule that say thay you have to pee with your head in
the urinal and everybody is doing their best to fullfil that.

Cor


Jan 25 '07 #8
It turns out that the only place where I need to convert the
StringCollection to ClientList is in a line such as:

dim sc as StringCollection
dim cl as ClientList
' do some processing that may or may not create an actual StringCollection
if sc is Nothing then cl = new ClientList else cl = new ClientList(sc)

The code in this application is littered with cl.Contains, which as I stated
in another clarification post, sometimes needs to always return true (this
is actually part of a web-site and internal users get additional features)

I ended up implementing ClientList.New(StringCollection) by interating
through the collection and adding it to mybase.items.

Thanks for all the feedback.
Mike Ober.

"Tom Leylan" <tl*****@nospam.netwrote in message
news:ua**************@TK2MSFTNGP06.phx.gbl...
>I suppose I was a bit too cryptic. A StringCollection isn't a ClientList
so unless something changes in the way OOP is implemented in DotNet I don't
think you'll just pass a reference and have it magically connect up.

You can copy the elements in the StringCollection into the ClientList and
probably fairly quickly using CopyTo and AddRange(). I could be mistaken
but I don't think you'll find syntax that will permit what you are hoping
to do.

Tom
"Michael D. Ober" <obermd.@.alum.mit.edu.no.spamwrote in message
news:za******************@newsread1.news.pas.earth link.net...
>Business Rules create the need.

Mike.

"Tom Leylan" <tl*****@nospam.netwrote in message
news:eK**************@TK2MSFTNGP06.phx.gbl...
>>>I guess the standard question is "why?" You'll agree that a
StringCollection <ClientList, so what creates the "need?"
"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:u2**************@TK2MSFTNGP02.phx.gbl...
I have a class:
Private Class ClientList
Inherits StringCollection

Public InternalUser As Boolean = False

Public Shadows Function Contains(ByVal ClientNumber As String) As
Boolean
If InternalUser Then Return True
Return MyBase.Contains(ClientNumber)
End Function
End Class

I need to do the following:

dim mClientList as ClientList
dim mStringCollection as new StringCollection

mClientList = mStringCollection.

What do I do to my class definition to allow this? Option Strict On
flags this as an illegal implicit conversion. I'm open to using

mClientList = new ClientList(mStringCollection) as well.

Thanks,
Mike Ober.




Jan 25 '07 #9
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:OI**************@TK2MSFTNGP02.phx.gbl...
Is there to a Business Rule that say thay you have to pee with your head
in the urinal and everybody is doing their best to fullfil that.
I'd just like to save everybody the effort and point out that I have indeed
tried this and while it may seem practical at first, it does not work!
Jan 25 '07 #10
You would need to overload the = operator with the implicit / explicit
keyword. The operator overloading method should return a type of the
other class.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Jan 25 '07 #11

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

Similar topics

4
by: Jim Hubbard | last post by:
I have some C# code that is supposed to wrap the defrag APIs and I am trying to convert it to VB.Net (2003). But, I keep having problems. The C# code is relatively short, so I'll post it...
6
by: Ryan Mitchley | last post by:
Hi all Given bool bResult; shared_ptr<cSampleData> pNewData; shared_ptr<cBase> pNewBase; where cSampleData is descended from cBase, the following gives me a valid pNewData to the correct...
12
by: Vladimir_petter | last post by:
Dear All, There is the problem in nutshells (see the program bellow): It is ok to convert pointer to F<T> to the pointer to I. Now if I have pointer to member "F<T> entity::*" can I convert it...
2
by: Harold Howe | last post by:
Howdy all, I am getting a compiler error regarding a consrained conversion. It complains that it can't make the type conversion, even though the generic type argument inherits from the target of...
6
by: Peter Lee | last post by:
what's the correct behaver about the following code ? ( C++ standard ) I got a very strange result.... class MyClass { public: MyClass(const char* p) { printf("ctor p=%s\n", p);
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.