-
Sep 25th, 2014, 07:12 AM
#1
Thread Starter
New Member
If test logic help please
Hi all,
Ive never used VB before and im struggling with a simple If Conditional test.
Basically i have a dropdownList control called drpArea and i want to say if the user selects a value from the DDL then take the value and assign it to the object Organisation Area Id. But i want to be able to reset it if the user doeasnt choose an option. Heres what i got so far...
If drpAdd.SelectedValue.Length > 0 Then Organisation.AddID = drpAdd.SelectedValue
Else If drpAdd.SelectedValue.Length = 0 Then Organisation.AddID = drpAdd.SelectedValue
End If
Visual studio is throwing me errors.
Any help please?
-
Sep 25th, 2014, 07:28 AM
#2
Re: If test logic help please
Welcome to the forums.
What errors and on which line of code?
I suspect the problem is that you're checking the length of SelectedValue and I'm guessing you think that SelectedValue will be an empty string if nothing is selected. It won't . It'll be Nothing and Nothing doesn't have a length. Probably you need to change your check to
Code:
If drpAdd.SelectedValue Is Not Nothing
You also don't need an Else If for this. Just use Else.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Sep 25th, 2014, 07:33 AM
#3
Thread Starter
New Member
Re: If test logic help please
Hi Thanks very much for the reply when i write the following code (below) i get a blue line Under the code below the else and it says property access must assign to the property or use its value?
If drpAdd.SelectedValue.Length > 0 Then Organisation.AddID = drpAdd.SelectedValue
Else
drpAdd.SelectedValue.Length Is Not Nothing Then Organisation.AddID = "" drpAdd.SelectedValue
End If
Many Thanks
-
Sep 25th, 2014, 08:27 AM
#4
Re: If test logic help please
You only need to include the condition if you're using an if (or else if). By moving it to be an else you've removed the need to include the condition. So your code should be:-
Code:
If drpAdd.SelectedValue.Length > 0 Then
Organisation.AddID = drpAdd.SelectedValue
Else
Organisation.AddID = drpAdd.SelectedValue
End If
By the way, I'm not sure you need the if at all. You want to set AddID to the selected value if a value is selected and nothing if no value is selected. So just set it to drpAdd.SelectedValue which will be nothing of no value is selected. Or am I missing something there?
Also, I noticed you posted your orginal thread twice, probably becasue it didn't appear immediately. That'll be becasue we manually moderate the first few posts a member makes just to make sure they're not spammers. I've deleted the duplicate and, if you notice a few of your posts don't appear straight away, don't worry. They'll appear soon enough when a mod checks them.
edit> actually, your condition should probalby read: If drpAdd.SelectedValue is not nothing. There's no need to check it's length which is just an unnecessary overhead.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|