473,804 Members | 3,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array os Class

Hi,

I must design a method that receive a Control object and return a single
value if it is a TextBox object, another value if it is a ComboBox object
and so on.

If I try a "if-else-if" loop, this will be very unhappy! Can I use a foreach
to make that? How declare and create the array for use in the operator "is"
(for test a Control, like "cont is TextBox")?

Thanks,

Max
Nov 16 '05 #1
10 1349
Max André Bündchen wrote:
Hi,

I must design a method that receive a Control object and return a single
value if it is a TextBox object, another value if it is a ComboBox object
and so on.

If I try a "if-else-if" loop, this will be very unhappy! Can I use a foreach
to make that? How declare and create the array for use in the operator "is"
(for test a Control, like "cont is TextBox")?

Thanks,

Max


You can't use foreach for things like "all *textboxes* in this list,
and ignore other controls", if that is what you mean.

I don't think you can use "overloadin g" on the method, as you supply
a *Control* (that happens to be a TextBox) instead of a "real" TextBox.

So I think you are stuck with a bunch of if's, probably along the
lines of:

private string ControlValue(Co ntrol myComtrol)
{
TextBox tb = myControl as TextBox;
if (tb != null)
return tb.Text;

ComboBox cbx = myControl as ComboBox;
if (cbx != null)
return cbx.SelectedVal ue; // or whatever the correct property is

// etc.
}
--
Hans Kesting
Nov 16 '05 #2
Hans,
I think using "is" is the better option.
if(_control is TextBox) .
{
//do ur stuff
}

I agree with the rest. If you run a "foreach" over the control collection,
you will have to have as many foreaches as the number of Control Types you
are supporting. This is a better and a faster way.

Ranjan.

--
http://dotnetjunkies.com/weblog/dotnut


"Hans Kesting" <ne***********@ spamgourmet.com > wrote in message
news:Ow******** ******@TK2MSFTN GP11.phx.gbl...
Max André Bündchen wrote:
Hi,

I must design a method that receive a Control object and return a single
value if it is a TextBox object, another value if it is a ComboBox object and so on.

If I try a "if-else-if" loop, this will be very unhappy! Can I use a foreach to make that? How declare and create the array for use in the operator "is" (for test a Control, like "cont is TextBox")?

Thanks,

Max


You can't use foreach for things like "all *textboxes* in this list,
and ignore other controls", if that is what you mean.

I don't think you can use "overloadin g" on the method, as you supply
a *Control* (that happens to be a TextBox) instead of a "real" TextBox.

So I think you are stuck with a bunch of if's, probably along the
lines of:

private string ControlValue(Co ntrol myComtrol)
{
TextBox tb = myControl as TextBox;
if (tb != null)
return tb.Text;

ComboBox cbx = myControl as ComboBox;
if (cbx != null)
return cbx.SelectedVal ue; // or whatever the correct property is

// etc.
}
--
Hans Kesting

Nov 16 '05 #3
> If I try a "if-else-if" loop, this will be very unhappy! Can I use a
foreach

:-)

"Max André Bündchen" <.> wrote in message
news:OC******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

I must design a method that receive a Control object and return a single
value if it is a TextBox object, another value if it is a ComboBox object
and so on.

If I try a "if-else-if" loop, this will be very unhappy! Can I use a foreach to make that? How declare and create the array for use in the operator "is" (for test a Control, like "cont is TextBox")?

Thanks,

Max

Nov 16 '05 #4
Hi,

Yes, you can do it.
Just create a new struct like this:
struct XXX
{
Type ControlType;
string Value;
}

create a collection of it with the controls/values you want to add

ArrayList ar = new ArrayList()

ar.Add( new XXX( typeof( System.Windows. Forms.TextBox, "textbox);
.....

the later you can do your method like this:

string GetValue( object o )
{
foreach( XXX x in ar)
if ( x.ControlType == o.GetType() )
return x.Value;
}
You could do it even without the foreach if you use a Hashtable instead of a
ArrayList

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Max André Bündchen" <.> wrote in message
news:OC******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

I must design a method that receive a Control object and return a single
value if it is a TextBox object, another value if it is a ComboBox object
and so on.

If I try a "if-else-if" loop, this will be very unhappy! Can I use a
foreach to make that? How declare and create the array for use in the
operator "is" (for test a Control, like "cont is TextBox")?

Thanks,

Max

Nov 16 '05 #5
Ignacio,
You still have to have the "if" statements. So whatever you have said,
though technically correct, is an overkill i am afraid.
Ranjan

--
http://dotnetjunkies.com/weblog/dotnut


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:O5******** ******@TK2MSFTN GP11.phx.gbl...
Hi,

Yes, you can do it.
Just create a new struct like this:
struct XXX
{
Type ControlType;
string Value;
}

create a collection of it with the controls/values you want to add

ArrayList ar = new ArrayList()

ar.Add( new XXX( typeof( System.Windows. Forms.TextBox, "textbox);
....

the later you can do your method like this:

string GetValue( object o )
{
foreach( XXX x in ar)
if ( x.ControlType == o.GetType() )
return x.Value;
}
You could do it even without the foreach if you use a Hashtable instead of a ArrayList

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Max André Bündchen" <.> wrote in message
news:OC******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

I must design a method that receive a Control object and return a single
value if it is a TextBox object, another value if it is a ComboBox object and so on.

If I try a "if-else-if" loop, this will be very unhappy! Can I use a
foreach to make that? How declare and create the array for use in the
operator "is" (for test a Control, like "cont is TextBox")?

Thanks,

Max


Nov 16 '05 #6
Hi,

The IF ( or a comparision) will never dissapear, you could only hide it.
even if you use a Hashtable , use object.GetType( ).ToString() as the key
internally the Hashtable will do a IF

Your code may looks much cleaner though.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ranjan" <ra************ *@gmail.com> wrote in message
news:uJ******** ******@TK2MSFTN GP09.phx.gbl...
Ignacio,
You still have to have the "if" statements. So whatever you have
said,
though technically correct, is an overkill i am afraid.
Ranjan

--
http://dotnetjunkies.com/weblog/dotnut


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
wrote
in message news:O5******** ******@TK2MSFTN GP11.phx.gbl...
Hi,

Yes, you can do it.
Just create a new struct like this:
struct XXX
{
Type ControlType;
string Value;
}

create a collection of it with the controls/values you want to add

ArrayList ar = new ArrayList()

ar.Add( new XXX( typeof( System.Windows. Forms.TextBox, "textbox);
....

the later you can do your method like this:

string GetValue( object o )
{
foreach( XXX x in ar)
if ( x.ControlType == o.GetType() )
return x.Value;
}
You could do it even without the foreach if you use a Hashtable instead
of

a
ArrayList

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Max André Bündchen" <.> wrote in message
news:OC******** ******@TK2MSFTN GP10.phx.gbl...
> Hi,
>
> I must design a method that receive a Control object and return a
> single
> value if it is a TextBox object, another value if it is a ComboBox object > and so on.
>
> If I try a "if-else-if" loop, this will be very unhappy! Can I use a
> foreach to make that? How declare and create the array for use in the
> operator "is" (for test a Control, like "cont is TextBox")?
>
> Thanks,
>
> Max
>



Nov 16 '05 #7
Hans Kesting wrote:
TextBox tb = myControl as TextBox;
if (tb != null)
return tb.Text;
Ranjan wrote: I think using "is" is the better option.
if(_control is TextBox) .
{
//do ur stuff
}


I'm reading Jeffrey Richter's "Applied Microsoft .NET Framework Programming"
right now, and he makes an argument on pages 119 and 120 that "as" is a
better option than "is". The argument goes something like this. Each time
you perform an "is", an "as" or a cast, the run-time checks the types
involved for compatibility, and that costs time. When you do an "is", type
checking typically needs to occur twice:

if(myControl is TextBox) //type checking occurs here...
{
TextBox myTextBox=(Text Box) myControl; //...and again here!
//do stuff with myTextBox...
}

Whereas if you do an "as", a type check only occurs once:

TextBox myTextBox=(myCo ntrol as TextBox) //type checking occurs here...
if(myTextBox!=n ull) //...but not here!
{
//do stuff with myTextBox...
}

So theoretically at least, code is more efficient with "as" than with "is"
plus a cast, because a type compatibility check takes more time than simple
null-reference check.

This is probably not very helpful to the person who asked the original
question, but I thought I'd share the point anyway :-)

--
Michal Boleslav Mechura
va******@hotmai l.com
Nov 16 '05 #8
Thanks Michal for the pointer. I seem to have forgot my basics.

Thanks

--
http://dotnetjunkies.com/weblog/dotnut


"Michal Boleslav Mechura" <va******@hotma il.com> wrote in message
news:11******** *******@ns1-ext.dcu.ie...
Hans Kesting wrote:
TextBox tb = myControl as TextBox;
if (tb != null)
return tb.Text;
Ranjan wrote:
I think using "is" is the better option.
if(_control is TextBox) .
{
//do ur stuff
}


I'm reading Jeffrey Richter's "Applied Microsoft .NET Framework

Programming" right now, and he makes an argument on pages 119 and 120 that "as" is a
better option than "is". The argument goes something like this. Each time
you perform an "is", an "as" or a cast, the run-time checks the types
involved for compatibility, and that costs time. When you do an "is", type
checking typically needs to occur twice:

if(myControl is TextBox) //type checking occurs here...
{
TextBox myTextBox=(Text Box) myControl; //...and again here!
//do stuff with myTextBox...
}

Whereas if you do an "as", a type check only occurs once:

TextBox myTextBox=(myCo ntrol as TextBox) //type checking occurs here...
if(myTextBox!=n ull) //...but not here!
{
//do stuff with myTextBox...
}

So theoretically at least, code is more efficient with "as" than with "is"
plus a cast, because a type compatibility check takes more time than simple null-reference check.

This is probably not very helpful to the person who asked the original
question, but I thought I'd share the point anyway :-)

--
Michal Boleslav Mechura
va******@hotmai l.com

Nov 16 '05 #9
but not applicable here :)

--
http://dotnetjunkies.com/weblog/dotnut


"Ranjan" <ra************ *@gmail.com> wrote in message
news:ec******** ******@tk2msftn gp13.phx.gbl...
Thanks Michal for the pointer. I seem to have forgot my basics.

Thanks

--
http://dotnetjunkies.com/weblog/dotnut


"Michal Boleslav Mechura" <va******@hotma il.com> wrote in message
news:11******** *******@ns1-ext.dcu.ie...
Hans Kesting wrote:
TextBox tb = myControl as TextBox;
if (tb != null)
return tb.Text;


Ranjan wrote:
I think using "is" is the better option.
if(_control is TextBox) .
{
//do ur stuff
}


I'm reading Jeffrey Richter's "Applied Microsoft .NET Framework

Programming"
right now, and he makes an argument on pages 119 and 120 that "as" is a
better option than "is". The argument goes something like this. Each time you perform an "is", an "as" or a cast, the run-time checks the types
involved for compatibility, and that costs time. When you do an "is", type checking typically needs to occur twice:

if(myControl is TextBox) //type checking occurs here...
{
TextBox myTextBox=(Text Box) myControl; //...and again here!
//do stuff with myTextBox...
}

Whereas if you do an "as", a type check only occurs once:

TextBox myTextBox=(myCo ntrol as TextBox) //type checking occurs here...
if(myTextBox!=n ull) //...but not here!
{
//do stuff with myTextBox...
}

So theoretically at least, code is more efficient with "as" than with "is" plus a cast, because a type compatibility check takes more time than

simple
null-reference check.

This is probably not very helpful to the person who asked the original
question, but I thought I'd share the point anyway :-)

--
Michal Boleslav Mechura
va******@hotmai l.com


Nov 16 '05 #10

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

Similar topics

4
3770
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
6
1738
by: Buddy Ackerman | last post by:
I created a simple class: Public Class MyTestClass Public Test() As String End Class I tried to assign some values to the array Test() and display them like this:
14
3920
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
3
837
by: michi | last post by:
Hello, I need to initialize a 2 dimensional square arrays of structures. The size of array I get from the user. I can do one-dimensional array, but I don't know how to specify the size of array when I want to do it 2D. I want to do it using 'new' operator. Thanks, michi.
9
4815
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
7
3661
by: Rade | last post by:
Please have a look at the following program: #include <iostream> template <const int array, size_t index> class ArrayIndex { public: static const int value = array; };
4
1531
by: Armand | last post by:
Hi Guys, I have a set of array that I would like to clear and empty out. Since I am using "Array" not "ArrayList", I have been struggling in finding the solution which is a simple prob for those who experience. (For some reason I have to implement Array not ArrayLists) Below are the simple following code: Dim Array() As String Dim intCounter As Integer
23
7424
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
17
7260
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
6
2947
by: npankey | last post by:
I've started experimenting with template metaprogramming in a small project of mine. What I'm trying to accomplish is to generate a static array of templated objects that get specialized based on there position in the array. This should be possible but I can't figure out exactly how to accomplish this. The below code should better illustrate what I'm trying to do: template <int I> class Item
0
9571
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5505
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.