• 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"?

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.
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
 
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.
 
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? 😉
 
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.
 
Back
Top