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 10 1315
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 "overloading" 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(Control myComtrol)
{
TextBox tb = myControl as TextBox;
if (tb != null)
return tb.Text;
ComboBox cbx = myControl as ComboBox;
if (cbx != null)
return cbx.SelectedValue; // or whatever the correct property is
// etc.
}
--
Hans Kesting
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**************@TK2MSFTNGP11.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 "overloading" 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(Control myComtrol) { TextBox tb = myControl as TextBox; if (tb != null) return tb.Text;
ComboBox cbx = myControl as ComboBox; if (cbx != null) return cbx.SelectedValue; // or whatever the correct property is
// etc. }
-- Hans Kesting
> 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**************@TK2MSFTNGP10.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
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**************@TK2MSFTNGP10.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
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.machin AT dot.state.fl.us> wrote
in message news:O5**************@TK2MSFTNGP11.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**************@TK2MSFTNGP10.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
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**************@TK2MSFTNGP09.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.machin AT dot.state.fl.us> wrote in message news:O5**************@TK2MSFTNGP11.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**************@TK2MSFTNGP10.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 >
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=(TextBox) myControl; //...and again here!
//do stuff with myTextBox...
}
Whereas if you do an "as", a type check only occurs once:
TextBox myTextBox=(myControl as TextBox) //type checking occurs here...
if(myTextBox!=null) //...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******@hotmail.com
Thanks Michal for the pointer. I seem to have forgot my basics.
Thanks
-- http://dotnetjunkies.com/weblog/dotnut
"Michal Boleslav Mechura" <va******@hotmail.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=(TextBox) myControl; //...and again here! //do stuff with myTextBox... }
Whereas if you do an "as", a type check only occurs once:
TextBox myTextBox=(myControl as TextBox) //type checking occurs here... if(myTextBox!=null) //...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******@hotmail.com
but not applicable here :)
-- http://dotnetjunkies.com/weblog/dotnut
"Ranjan" <ra*************@gmail.com> wrote in message
news:ec**************@tk2msftngp13.phx.gbl... Thanks Michal for the pointer. I seem to have forgot my basics.
Thanks
-- http://dotnetjunkies.com/weblog/dotnut
"Michal Boleslav Mechura" <va******@hotmail.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=(TextBox) myControl; //...and again here! //do stuff with myTextBox... }
Whereas if you do an "as", a type check only occurs once:
TextBox myTextBox=(myControl as TextBox) //type checking occurs here... if(myTextBox!=null) //...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******@hotmail.com
Hans,
Funny, I don't think you can use "overloading" on the method, as you supply a *Control* (that happens to be a TextBox) instead of a "real" TextBox.
You reply on an answer in the general newsgroup on this multiposted message.
Cor This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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:
|
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>
|
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...
|
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...
|
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;
};
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
|
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: 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,...
|
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...
|
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: 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,...
| |