Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

VB.net Ability for single line code

Hi, I would like to have multiple line code in just one line, Like I am able to do in other software that I am familiar with.

Example:

Code
If AetherTextbox5.Text Is "" Then
            AetherTextbox5.Text = "0"
        End If

I would like to turn it into

Code
If AetherTextbox5.Text Is "" Then AetherTextbox5.Text = "0" End If

Is this possible?
Check out my Youtube Channel to support my Computer Tutorials!

[Image: D1daWfF.png]

It's possible but people will make fun of you.
(Oct 10, 2015 11:42 PM)oυтlaw Wrote:  It's possible but people will make fun of you.

+1. I was going to comment that it's stupid and pointless. Visual Studio holds your hand while you code VB.net anyways. Lmao
Code
If AetherTextbox5.Text = "" Then AetherTextbox5.Text = "0"

OR:

Code
AetherTextBox5.Text = If(AetherTextBox5.Text = "", AetherTextBox5.Text = "0", AetherTextBox5.Text)

OR (please don't ever use this method, just demonstrating that it works):

Code
If AetherTextBox5.Text = "" Then : AetherTextBox5.Text = "0" : End If

My personal preference though would be something like..

Code
If String.IsNullOrEmpty(AetherTextBox5.Text) Then
     AetherTextBox5.Text = "0"
End If

It really depends on your particular scenario though, I assume you are doing this to multiple textboxes in which case you should probably create a helper function to reduce code redundancy.

Edit:
Don't use 'Is' to compare strings, it's only by magic .NET hackery that your usage of it actually works. In all other cases if you were to initiate a new reference type and use 'Is' it would return false even if they were initiated with the same value. Instead, you should be using '='. :)
What would a helper function look like?

Code
If AetherTextbox5.Text = String.Empty Then
            AetherTextbox5.Text = "0"
        End If

        If AetherTextbox6.Text = String.Empty Then
            AetherTextbox6.Text = "0"
        End If
        If AetherTextbox7.Text = String.Empty Then
            AetherTextbox7.Text = "0"
        End If
        If AetherTextbox8.Text = String.Empty Then
            AetherTextbox8.Text = "0"
        End If
        If AetherTextbox9.Text = String.Empty Then
            AetherTextbox9.Text = "0"
        End If
        If AetherTextbox3.Text = String.Empty Then
            AetherTextbox3.Text = "0"
        End If
        If AetherTextbox11.Text = String.Empty Then
            AetherTextbox11.Text = "0"
        End If
        If AetherTextbox10.Text = String.Empty Then
            AetherTextbox10.Text = "0"
        End If
        If AetherTextbox12.Text = String.Empty Then
            AetherTextbox12.Text = "0"
        End If
        If AetherTextbox13.Text = String.Empty Then
            AetherTextbox13.Text = "0"
        End If
        If AetherTextbox14.Text = String.Empty Then
            AetherTextbox14.Text = "0"
        End If

This is what my redundant script looks like lol
Check out my Youtube Channel to support my Computer Tutorials!

[Image: D1daWfF.png]

Create a universal handler and cast the sender to type TextBox. Or if this is for all, just loop through your form's controls (but this won't account for child controls) of type TextBox, then run your check.

To reiterate what Aeon said, never use the colon in place of a new line.
If you want you'r code to look smaller you can use :
Code
If AetherTextbox5.Text = String.Empty Then AetherTextbox5.Text = "0"
        If AetherTextbox6.Text = String.Empty Then AetherTextbox6.Text = "0"
        If AetherTextbox7.Text = String.Empty Then AetherTextbox7.Text = "0"
        If AetherTextbox8.Text = String.Empty Then AetherTextbox8.Text = "0"
        If AetherTextbox9.Text = String.Empty Then AetherTextbox9.Text = "0"
        If AetherTextbox3.Text = String.Empty Then AetherTextbox3.Text = "0"
        If AetherTextbox11.Text = String.Empty Then AetherTextbox11.Text = "0"
        If AetherTextbox10.Text = String.Empty Then AetherTextbox10.Text = "0"
        If AetherTextbox12.Text = String.Empty Then AetherTextbox12.Text = "0"
        If AetherTextbox13.Text = String.Empty Then AetherTextbox13.Text = "0"
        If AetherTextbox14.Text = String.Empty Then AetherTextbox14.Text = "0"
I had a thought after reading what you were actually doing. Why not set the Text property of your TextBoxes to 0 or even better, use a NumericUpDown if the input is numeric.
(Oct 10, 2015 11:52 PM)6e756c6c Wrote:  
Code
If AetherTextbox5.Text = "" Then AetherTextbox5.Text = "0"

OR:

Code
AetherTextBox5.Text = If(AetherTextBox5.Text = "", AetherTextBox5.Text = "0", AetherTextBox5.Text)

OR (please don't ever use this method, just demonstrating that it works):

Code
If AetherTextBox5.Text = "" Then : AetherTextBox5.Text = "0" : End If

My personal preference though would be something like..

Code
If String.IsNullOrEmpty(AetherTextBox5.Text) Then
     AetherTextBox5.Text = "0"
End If

It really depends on your particular scenario though, I assume you are doing this to multiple textboxes in which case you should probably create a helper function to reduce code redundancy.

Edit:
Don't use 'Is' to compare strings, it's only by magic .NET hackery that your usage of it actually works. In all other cases if you were to initiate a new reference type and use 'Is' it would return false even if they were initiated with the same value. Instead, you should be using '='. :)
but why we can't use this method
i am used this method some time !!!
Code
some code Then===> : <== some code ===> : <==  some code
(Oct 11, 2015 11:11 PM)nazist Wrote:  but why we can't use this method
i am used this method some time !!!
Code
some code Then===> : <== some code ===> : <==  some code

Because it doesn't have any effect on your code, it will still be treated as if the lines of code were on separate lines and it makes code extremely difficult to read / maintain. I promise if you work on any kind of major project and have to go back later on to update it, dealing with all those condensed lines is going to be a nightmare.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by Formula 1Dec 6, 2019 01:11 AM 23 706 Dec 6, 2019 01:11 AM
Last Post: itcorp
[Java] by RaitzNov 4, 2017 02:04 PM 7 183 Nov 4, 2017 02:04 PM
Last Post: ZeratulTheWizard
[.NET] by ShikatanaiSep 8, 2017 01:20 AM 8 383 Sep 8, 2017 01:20 AM
Last Post: th3s3cr3t0n3