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

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: XZeroII
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.

I was referring to Deeko's overload suggestion, not VB's With :) I like VB's With, and use it quite often. I use it primarily when I'm working with ADO Command objects:

...

With oCmd.
.CommandType = adCmdStoredProc
.CommandText = "SomeSproc"
.Parameters.Add oCmd.CreateParameter(...)
.Execute
... etc.
End With
 

Reel

Diamond Member
Jul 14, 2001
4,484
0
76
I still think this whole question is based on laziness in typing. Am I missing something here or is this all about saving a bit of typing? I cannot see how it might save any time in compiling or running. So you have a short-hand way to do something in VB and you got used to it, it looks like you're leaving it behind in VB. Making overload functions would only create more unnecessary coding.
 

KingNothing

Diamond Member
Apr 6, 2002
7,141
1
0
Originally posted by: ReelC00L
I still think this whole question is based on laziness in typing. Am I missing something here or is this all about saving a bit of typing? I cannot see how it might save any time in compiling or running. So you have a short-hand way to do something in VB and you got used to it, it looks like you're leaving it behind in VB. Making overload functions would only create more unnecessary coding.

Yeah, all it saves is typing. Plus if you're doing comparisons on object properties, you can shorten your comparison lines by not having to type the object name in front of the property name all the time.

The best programmers are lazy, didn't you know that? ;)
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: ReelC00L
I still think this whole question is based on laziness in typing. Am I missing something here or is this all about saving a bit of typing? I cannot see how it might save any time in compiling or running. So you have a short-hand way to do something in VB and you got used to it, it looks like you're leaving it behind in VB. Making overload functions would only create more unnecessary coding.

I don't think it's laziness in typing; rather, I think it's about being succinct. For example, why would you want to use the ternary ?: operator? Sure, you could use a typical if/else, but ?: is often more succinct.

Why do a switch/Select Case? You could just do a bunch of if statements. Again, it's about being succinct.

As I've said, this is all *syntactic sugar*, no more. This has absolutely no improvement on execution, it is only a convenience for the programmer.