Author Topic: looking for C# equvilent to the following lisp statement  (Read 3655 times)

0 Members and 1 Guest are viewing this topic.

Binky

  • Guest
looking for C# equvilent to the following lisp statement
« on: April 09, 2008, 11:50:55 AM »
Wanted to see how using C# would work for some things, and I just wanted to start learning something new.  I decided to start by creating a few simple calculators, room cavity ratio, volt drop etc..  I have a few of them in autolisp/dcl but dcl will not do tabs and someday (distant future, just working on baby steps for now) I thought having a tabbed dialog that had all of them together and unlike dcl will let me keep it on the screen and still work on the drawings.  Anyway things are fine on the first one except that I am not sure how to test if a variable actually has a value assigned or not.  Here is an example of what I got started at home for this and a autolisp version of what I need a C# version for.

private void button1_Click()
   float var1 = float.Parse(textBox1.Text);
   float var2 = float.Parse(textBox2.Text);
   float var3 = float.Parse(textBox3.Text);

looking for C# equivalent to the following lisp statement

(if (and (var1)(var2)(var3))
   (True do this)
   (Nil do that))

Basically if a the button is hit and they forgot to fill in a field I want to admonish them for that.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: looking for C# equvilent to the following lisp statement
« Reply #1 on: April 09, 2008, 12:09:04 PM »
You can test to see how long the string is.  You can compare the string to an empty string and see if they are equal.  And you can test them all in one if statement also.

Code: [Select]
if (
string.Compare( var1, string.Empty ).Equals( 0 )
&&
string.Compare( var2, string.Empty ).Equals( 0 )
&&
string.Compare( var3, string.Empty ).Equals (0 )
) {

This will see if all the string are empty.  If you want to make sure they are not empty, you can put the '!' in front.  Its like adding a 'not' to the front of an 'and' statement to test of falseness.

Code: [Select]
if (
var1.Length.Equals( 0 )
&&
var2.Length.Equals( 0 )
&&
var3.Length.Equals( 0 )
) {

This will tell you if all the lengths are 0, or null strings. Same as above to test the opposite with the '!' character.

Edit:  There is a .Net forum, and maybe that is a better place for this since it is specific to C#.  /idea
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

sinc

  • Guest
Re: looking for C# equvilent to the following lisp statement
« Reply #2 on: April 09, 2008, 08:32:29 PM »
Note that

Code: [Select]
var1.Length.Equals( 0 )
will probably crash your program on an unhandled access violation error if var1 is null.

If you use this instead, you avoid that potential problem:

Code: [Select]
String.IsNullOrEmpty(var1)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: looking for C# equvilent to the following lisp statement
« Reply #3 on: April 09, 2008, 10:10:02 PM »
I would use a bitwise value to hold the state of each textbox
Then in the events for the textbox lose focus add or subtract the bitwise value for that textbox ...

Check the value of the textbox against String.IsNullOrEmpty(TextBox1.Text) then based on the value set the proper bit.
ParseForm = ParseForm +/- 1, 2 or 4

then when clicking the button to parse the form, if the value of ParseForm != 7 then alert the user that they need to fill in the proper values according to 7 ^ ParseForm ...

I hope that makes sense ...

I was going to post some code, ....well I did by accident then promptly deleted it ... I was pasting in the quick reply box and hit enter by mistake .. so I deleted it to fix it .. got busy on another something else ...and .. well... this is what you get for now.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Binky

  • Guest
Re: looking for C# equvilent to the following lisp statement
« Reply #4 on: April 09, 2008, 10:48:37 PM »
Thanks guys for the responses.

Late night at work, so I will have to dig into again later. 

Looks like I need to test textbox strings prior to the Parse.float statements as it seems that 'Null' crashes the Parse statement.

Kieth, I think I got what you are saying. that would make for a easy test expression to keep the button deactivated until the necessary fields were filled in. after every 'OnChange' run the test and activate if ParseForm = 7 (in the case here at least)

Thanks again guys

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: looking for C# equvilent to the following lisp statement
« Reply #5 on: April 09, 2008, 11:22:15 PM »
Correct ...

Use the bitwise functions to add and subtract ..

Code: [Select]
int TB1_VALID = 1
int TB2_VALID = 2
int TB3_VALID = 4

if String.IsNullOrEmpty(TextBox1.Text) == true
{
     ParseForm = ParseForm & TB1_VALID
}
else
{
     ParseForm = ParseForm | TB1_VALID
}

or something like that ..

then check that all items are valid

Code: [Select]
if ParseForm == TB1_VALID|TB2_VALID|TB3_VALID //or if ParseForm == 7
{
    //do stuff here
}
else
{
    //notify user here
}


I hope I got that right .. it is late
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2141
  • class keyThumper<T>:ILazy<T>
Re: looking for C# equvilent to the following lisp statement
« Reply #6 on: April 10, 2008, 01:23:37 AM »
You may also want to have a look at the  Control.CausesValidation Property
 and the examples (for same) in the help docs.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Binky

  • Guest
Re: looking for C# equvilent to the following lisp statement
« Reply #7 on: April 23, 2008, 07:18:18 PM »
Finally had a chance to get back to this thing.  I spent some time writing a function to verify that the user input was going to be valid, checked to make sure it was not empty via...

string.IsNullOrEmpty(RmAreaTextBox.Text)

Then wanted to verify that only numbers were being input via... (wrapped in a loop based on length of string)

char.IsDigit(RmAreaTextBox.Text index)

And everything was fine, no empty strings to crash when I parse to a double and no letters either, cool.  However a decimal point is going to be needed as valid input, now what, test the for the point too?  This can't be the right way, can it?  No, I am new and just don't have the experience to know even where in the books to look for what I want as yet.  So imagine how happy I was when I stumbled across   "double.tryparse"  Returns true/false depending if the attempt to parse a string to a double succeeded or not and will further be kind enough to set the output, if valid, to a variable for you at the same time.  This method made my entire function of checking the input as described above unnecessary by replacing it with...

            double.TryParse(RmAreaTextBox.Text, out RmArea)

Made my life simpler and thought I would share.

Now if I can just get the focus to start on a specific box on starting, it would be completed. I have the "load" event on the form itself flagged to this...

        private void RmCavForm_Load(object sender, EventArgs e)
        {
            RmWidthTextBox.Focus();
        }

But the focus does not start there.  I can change the focus to that control in other places, for example a button that clears the fields so the user can have another go.  but it just wont start with the focus there.  Thats probably another thread.


Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: looking for C# equvilent to the following lisp statement
« Reply #8 on: April 23, 2008, 07:49:55 PM »
In the GUI editor, try setting the taborder for the textbox to 1
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Binky

  • Guest
Re: looking for C# equvilent to the following lisp statement
« Reply #9 on: April 23, 2008, 08:12:08 PM »
Checked that, already was.  I added a line to put some text into another box with the focus statement to see if the function was even being called.  It is, so maybe the focus is being changed again afterwards.

Again, being new to this, I have to ask.  Is there another event that takes place after the form loads?

All that is in the form.cs file are functions to handle events and the variable declarations.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: looking for C# equvilent to the following lisp statement
« Reply #10 on: April 23, 2008, 09:34:22 PM »
Try this then ...
Shown is the final event for a form when it is displayed. If the focus does not work here, then I don't know what else it could be, unless you have another control set to the default control
Code: [Select]
private void RmCavForm_Shown(Object sender, EventArgs e)
{
    RmAreaTextBox.Focus();
}

or

Code: [Select]
private void RmCavForm_Shown(Object sender, EventArgs e)
{
  this.activeControl = RmAreaTextBox;
}


other than that I got nothing
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Binky

  • Guest
Re: looking for C# equvilent to the following lisp statement
« Reply #11 on: April 24, 2008, 12:26:14 AM »
Shown was the trick. Thanks!!!!!!!

Not knowing for sure which files (there are plenty) I zipped them all

Not ground breaking but my first C# application, granted a Room Cavity Ratio calculator is of limited use, but we do alot of work out in California and end up needing it on the Title 24 forms.

Now just to figure out how to post it.... ah 'additional options....here it is. 

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8728
  • AKA Daniel
Re: looking for C# equvilent to the following lisp statement
« Reply #12 on: April 24, 2008, 11:16:44 AM »
Nice Work!

Binky

  • Guest
Re: looking for C# equvilent to the following lisp statement
« Reply #13 on: April 25, 2008, 01:57:09 PM »
Nice Work!

Thanks, I think I learned more about using Studio Express and object browser then anything else.  Need to know the tools too.

Keith, thanks to you again for the help.

I gotta ask though. how does the complier determine where the initial focus will be?  There were 8 textboxes and they were identical except for name, location, and tab order.  Obviously it did not use tab order.  Just curious.  Now that I think about it, the one it did chose would be the first alphabetically. I wonder?

Anyway have a good weekend folks!!