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

One Object, two possible types

Hi there,

I have a function, converted from VB, that is passed an object, and sets it
up. The object passed is either a ListBox or a ComboBox. The code checks to
make sure that the object passed is one of the two types, and then does some
work. I provide a cut-down version (the first code).

The problem is that, with the exception of one line (setting the ComboBox
DropDownStyle) the work done on the items is exactly the same, but I don't
know how to simplify the code by making a variable that is EITHER a ComboBox
OR a ListBox, but unspecified, with the relevant Methods and Properties
available of both. A (non-working) example is the second code.

In the code, Box.DoStuff() is things like .BeginUpdate(), .Items.Clear(),
and so on.

Is there any way to do this? Do I have to have separate code for each type
(ComboBox and ListBox), or perhaps separate functions? I suppose I could
possibly do some funky inheritance thing, creating special classes, but this
is only two functions, so it seems like overkill.

Thanks in advance, and apologies for any Newbieness

James

public void SetBoxUp(object TheListControl)
{
if ( ! ( TheListControl.GetType() == typeof(ListBox) ||
TheListControl.GetType() == typeof(ComboBox) ) )
return;

ListControl lc = (ListControl) TheListControl;
lc.DoStuff(); // do things that are accessable to a ListControl

if ( TheListControl.GetType() == typeof(ComboBox) )
{
ComboBox cb = (ComboBox) TheListControl;
cb.DropDownStyle = ComboBoxStyle.DropDownList;
cb.DoStuff(); // do things that are accessable to a ComboBox
}
else
{
ListBox lb = (ListBox) TheListControl;
lb.DoStuff(); // do things that are accessable to a ListBox
}
}

public void SetBoxUpNew(object TheListControl)
{
if ( ! ( TheListControl.GetType() == typeof(ListBox) ||
TheListControl.GetType() == typeof(ComboBox) ) )
return;

((ListControl) TheListControl).DoStuff(); // do things that are
accessable to a ListControl

if ( TheListControl.GetType() == typeof(ComboBox) )
((ComboBox) TheListControl).DropDownStyle =
ComboBoxStyle.DropDownList;

// Get the object type, and use to Class the object correctly, then
DoStuff() - this doesn't work
( (TheListControl.GetType()) TheListControl).DoStuff(); // do
things that are accessable to a ComboBox and ListBox
}
Nov 17 '05 #1
7 1346
Hi James,
You could mask the object as Control first and setup the common properties
and then mask as each control for the speciffic properties.
hope that helps
"James CC" <Ja*****@Nowhere.com> wrote in message
news:OC**************@tk2msftngp13.phx.gbl...
Hi there,

I have a function, converted from VB, that is passed an object, and sets
it up. The object passed is either a ListBox or a ComboBox. The code
checks to make sure that the object passed is one of the two types, and
then does some work. I provide a cut-down version (the first code).

The problem is that, with the exception of one line (setting the ComboBox
DropDownStyle) the work done on the items is exactly the same, but I don't
know how to simplify the code by making a variable that is EITHER a
ComboBox OR a ListBox, but unspecified, with the relevant Methods and
Properties available of both. A (non-working) example is the second code.

In the code, Box.DoStuff() is things like .BeginUpdate(), .Items.Clear(),
and so on.

Is there any way to do this? Do I have to have separate code for each type
(ComboBox and ListBox), or perhaps separate functions? I suppose I could
possibly do some funky inheritance thing, creating special classes, but
this is only two functions, so it seems like overkill.

Thanks in advance, and apologies for any Newbieness

James

public void SetBoxUp(object TheListControl)
{
if ( ! ( TheListControl.GetType() == typeof(ListBox) ||
TheListControl.GetType() == typeof(ComboBox) ) )
return;

ListControl lc = (ListControl) TheListControl;
lc.DoStuff(); // do things that are accessable to a ListControl

if ( TheListControl.GetType() == typeof(ComboBox) )
{
ComboBox cb = (ComboBox) TheListControl;
cb.DropDownStyle = ComboBoxStyle.DropDownList;
cb.DoStuff(); // do things that are accessable to a ComboBox
}
else
{
ListBox lb = (ListBox) TheListControl;
lb.DoStuff(); // do things that are accessable to a ListBox
}
}

public void SetBoxUpNew(object TheListControl)
{
if ( ! ( TheListControl.GetType() == typeof(ListBox) ||
TheListControl.GetType() == typeof(ComboBox) ) )
return;

((ListControl) TheListControl).DoStuff(); // do things that are
accessable to a ListControl

if ( TheListControl.GetType() == typeof(ComboBox) )
((ComboBox) TheListControl).DropDownStyle =
ComboBoxStyle.DropDownList;

// Get the object type, and use to Class the object correctly, then
DoStuff() - this doesn't work
( (TheListControl.GetType()) TheListControl).DoStuff(); // do
things that are accessable to a ComboBox and ListBox
}

Nov 17 '05 #2
Both ListBox and ComboBox inherit from ListControl, so you can cast to that
class and use its methods.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"James CC" <Ja*****@Nowhere.com> escribió en el mensaje
news:OC**************@tk2msftngp13.phx.gbl...
Hi there,

I have a function, converted from VB, that is passed an object, and sets
it up. The object passed is either a ListBox or a ComboBox. The code
checks to make sure that the object passed is one of the two types, and
then does some work. I provide a cut-down version (the first code).

The problem is that, with the exception of one line (setting the ComboBox
DropDownStyle) the work done on the items is exactly the same, but I don't
know how to simplify the code by making a variable that is EITHER a
ComboBox OR a ListBox, but unspecified, with the relevant Methods and
Properties available of both. A (non-working) example is the second code.

In the code, Box.DoStuff() is things like .BeginUpdate(), .Items.Clear(),
and so on.

Is there any way to do this? Do I have to have separate code for each type
(ComboBox and ListBox), or perhaps separate functions? I suppose I could
possibly do some funky inheritance thing, creating special classes, but
this is only two functions, so it seems like overkill.

Thanks in advance, and apologies for any Newbieness

James

public void SetBoxUp(object TheListControl)
{
if ( ! ( TheListControl.GetType() == typeof(ListBox) ||
TheListControl.GetType() == typeof(ComboBox) ) )
return;

ListControl lc = (ListControl) TheListControl;
lc.DoStuff(); // do things that are accessable to a ListControl

if ( TheListControl.GetType() == typeof(ComboBox) )
{
ComboBox cb = (ComboBox) TheListControl;
cb.DropDownStyle = ComboBoxStyle.DropDownList;
cb.DoStuff(); // do things that are accessable to a ComboBox
}
else
{
ListBox lb = (ListBox) TheListControl;
lb.DoStuff(); // do things that are accessable to a ListBox
}
}

public void SetBoxUpNew(object TheListControl)
{
if ( ! ( TheListControl.GetType() == typeof(ListBox) ||
TheListControl.GetType() == typeof(ComboBox) ) )
return;

((ListControl) TheListControl).DoStuff(); // do things that are
accessable to a ListControl

if ( TheListControl.GetType() == typeof(ComboBox) )
((ComboBox) TheListControl).DropDownStyle =
ComboBoxStyle.DropDownList;

// Get the object type, and use to Class the object correctly, then
DoStuff() - this doesn't work
( (TheListControl.GetType()) TheListControl).DoStuff(); // do
things that are accessable to a ComboBox and ListBox
}

Nov 17 '05 #3
Hi Nassos,

Thanks for your reply.

I'm not totally sure what you mean by 'masking'. I tried casting the object
to ListBox to access the methods and properties common to both, but when the
object passed was a CheckBox I got an error.

How does one mask?

James

"Nassos" <na***@orama-tech.gr> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi James,
You could mask the object as Control first and setup the common properties
and then mask as each control for the speciffic properties.
hope that helps

Nov 17 '05 #4
Dear Carlos,

Thanks for taking the time to answer.

I am trying to access Methods and Parameters that are present in ListBox and
ComboBox, but not ListControl, for example .BeginUpdate(), .EndUpdate(),
..Items[], and so on. As far as I know you can't access those through a
ListControl, can you?

James

"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:%2***************@TK2MSFTNGP09.phx.gbl...
Both ListBox and ComboBox inherit from ListControl, so you can cast to
that class and use its methods.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"James CC" <Ja*****@Nowhere.com> escribió en el mensaje
news:OC**************@tk2msftngp13.phx.gbl...
Hi there,

I have a function, converted from VB, that is passed an object, and sets
it up. The object passed is either a ListBox or a ComboBox. The code
checks to make sure that the object passed is one of the two types, and
then does some work. I provide a cut-down version (the first code).

The problem is that, with the exception of one line (setting the ComboBox
DropDownStyle) the work done on the items is exactly the same, but I
don't know how to simplify the code by making a variable that is EITHER a
ComboBox OR a ListBox, but unspecified, with the relevant Methods and
Properties available of both. A (non-working) example is the second code.

In the code, Box.DoStuff() is things like .BeginUpdate(), .Items.Clear(),
and so on.

Is there any way to do this? Do I have to have separate code for each
type (ComboBox and ListBox), or perhaps separate functions? I suppose I
could possibly do some funky inheritance thing, creating special classes,
but this is only two functions, so it seems like overkill.

Thanks in advance, and apologies for any Newbieness

James

public void SetBoxUp(object TheListControl)
{
if ( ! ( TheListControl.GetType() == typeof(ListBox) ||
TheListControl.GetType() == typeof(ComboBox) ) )
return;

ListControl lc = (ListControl) TheListControl;
lc.DoStuff(); // do things that are accessable to a ListControl

if ( TheListControl.GetType() == typeof(ComboBox) )
{
ComboBox cb = (ComboBox) TheListControl;
cb.DropDownStyle = ComboBoxStyle.DropDownList;
cb.DoStuff(); // do things that are accessable to a
ComboBox
}
else
{
ListBox lb = (ListBox) TheListControl;
lb.DoStuff(); // do things that are accessable to a ListBox
}
}

public void SetBoxUpNew(object TheListControl)
{
if ( ! ( TheListControl.GetType() == typeof(ListBox) ||
TheListControl.GetType() == typeof(ComboBox) ) )
return;

((ListControl) TheListControl).DoStuff(); // do things that are
accessable to a ListControl

if ( TheListControl.GetType() == typeof(ComboBox) )
((ComboBox) TheListControl).DropDownStyle =
ComboBoxStyle.DropDownList;

// Get the object type, and use to Class the object correctly, then
DoStuff() - this doesn't work
( (TheListControl.GetType()) TheListControl).DoStuff(); // do
things that are accessable to a ComboBox and ListBox
}


Nov 17 '05 #5

I had been about to post what Carlos did, but then I checked the docs
and found what you did - that there are members which are present in
both ComboBox and ListBox - with identical semantics - but which are
NOT defined in their parent class ListControl. This is arguably an
oversight on the part of the Framework creators, although there might
be a good reason why they did it this way.

As it is, I don't think there's any way around the inelegance of
(pseudo-code)

If it's a combobox
((combobox)it).beginupdate
((combobox)it).items.clear
...
Else
((listbox)it).beginupdate
((listbox)it).items.clear
...

If we were in an environment where late-binding were possible (eg
VBScript, non-Strict VB), *then* we could just say

var.beginupdate
var.items.clear
....

BUT to my mind the performance loss from late binding, and the safety
loss from weak typing are too high a price for the code clarity gains.

Enter the long-winded code and mutter curses at the Framework, is the
best we can do here.

JamesCC wrote:
Dear Carlos,

Thanks for taking the time to answer.

I am trying to access Methods and Parameters that are present in ListBox and
ComboBox, but not ListControl, for example .BeginUpdate(), .EndUpdate(),
.Items[], and so on. As far as I know you can't access those through a
ListControl, can you?

James

"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:%2***************@TK2MSFTNGP09.phx.gbl...
Both ListBox and ComboBox inherit from ListControl, so you can cast to
that class and use its methods.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"James CC" <Ja*****@Nowhere.com> escribió en el mensaje
news:OC**************@tk2msftngp13.phx.gbl...
Hi there,

I have a function, converted from VB, that is passed an object, and sets
it up. The object passed is either a ListBox or a ComboBox. The code
checks to make sure that the object passed is one of the two types, and
then does some work. I provide a cut-down version (the first code).

The problem is that, with the exception of one line (setting the ComboBox
DropDownStyle) the work done on the items is exactly the same, but I
don't know how to simplify the code by making a variable that is EITHER a
ComboBox OR a ListBox, but unspecified, with the relevant Methods and
Properties available of both. A (non-working) example is the second code.

In the code, Box.DoStuff() is things like .BeginUpdate(), .Items.Clear(),
and so on.

Is there any way to do this? Do I have to have separate code for each
type (ComboBox and ListBox), or perhaps separate functions? I suppose I
could possibly do some funky inheritance thing, creating special classes,
but this is only two functions, so it seems like overkill.

Thanks in advance, and apologies for any Newbieness

James

public void SetBoxUp(object TheListControl)
{
if ( ! ( TheListControl.GetType() == typeof(ListBox) ||
TheListControl.GetType() == typeof(ComboBox) ) )
return;

ListControl lc = (ListControl) TheListControl;
lc.DoStuff(); // do things that are accessable to a ListControl

if ( TheListControl.GetType() == typeof(ComboBox) )
{
ComboBox cb = (ComboBox) TheListControl;
cb.DropDownStyle = ComboBoxStyle.DropDownList;
cb.DoStuff(); // do things that are accessable to a
ComboBox
}
else
{
ListBox lb = (ListBox) TheListControl;
lb.DoStuff(); // do things that are accessable to a ListBox
}
}

public void SetBoxUpNew(object TheListControl)
{
if ( ! ( TheListControl.GetType() == typeof(ListBox) ||
TheListControl.GetType() == typeof(ComboBox) ) )
return;

((ListControl) TheListControl).DoStuff(); // do things that are
accessable to a ListControl

if ( TheListControl.GetType() == typeof(ComboBox) )
((ComboBox) TheListControl).DropDownStyle =
ComboBoxStyle.DropDownList;

// Get the object type, and use to Class the object correctly, then
DoStuff() - this doesn't work
( (TheListControl.GetType()) TheListControl).DoStuff(); // do
things that are accessable to a ComboBox and ListBox
}



Nov 17 '05 #6
Dear Larry,

Thank you for your considered and clear answer (not implying anything
against the others, though). Yeah, I'd suspected that might be the case; I'm
not totally comfortable with objects in C#, coming from a C background, and
I hoped there was something I didn't know, some sort of way of creating a
variable that was a class of both ListBox and ComboBox.

I have considered creating a class that inherits from ComboBox and ListBox
(I seem to recall that's possible) and then passing and using that, but I'm
not at home, so I haven't tried it yet. It may not work.

For one moment I really thought some variation on the following might work.
( (TheListControl.GetType()) TheListControl).DoStuff();
It didn't. I also looked for a specific Function or Method that would cast
something, for similar use, eg:
((TheListControl.GetType().SetClass(TheListControl )).DoStuff();
Didn't find it yet.

Your comments on VB (which I don't use) are interesting, and pertinent,
since the original source code was in VB, and indeed did not differentiate
between the two classes. It is a simple way of creating a ComboBox or
ListBox containing word and color rectangle examples of either all
'KnownColors' or 'SystemColors' or Web Colors. Two short functions. If
anyone wants, I could post the original VB and my C# versions, for interest.

I guess at the end of the day, the difference this makes to my code is
minimal, but I was more interested for the sake of understanding classes and
objects a little better. I thought this might be the sort of thing that OOP
languages did easily.

Thanks for taking the time,

James

<la*******@hotmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

I had been about to post what Carlos did, but then I checked the docs
and found what you did - that there are members which are present in
both ComboBox and ListBox - with identical semantics - but which are
NOT defined in their parent class ListControl. This is arguably an
oversight on the part of the Framework creators, although there might
be a good reason why they did it this way.

As it is, I don't think there's any way around the inelegance of
(pseudo-code)

If it's a combobox
((combobox)it).beginupdate
((combobox)it).items.clear
...
Else
((listbox)it).beginupdate
((listbox)it).items.clear
...

If we were in an environment where late-binding were possible (eg
VBScript, non-Strict VB), *then* we could just say

var.beginupdate
var.items.clear
....

BUT to my mind the performance loss from late binding, and the safety
loss from weak typing are too high a price for the code clarity gains.

Enter the long-winded code and mutter curses at the Framework, is the
best we can do here.

Nov 17 '05 #7
Yes, if the methods are duplicated in ListBox and ComboBox rather than
implemented in the ListControl base, you have to cast to the proper class.
This is the best approach.

There is other approach but it is overkill: you can call methods and
properties by name of any object using Reflection (VB.NET allows to do this
with Option Strict Off too):

// Not tested
Type myType;
myType = myObject.GetType();
myType.InvokeMember("BeginUpdate",...., myObject,...)

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

<la*******@hotmail.com> escribió en el mensaje
news:11**********************@g44g2000cwa.googlegr oups.com...

I had been about to post what Carlos did, but then I checked the docs
and found what you did - that there are members which are present in
both ComboBox and ListBox - with identical semantics - but which are
NOT defined in their parent class ListControl. This is arguably an
oversight on the part of the Framework creators, although there might
be a good reason why they did it this way.

As it is, I don't think there's any way around the inelegance of
(pseudo-code)

If it's a combobox
((combobox)it).beginupdate
((combobox)it).items.clear
...
Else
((listbox)it).beginupdate
((listbox)it).items.clear
...

If we were in an environment where late-binding were possible (eg
VBScript, non-Strict VB), *then* we could just say

var.beginupdate
var.items.clear
....

BUT to my mind the performance loss from late binding, and the safety
loss from weak typing are too high a price for the code clarity gains.

Enter the long-winded code and mutter curses at the Framework, is the
best we can do here.

Nov 17 '05 #8

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

Similar topics

100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
7
by: Arpan | last post by:
The .NET Framework 2.0 documentation states that An Object variable always holds a pointer to the data, never the data itself. Now w.r.t. the following ASP.NET code snippet, can someone please...
5
by: David Palau | last post by:
I'm looking for some guidance on what data type would work best as the result output of a web service function method. This web method will return an object that implements a class that is...
23
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object...
6
by: muckymuck | last post by:
Hello, i would like to know how i can get better output from a foreach. this code: $x = $wpdb->get_results("SELECT * FROM " . WP_PLAATJES); foreach ($x as $y) { print_r($y); var_dump( $y...
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:
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...
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,...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.