Basic questions of a new immigrant from VB6..

  • Thread starter Thread starter Geri Reshef
  • Start date Start date
G

Geri Reshef

I'm trying to get into C# after a long love affair with VB6..

1. In VB I had the 'iif' and the 'isnull' functions which helped me to easily constract dynamic SQL statements. For example:
S="Select * From MyTable" & iif(isnull(MyControl),""," Where MyField=" & MyControl)
How can I do that in C#? what are the equivalents of iif and isnull?

2. Is there an equivalent for NZ function (the function NZ(X,0) returns the value of X if it is not null and 0 if it is null).

3. I wrote a simple event for the combobox- MyCombo_SelectedIndexChanged which will run after I select a value (I initialize the combo when I open the form with a DataSet).
Unfotunately the procedures runs during the initialization because it automativally receives the value of the first item in the dataset. How can I prevent it from running (I want it to run only when I manually change the value)?
 
I thought you could use something like variable = (expression? iftrue : iffalse);

this.textbox1.text = ( count == 1? "its one " : "its not one" );


I'm trying to get into C# after a long love affair with VB6..

1. In VB I had the 'iif' and the 'isnull' functions which helped me to easily constract dynamic SQL statements. For example:
S="Select * From MyTable" & iif(isnull(MyControl),""," Where MyField=" & MyControl)
How can I do that in C#? what are the equivalents of iif and isnull?

2. Is there an equivalent for NZ function (the function NZ(X,0) returns the value of X if it is not null and 0 if it is null).

3. I wrote a simple event for the combobox- MyCombo_SelectedIndexChanged which will run after I select a value (I initialize the combo when I open the form with a DataSet).
Unfotunately the procedures runs during the initialization because it automativally receives the value of the first item in the dataset. How can I prevent it from running (I want it to run only when I manually change the value)?
 
Geri Reshef said:
what are the equivalents of iif and isnull?

IIf(a, b, c) becomes a ? b : c
To test whether a variable is null, simply use if (x == null) { ... }
Is there an equivalent for NZ function
(the function NZ(X,0) returns the value of X
if it is not null and 0 if it is null).

Convert.ToInt32(x) will return zero if x is null, else the integer
value of x. Alternatively, you could use (x == null) ? 0 : x

Not sure about your third question.

P.
 
Regarding your third question, one solution is to have a boolean member
variable declared in the form:

protected boolean isLoading;

Initialize this to True in the constructor just before the
InitializeComponents() call, and set it to false at the end of the form
load.

In your SelectedIndexChanged event handler, just put your code inside an "if
(this.isLoading == false)" block. That way the code won't run when the form
is initializing.

If you see the event firing at other unwanted times then you will have other
places where you'll have to turn isLoading on or off, perhaps around your
data binding code or in other events. The basic idea is to have a global
flag that all your *Changed event handlers can look to to see if they are
responding to situations you really want them to.

I haven't yet explored other ways -- if any one knows a better one I'd be
all ears. If there were a more generic way to distinguish interactive from
programmatic changes to a control's value, that would be great, but offhand
I am not aware of any.

--Bob

I'm trying to get into C# after a long love affair with VB6..

3. I wrote a simple event for the combobox- MyCombo_SelectedIndexChanged
which will run after I select a value (I initialize the combo when I open
the form with a DataSet).
Unfotunately the procedures runs during the initialization because it
automativally receives the value of the first item in the dataset. How can I
prevent it from running (I want it to run only when I manually change the
value)?
 
Back
Top