how to break out of "if" block in VB.NET - asp.net

How can I break out of an if statement?
Exit only works for "for", "sub", etc.

In VB.net:
if i > 0 then
do stuff here!
end if
In C#:
if (i > 0)
{
do stuff here!
}
You can't 'break out' of an if statement. If you are attempting this, your logic is wrong and you are approaching it from the wrong angle.
An example of what you are trying to achieve would help clarify, but I suspect you are structuring it incorrectly.

There isn't such an equivalent but you should't really need to with an If statement. You might want to look into using Select Case (VB) or Switch (C#) statements.

In C# .NET:
if (x > y)
{
if (x > z)
{
return;
}
Console.Writeline("cool");
}
Or you could use the goto statement.

You can use
bool result = false;
if (i < 10)
{
if (i == 7)
{
result = true;
break;
}
}
return result;

I have to admit, that in some cases you really wanna have something like an exit sub or a break. On a rare occasion is I use "Goto End" and jump over the "End If" with the def. End:

I know this is an old post but I have been looking for the same answer then eventually I figured it out
try{
if (i > 0) // the outer if condition
{
Console.WriteLine("Will work everytime");
if (i == 10)//inner if condition.when its true it will break out of the outer if condition
{
throw new Exception();
}
Console.WriteLine("Will only work when the inner if is not true");
}
}
catch (Exception ex)
{
// you can add something if you want
}
`

Related

Selenium WD | Trying to use an 'If' statement with logical 'or'

I have a step in my test that goes into several html pages and looks for an element on the screen. That element can have 2 different CSS class names while looking the same in the website (visually speaking) , I have to use an if statement with a logical 'or' to identify them
if (Status == driver.findElement(By.cssSelector(".inlineblock.redClockBigIcon.middle.isOpenExchBig-2")) || Status == driver.findElement(By.cssSelector(".inlineblock.redClockBigIcon.middle.isOpenExchBig-1")));
System.out.println("Stock is closed");)
I expected that if one of the 2 elements would appear, the Eclipse would
recognize it. Well - The second element out of the 2 appeared - and for some reason I've got an exception error. The if statement gave attention only to the first condition in the if, and ignored the second.
org.openqa.selenium.NoSuchElementException: no such element:
locate element: {"method":"css
selector","selector":".inlineblock.redClockBigIcon.middle.isOpenExchBig-2"}Unable to
How can I make the || to work in this 'if' statement?
Thanks
Screenshots of the elements
In your above logic, you have Status which is an already existing WebElement that you're comparing against another Webelement that you're looking up. I don't think this is what your intention was so I'm going to make some assumptions in a solution.
First: Find all of the elements that might exist with your desired selector (Note I'm using findElements instead of findElement)
List<WebElement> clockIconThingies = driver.findElements(By.cssSelector(".inlineblock.redClockBigIcon.middle.isOpenExchBig-2, .inlineblock.redClockBigIcon.middle.isOpenExchBig-1"));
Second: Check if that found anything
if(clockIconThingies.size() > 0)
{
System.out.println("Stock is closed");
}
Alternatively for your css selector, from the image it looks like you might not need to do an or at all and just look for the class redClockBigIcon like this:
List<WebElement> clockIconThingies = driver.findElements(By.cssSelector(".redClockBigIcon"));
You can try using try catch block:
boolean isFirstElemPresent = true;
try{
driver.findElement(By.cssSelector(".inlineblock.redClockBigIcon.middle.isOpenExchBig-1"));
}catch (NoSuchElementException e){
isFirstElemPresent = false;
}
if(isFirstElemPresent == false)
driver.findElement(By.cssSelector(".inlineblock.redClockBigIcon.middle.isOpenExchBig-2"));
OrTo avoid try catch block, use below code snap:
List<WebElement> elements = driver.findElements(By.className("redClockBigIcon"));
if (elements.size() == 0) {
System.out.println("Element is not present");
} else {
System.out.println("Element is present");
}

How to assign dataGrid to other dataGrid in Flex. a = b doesn't work

in Flex I have something like that:
var dg:DataGrid = new DataGrid();
if (something) dg = dg1 else if (something_2) dg = dg2;
dg.dataProvider.getItemAt(3).id;
and dg is ALWAYS pointing at DataGrid (even if dg1 has name DataGrid_test and dg2 = DataGrid_test2) and finally action is made on my first DataGrid (DataGrid_test).
Why?
How can I pass dg1 or dg2 to dg?
Here is pasted almost full code of this part of application. I edited it to make that more clear.
var dg:DataGrid = null;
if ( currentState == "state1" ) { //if this condition is true then app. go into if and
dg = dataGrid_first; // make dg = DataGrid (1)
test.text = "inco"; // shows "inco" in "test" label
} else if ( currentState == "state2" ) { // if this is true then app. go..
dg = dataGrid_second; //here and set dg as DataGrid (exactly!) (2)
test.text = "outgo"; // and change test label into blank text (earlier text disapears)
}
search(dg);
It is modified with advice of '#splash'
Still not working.
EDIT:
I made this sceond edit to answer for all You who are helping me with that :) I think that it will be the best way. In codeblock above I added comments. (please read now comments and after that come back here :) )
Now I will explain exactly what happens.
I debug it many times and here are results:
dg is pointing at DataGrid (as component in flex, not as my dataGrid_first), I needed to extend DataGrid so now it is ColorColumn component (I don't know if I called it properly), not DataGrid. And dg is pointing at ColorColumn not at dataGrid_first or dataGrid_second. I even tried today the same thing what suggest #splash:
if ( currentState == "state1" ) {
test.text = "inco";
search(dataGrid_first);
} else if ( currentState == "state2" ) {
test.text = "outgo";
search(dataGrid_second);
}
and search still points at ColorColumn :/ My problem is really easy- I just want to pass to search different dataGrid on each state. If You have other ideas how I can do that in right way then I will pleased to hear about it. :)
But still I don't understand why it doesn't work. My search function uses algorhitm Boyer-Moor for searching through dataGrid.dataProvider for some text. If it find something then it is pushed into new array and after passing whole dataProvider I colorize rows with searched word.
If dg is never pointing to dg1 and dg2 then your (something) expressions may be evaluate to false. Check the value of your if-conditions - this should be easy to debug.
This should work:
var dg:DataGrid = null;
if (something)
dg = dg1;
else if (something_2)
dg = dg2;
if (dg)
{
// do something with dg
}
[Update]
I still can't see why your code isn't working, but you could simplify it like this:
if ( currentState == "state1" ) {
test.text = "inco";
search(dataGrid_first);
} else if ( currentState == "state2" ) {
test.text = "outgo";
search(dataGrid_second);
}
I'd propose to write this - since I guess either dg1 or dg2 should be assigned:
if (something) {
dg = dg1;
} else {
dg = dg2;
}
There may be cases, where if () {} else () {} neither executes the first or the second conditional block.
Finally a small hint, which structurally eliminates unwanted assignments in if conditions: Always write the literal left of the comparison operation: if ( "state1" == currentState ). If you accidentally typed = instead of ==, the flex compiler emits an error. The other notation silently assigns a value.
Additionally: Did you single-stepped through your code and watched the variables dg1, dg2 and dg? If not, set a breakpoint a few line before the if-statement and run the code step by step from there on. What do you see?
Here's a another tip: Use assertions to check for inconistencies:
package my.company.utilities {
public function assert(expression:Boolean):void {
// probably conditionally compile this statement
if (!expression) {
throw new Error("Assertion failed!");
}
} // assert
}
Use it e.g. at the beginning of a method like this:
public function doTransaction( fromAccount:int, toAccount:int ) {
assert( 0 < fromAccount );
assert( 0 < toAccount );
}
A typically good use of assert is to check variables regarding their range. As of the above example, fromAccount and toAccount should always be positive. Due to a bug, bad values might get passed to doTransaction(). In this case, the assertion fires an error.

Detect 0 rows in a Telerik ASP.NET MVC Grid

What is considered the best practice for determining whether there are any rows bound?
Currently, I'm using the client-side OnDataBound event, and code similar to the following:
gridDataBound: function (event)
{
var rows = $('tbody tr:has(td)', this);
if (rows.length == 0 || (rows.length == 1 && rows[0].innerText == "No records to display'))
$('#GridSection').hide("slow");
}
There has got to be a better way!
I can suggest a shorter version:
if ($(this).find(".t-no-data").length) {
$("#GridSection").hide("slow");
}
Ah, a few minutes poking around and I think I have a solution that really feels better-
if ($("tbody tr:has(td).t-no-data", this).length != 0) {
$("#GridSection").hide("slow");
}
$('#grid-name').data('tGrid').data is an array of all of the records.
So, you can get the number of records using:
$('#grid-name').data('tGrid').data.length;

1050: Cannot assign to a non-reference value

Hi my code as fallows what is wrong with it?
thank you and sorry for my bad english.
protected function belgelerDG_itemClickHandler(event:ListEvent):void
{
var durum:Boolean = false;
if(belgeicerikWindow==null){
belgeicerikWindow=new belgeicerik();
belgeicerikWindow.title=belgelerDG.selectedItem.belge;
belgeicerikWindow.open();
}
else{
durum=false;
for ( var i:int = NativeApplication.nativeApplication.openedWindows.length - 1; i >= 0; --i ) {
if(NativeApplication.nativeApplication.openedWindows[i].title.toString() == belgeicerikWindow.title=belgelerDG.selectedItem.belge){
belgeicerikWindow.orderToFront();
durum=true;
}
}
if(durum==false){
belgeicerikWindow=new belgeicerik();
belgeicerikWindow.title=belgelerDG.selectedItem.belge;
belgeicerikWindow.open();
}
}
}
I'm betting the problem lies with the if statement that starts with:
if(NativeApplication.nativeApplication.openedWindows[i].title.toString()
You are doing an assignment within the value you are trying to compare against:
== belgeicerikWindow.title=belgelerDG.selectedItem.belge)
If it isn't what is causing your problem, at least it is something you should fix to make things more legible. :)

Is there a version of the removeElement function in Go for the vector package like Java has in its Vector class?

I am porting over some Java code into Google's Go language and I converting all code except I am stuck on just one part after an amazingly smooth port. My Go code looks like this and the section I am talking about is commented out:
func main() {
var puzzleHistory * vector.Vector;
puzzleHistory = vector.New(0);
var puzzle PegPuzzle;
puzzle.InitPegPuzzle(3,2);
puzzleHistory.Push(puzzle);
var copyPuzzle PegPuzzle;
var currentPuzzle PegPuzzle;
currentPuzzle = puzzleHistory.At(0).(PegPuzzle);
isDone := false;
for !isDone {
currentPuzzle = puzzleHistory.At(0).(PegPuzzle);
currentPuzzle.findAllValidMoves();
for i := 0; i < currentPuzzle.validMoves.Len(); i++ {
copyPuzzle.NewPegPuzzle(currentPuzzle.holes, currentPuzzle.movesAlreadyDone);
copyPuzzle.doMove(currentPuzzle.validMoves.At(i).(Move));
// There is no function in Go's Vector that will remove an element like Java's Vector
//puzzleHistory.removeElement(currentPuzzle);
copyPuzzle.findAllValidMoves();
if copyPuzzle.validMoves.Len() != 0 {
puzzleHistory.Push(copyPuzzle);
}
if copyPuzzle.isSolutionPuzzle() {
fmt.Printf("Puzzle Solved");
copyPuzzle.show();
isDone = true;
}
}
}
}
If there is no version available, which I believe there isn't ... does anyone know how I would go about implementing such a thing on my own?
How about Vector.Delete( i ) ?
Right now Go doesn't support generic equality operators. So you'll have to write something that iterates over the vector and removes the correct one.

Resources