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

KingNothing

Diamond Member
Apr 6, 2002
7,141
1
0
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:
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
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.....
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
uh, so you prefer to type:

With Foo
.bar()
End With

to

Foo.bar();

:confused: Uh, the second one is much shorter.
 

KingNothing

Diamond Member
Apr 6, 2002
7,141
1
0
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?
 

Reel

Diamond Member
Jul 14, 2001
4,484
0
76
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.
 

Reel

Diamond Member
Jul 14, 2001
4,484
0
76
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.
 

XZeroII

Lifer
Jun 30, 2001
12,572
0
0
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.
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
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 ;)
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
oh i get it now

i don't know of one, but you should be able to do this pretty fast in any decent text editor.
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
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.
 

KingNothing

Diamond Member
Apr 6, 2002
7,141
1
0
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.
 

XZeroII

Lifer
Jun 30, 2001
12,572
0
0
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.

 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
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.
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
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 :p
 

KingNothing

Diamond Member
Apr 6, 2002
7,141
1
0
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 :p

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.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
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 :p

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.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
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.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
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.
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
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.
 

XZeroII

Lifer
Jun 30, 2001
12,572
0
0
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.