473,396 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

IF THEN & this & that etc before ELSE ...VB6 (Newby)

Hi Guys,
I've looked for this and can't find anything relative.
My problem is this :-
I want to set more than one value as the result of an IF function, (I seem to remember being able to do this with BBC Basic) but everything I've tried returns an error.
can someone point me in the right direction?
regards
Mar 12 '07 #1
15 2814
You can follow this code

Expand|Select|Wrap|Line Numbers
  1.  
  2. ****************Simple If End If**************************
  3.  if condition then
  4.     statement
  5.  else
  6.    statement
  7.  end if
  8. ****************Multiple If **********************
  9.  if condition then
  10.    statement
  11.  elseif condition then
  12.    statement
  13.  elseif condition then
  14.   statement
  15.  else
  16.   statement
  17.  end if
  18.  
  19. ************************************************
  20.  
  21.  
  22.  
Mar 12 '07 #2
Thankyou! I'll give it a go. while Im on, every time I finish with 'End If', when I run the program It stops at that point with the statement "Compile error: End If without block If", having said that it seems to run ok without putting it in...what am I missing?
regards
Mar 12 '07 #3
willakawill
1,646 1GB
Hi. a couple of things. Firstly it may be better for you to use a select case statement if you are checking only one variable as in:
Expand|Select|Wrap|Line Numbers
  1. Select Case TheVariable
  2.    Case 1
  3.       'do something
  4.  
  5.    Case 2
  6.       'do something
  7.  
  8.    Case 3
  9.       'do something
  10.  
  11.    Case 4
  12.       'do something
  13.  
  14.    Case Else
  15.       'do something
  16. End Select
Secondly, regarding the problem with your if statement, you need to post that code here so we can go over it together.
Mar 12 '07 #4
Brilliant!
I didn't know there was a 'Case' statement
Thanks that'll sort it.

Here's the code thats doesn't seem to be going by the book.


‘Cill is the Checkbox


(This Works)

If Cill.Value = 1 Then Height = Height - 30 _
Else: Height = Height



(This Don’t)

If Cill.Value = 1 Then Height = Height - 30 _
Else: Height = Height _
End If

regards
Mar 12 '07 #5
willakawill
1,646 1GB
The part that doesn't work does not need an End If statement because you have put the code inline and not on a separate line.
Mar 12 '07 #6
Killer42
8,435 Expert 8TB
...
If Cill.Value = 1 Then Height = Height - 30 _
Else: Height = Height _
End If
You've got some mighty strange coding habits going, there. What you've done is to write a complex single-line statement, and break it over multiple lines. In other words, thanks to the "_" continuation characters, your single statement actually says
Expand|Select|Wrap|Line Numbers
  1. If Cill.Value = 1 Then Height = Height - 30 Else: Height = Height End If
Trust me, there is no longer any need to try and cram multiple statements into a single line like this. I highly recommend you consider the following changes.
  • As willakawill pointed out, you don't use End If for a single-line or "inline" If statement.
  • Lose the colon. Very rarely needed in VB, and in this case completely unnecessary
  • Split your If statement and indent the dependent code - much easier to read.
  • Height = Height ??? What the... :confused:
The result would look something like this...
Expand|Select|Wrap|Line Numbers
  1. If Cill.Value = 1 Then
  2.   Height = Height - 30
  3. Else
  4.   Height = Height
  5. End If
Mar 13 '07 #7
[maybe you can add this

Do
if condition then
codes
end if
exit do
...
...
...
...
.

until condition
if a certain condition was met in a if statement it will run that particular statement, if not it will go to exit then to next if.....

i hope this could help ya....
Mar 13 '07 #8
Killer42
8,435 Expert 8TB
Yes, I suppose if you can't be bothered writing a proper structure you can always fall back on the old GoTo statement - or code a more complex equivalent as in this case .
Mar 13 '07 #9
Wow!!
Ok Thanks Guys, I've embarrassed myself again..oh well

in my defence I picked up the book about 3 days ago and ended up realising its the wrong one (Visual Basic 2005 for Dummies) so that ended up in the bin.
I've been using the Internet for reference, and a very basic knowledge of BBC Basic 25 years ago.
by the way I didn't put the Colon in, I watched as it was added automatically.

Thanks all of you for helping, I've learnt a lot from it and Its helping me a lot, I'm not giving up!

Best regards
Mar 13 '07 #10
The part that doesn't work does not need an End If statement because you have put the code inline and not on a separate line.
Thanks willakawill, I've grasped it now, I've realised that the "Then" statement has to be on the "If" line; I was putting it on the next line and that was screwing everything else up, unless I used the Underscore; Now its in the right place I'm making sense out of everything you've said.
Mar 13 '07 #11
You've got some mighty strange coding habits going, there. What you've done is to write a complex single-line statement, and break it over multiple lines. In other words, thanks to the "_" continuation characters, your single statement actually says
Expand|Select|Wrap|Line Numbers
  1. If Cill.Value = 1 Then Height = Height - 30 Else: Height = Height End If
Trust me, there is no longer any need to try and cram multiple statements into a single line like this. I highly recommend you consider the following changes.
  • As willakawill pointed out, you don't use End If for a single-line or "inline" If statement.
  • Lose the colon. Very rarely needed in VB, and in this case completely unnecessary
  • Split your If statement and indent the dependent code - much easier to read.
  • Height = Height ??? What the... :confused:
The result would look something like this...
Expand|Select|Wrap|Line Numbers
  1. If Cill.Value = 1 Then
  2.   Height = Height - 30
  3. Else
  4.   Height = Height
  5. End If
Thanks Killer42
I was looking at your code when I realised what I was doing wrong; I've been assuming that the "If & Then" statements could be put on seperate lines, which dosent work, and then found that if I put everything on one line it worked; having read that it's recommended to keep the lines short, I broke it up with the Underscore and then had a problem with "End If".
Now I know to finish the "If" line with "Then", everything else falls into place.
Thankyou!
("Height" is a variable defined by an input in the window frame pricing prog I'm trying to make, - 30 is the thickness of the Cill if required)
Mar 13 '07 #12
Killer42
8,435 Expert 8TB
Thanks Killer42
I was looking at your code when I realised what I was doing wrong; I've been assuming that the "If & Then" statements could be put on seperate lines, which dosent work, and then found that if I put everything on one line it worked; having read that it's recommended to keep the lines short, I broke it up with the Underscore and then had a problem with "End If".
Now I know to finish the "If" line with "Then", everything else falls into place.
Thankyou!
("Height" is a variable defined by an input in the window frame pricing prog I'm trying to make, - 30 is the thickness of the Cill if required)
It sounds as though you're doing fine - you're not embarrassing yourself. Just keep in mind...
  1. There's no such thing as a "Then statement". It's just a required part of the If statement.
  2. There are subtle differences between a single-line statement and a multi-line "statement block". Packing multiple statements onto a line just confuses the issue. It's usually (though not necessarily always) simpler, cleaner and easier to read if you stick to a single statement per line.
  3. The underscore simply allows you to visually break up a long statement so that you can see it all without scrolling all over the place. It is still treated as a single statement. For example, to take it to ridiculous extremes you could calculate 2 + 3 like so...
    Expand|Select|Wrap|Line Numbers
    1. A _
    2. = _
    3. 2 _
    4. + _
    5. 3
  4. The point I was making about Height is just that...
    Expand|Select|Wrap|Line Numbers
    1. Else
    2.   Height = Height
    does absolutely nothing. But uses up some processor cycles to do it. :)
Mar 13 '07 #13
It sounds as though you're doing fine - you're not embarrassing yourself. Just keep in mind...
  1. There's no such thing as a "Then statement". It's just a required part of the If statement.
  2. There are subtle differences between a single-line statement and a multi-line "statement block". Packing multiple statements onto a line just confuses the issue. It's usually (though not necessarily always) simpler, cleaner and easier to read if you stick to a single statement per line.
  3. The underscore simply allows you to visually break up a long statement so that you can see it all without scrolling all over the place. It is still treated as a single statement. For example, to take it to ridiculous extremes you could calculate 2 + 3 like so...
    Expand|Select|Wrap|Line Numbers
    1. A _
    2. = _
    3. 2 _
    4. + _
    5. 3
  4. The point I was making about Height is just that...
    Expand|Select|Wrap|Line Numbers
    1. Else
    2.   Height = Height
    does absolutely nothing. But uses up some processor cycles to do it. :)
I appreciate the help;
Does this mean that you can use 'If & Then' without the 'Else'?, or put the 'Else' in without specifying anything after it?; I've been concerned that unless I tell it to revert back to the former value of "Height" for any other condition, it will remain the value of the 'If' line.
regards
Mar 14 '07 #14
Killer42
8,435 Expert 8TB
I appreciate the help;
Does this mean that you can use 'If & Then' without the 'Else'?, or put the 'Else' in without specifying anything after it?; I've been concerned that unless I tell it to revert back to the former value of "Height" for any other condition, it will remain the value of the 'If' line.
regards
  • The Else is completely optional on an If statement. If you don't use it, you are effectively saying "Else, do nothing".
  • You cannot simply write an Else statement - it has to follow an If.
  • If the If condition is not true, then nothing will happen.
It might be a good idea to play with the whole If structure for a while, just to ensure that you're completely comfortable with it. Test before and after values, that sort of thing. I think this would be time well spent, as it's a critically important programming structure that you will use a lot if you go continue coding.

Don't forget, VB has an excellent debugging facility which allows you to stop and continue the code, examine and change values on the fly, and so on. You should get into the habit of using the debugging functions, they'll make your life much easier.

Also, make sure you're familiar with the notation used in the manual/help to describe statement syntax, so you can identify the optional parts, repeating parts, etc.
Mar 14 '07 #15
Got It!
Thanks again
Mar 14 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Damien | last post by:
I have just built a simple stopwatch application, but when i f5 to get things goings i get this message, An unhandled exception of type 'System.ArithmeticException' occurred in...
2
by: curwen | last post by:
Hi, I have problem to create a well formed xsl-fo document using images dynamically generated from an http request using this tag: <fo:external-graphic content-type="content-type:image/gif"...
4
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following...
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
10
by: Fred Nelson | last post by:
Hi: I have programmed in VB.NET for about a year and I'm in the process of learing C#. I'm really stuck on this question - and I know it's a "newby" question: In VB.NET I have several...
1
by: Fred Nelson | last post by:
I'm a newby and I'm writing my first VB.NET print program! I need to change the default left and right margins for my printed output "e" is my "System.Drawing.Printing.PrintPageEventArgs By...
6
by: cj | last post by:
I'm receiving an xml formatted string that I pull data from by reading it into an xml document like this: Dim doc As New Xml.XmlDocument doc.LoadXml(respstr) Dim co_name As Xml.XmlNodeList =...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
0
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...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
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...
0
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,...

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.