• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java question: is there any equivalent to Visual Basic's "With"?

KingNothing

Diamond Member
In Visual Basic, if you had an object Foo with property Bar, you could do this:

With Foo

.Bar = 1

End With

Is there something similar in Java? I'm getting so sick of typing System.out.println. :frown:
 
I don't know either language...but that seems terrible ineffecient compared to objects in C++.

foo.bar = 1
if its public, if private then a function
foo.setbar(1)
seems alot easier.....
 
Originally posted by: Deeko
I don't know either language...but that seems terrible ineffecient compared to objects in C++.

foo.bar = 1
if its public, if private then a function
foo.setbar(1)
seems alot easier.....

The point is you don't have to type the object name repeatedly. I'd like to do this:

With System.out

println("line 1");
println("line 2");
println("line 3");

End With

Make sense?
 
Originally posted by: Deeko
I don't know either language...but that seems terrible ineffecient compared to objects in C++.

foo.bar = 1
if its public, if private then a function
foo.setbar(1)
seems alot easier.....

EVerything you said is valid in java.
 
Originally posted by: KingNothing
Originally posted by: Deeko
I don't know either language...but that seems terrible ineffecient compared to objects in C++.

foo.bar = 1
if its public, if private then a function
foo.setbar(1)
seems alot easier.....

The point is you don't have to type the object name repeatedly. I'd like to do this:

With System.out

println("line 1");
println("line 2");
println("line 3");

End With

Make sense?

Try copy + paste.
 
Originally posted by: Deeko
I don't know either language...but that seems terrible ineffecient compared to objects in C++.

foo.bar = 1
if its public, if private then a function
foo.setbar(1)
seems alot easier.....

No, you don't understand. Let's say you have a bunch of properties to set in an object. Instead of:
foo.bar = 1
foo.me = 2
foo.peanut = 3
foo.missile = 4

you can do this:
With foo
.bar = 1
.me =2
.peanut = 3
.missile = 4
End With

you can also use methods and such this way too. It's very nice with nested classes.

As for your question, I don't think there is. You might be able to create a macro, but I don't know if there are macros in Java.
 
Originally posted by: ReelC00L
Originally posted by: Deeko
I don't know either language...but that seems terrible ineffecient compared to objects in C++.

foo.bar = 1
if its public, if private then a function
foo.setbar(1)
seems alot easier.....

EVerything you said is valid in java.

well then...I knew they were similar but didn't want to say it would work cuz I didn't want to sound stupid 😉
 
Originally posted by: XZeroII
Originally posted by: Deeko
I don't know either language...but that seems terrible ineffecient compared to objects in C++.

foo.bar = 1
if its public, if private then a function
foo.setbar(1)
seems alot easier.....

No, you don't understand. Let's say you have a bunch of properties to set in an object. Instead of:
foo.bar = 1
foo.me = 2
foo.peanut = 3
foo.missile = 4

you can do this:
With foo
.bar = 1
.me =2
.peanut = 3
.missile = 4
End With

you can also use methods and such this way too. It's very nice with nested classes.

As for your question, I don't think there is. You might be able to create a macro, but I don't know if there are macros in Java.

so write an overloaded function foo.Set, with the possibility of one or all of the variables.
 
Originally posted by: XZeroII
Originally posted by: Deeko
I don't know either language...but that seems terrible ineffecient compared to objects in C++.

foo.bar = 1
if its public, if private then a function
foo.setbar(1)
seems alot easier.....

No, you don't understand. Let's say you have a bunch of properties to set in an object. Instead of:
foo.bar = 1
foo.me = 2
foo.peanut = 3
foo.missile = 4

you can do this:
With foo
.bar = 1
.me =2
.peanut = 3
.missile = 4
End With

you can also use methods and such this way too. It's very nice with nested classes.

As for your question, I don't think there is. You might be able to create a macro, but I don't know if there are macros in Java.

Yep, this is what I'm after. Visual Basic may not be as good as C++ in a lot of areas, but it sure has some niceties I miss. Not to mention the best IDE/help system I've ever used.
 
Originally posted by: Deeko
Originally posted by: XZeroII
Originally posted by: Deeko
I don't know either language...but that seems terrible ineffecient compared to objects in C++.

foo.bar = 1
if its public, if private then a function
foo.setbar(1)
seems alot easier.....

No, you don't understand. Let's say you have a bunch of properties to set in an object. Instead of:
foo.bar = 1
foo.me = 2
foo.peanut = 3
foo.missile = 4

you can do this:
With foo
.bar = 1
.me =2
.peanut = 3
.missile = 4
End With

you can also use methods and such this way too. It's very nice with nested classes.

As for your question, I don't think there is. You might be able to create a macro, but I don't know if there are macros in Java.

so right an overloaded function foo.Set, with the possibility of one or all of the variables.

here is an example from an application that I am currently working on:
With Table1.Rows(0)
_ .Cells(0).Text = "CatNum"
_ .Cells(0).Width = Unit.Pixel(400)
_ .Cells(1).Text = "Description"
_ .Cells(1).Width = Unit.Pixel(1500)
_ .Cells(2).Text = "Quantity"
_ .Cells(2).Width = Unit.Pixel(150)
_ .Cells(3).Text = "Unit Price"
_ .Cells(3).Width = Unit.Pixel(185)
_ .Cells(4).Text = "Total Price"
_ .Cells(4).Width = Unit.Pixel(185)
End With

I could change this to display on row 1 or 2 or 3 by changning just one single number and not using a constant. It is also easier to read. The underscores are there just so it's spaced properly.

 
Ok, I don't think a single person got what he was asking.

"With" simply maintains an implicit reference to an object within it's scope. It's syntactical sugar, nothing more. And no, no syntactically C-derived language (C++, Java, C#, ...) supports it.
 
Originally posted by: Descartes
Ok, I don't think a single person got what he was asking.

"With" simply maintains an implicit reference to an object within it's scope. It's syntactical sugar, nothing more. And no, no syntactically C-derived language (C++, Java, C#, ...) supports it.

I understand. I just prefer my method 😛
 
Originally posted by: Deeko
Originally posted by: Descartes
Ok, I don't think a single person got what he was asking.

"With" simply maintains an implicit reference to an object within it's scope. It's syntactical sugar, nothing more. And no, no syntactically C-derived language (C++, Java, C#, ...) supports it.

I understand. I just prefer my method 😛

Oh, I think I get your method now. You're talking about making a function with optional arguments? That'd work with a small number of methods/variables, but it's still not nearly as nice as With. Too bad C doesn't have it.
 
Originally posted by: Deeko
Originally posted by: Descartes
Ok, I don't think a single person got what he was asking.

"With" simply maintains an implicit reference to an object within it's scope. It's syntactical sugar, nothing more. And no, no syntactically C-derived language (C++, Java, C#, ...) supports it.

I understand. I just prefer my method 😛

You were just arguing the use of accessor/mutator methods over properties. Properties are, as noted above, just syntactic sugar, but they're no more inefficient than a normal accessor/mutator method call.
 
Originally posted by: KingNothing
Originally posted by: Deeko

so right an overloaded function foo.Set, with the possibility of one or all of the variables.

Can you explain this further?

It'd be a lot more work than just writing the object name a couple of extra times.

instead of doing this:

Thing.x = 1;
Thing.y = 2;
Thing.z = 3;

You add a function to the class that thing is a member of:

set(int sx, int sy, int sz){
x = sx;
y = sy;
z = sz;
}

Then you can just call Thing.set(1,2,3);

You can overload the set() function to take different types of arguments.
 
Originally posted by: Deeko
Originally posted by: XZeroII
Originally posted by: Deeko
I don't know either language...but that seems terrible ineffecient compared to objects in C++.

foo.bar = 1
if its public, if private then a function
foo.setbar(1)
seems alot easier.....

No, you don't understand. Let's say you have a bunch of properties to set in an object. Instead of:
foo.bar = 1
foo.me = 2
foo.peanut = 3
foo.missile = 4

you can do this:
With foo
.bar = 1
.me =2
.peanut = 3
.missile = 4
End With

you can also use methods and such this way too. It's very nice with nested classes.

As for your question, I don't think there is. You might be able to create a macro, but I don't know if there are macros in Java.

so right an overloaded function foo.Set, with the possibility of one or all of the variables.

I actually didn't read this before I responded a moment ago.

This is, imo, a poor solution. It is not type-safe, so validation will have to be added for *every* value in the mutator method. This can only guarantee compile-time type safety. An enum would obviously be 3000% better, but Java doesn't support them.

Either way, it's not a very OO way of doing things.
 
Originally posted by: KingNothing
Originally posted by: Deeko

so right an overloaded function foo.Set, with the possibility of one or all of the variables.

Can you explain this further?

Overloaded function works like this:

public:
void set(var1)
void set(var1, var2)
void set(var1, var3)
void set(var2, var3)
void set(var1, var2, var3)
private:
var1
var2
var3

now, in order for that to work properly, they would all have to be different variable types, otherwise the program wouldn't know which you wanted if you sent 2 variables in.
 
Originally posted by: Descartes
Originally posted by: Deeko
Originally posted by: XZeroII
Originally posted by: Deeko
I don't know either language...but that seems terrible ineffecient compared to objects in C++.

foo.bar = 1
if its public, if private then a function
foo.setbar(1)
seems alot easier.....

No, you don't understand. Let's say you have a bunch of properties to set in an object. Instead of:
foo.bar = 1
foo.me = 2
foo.peanut = 3
foo.missile = 4

you can do this:
With foo
.bar = 1
.me =2
.peanut = 3
.missile = 4
End With

you can also use methods and such this way too. It's very nice with nested classes.

As for your question, I don't think there is. You might be able to create a macro, but I don't know if there are macros in Java.

so right an overloaded function foo.Set, with the possibility of one or all of the variables.

I actually didn't read this before I responded a moment ago.

This is, imo, a poor solution. It is not type-safe, so validation will have to be added for *every* value in the mutator method. This can only guarantee compile-time type safety. An enum would obviously be 3000% better, but Java doesn't support them.

Either way, it's not a very OO way of doing things.

That's true, but that was just an example. You can do this with methods and properties as well. It comes in very handy at times. I guess you just need to use it a few times before you understand, I know I didn't understand it's usefulness until I used it a few times.
 
Back
Top