• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Accessing variables from nested class in kotlin

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am trying to access nested class variables how can I access it.

I want the output as "INVALID".
Currently I am getting "E400" I want to return the variable value of E400 ("INVALID").


What I want from code is I call a function with 400 as variable.And I want "Invalid" as output.
If I pass 401 I want "Unauthorised" as output.Or any other simple way of doing it.
I am new with kotlin so unable to do it.
 
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot Kotlin to a good extent, however, the construct of your code and logic looks wrong.

Don't know how to explain, but you can't pass variable to a function that way and expect to return field of such name as passed variable's value is.

You need to do something similar to (note: my syntax is illegal) :


Let's hope somebody else will join who got some more recent experience with that language.
 
Master Rancher
Posts: 3313
31
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutaras is pointing in the right direction.  I would be inclined to use a when statement (Kotlin's version of switch):



or a Map:
 
priyanshi bhardwaj
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:Liutaras is pointing in the right direction.  I would be inclined to use a when statement (Kotlin's version of switch):



or a Map:



Can I not use reflection library.was reading this :

https://stackoverflow.com/questions/35525122/kotlin-data-class-how-to-read-the-value-of-property-if-i-dont-know-its-name-at
 
Saloon Keeper
Posts: 5765
146
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The proper question is not "can you use reflection", the proper question is "does it make sense". You can use all kinds of approaches to all kinds of problems, but that may not be a good idea. In this case, I don't see what benefit using reflection would have over the approaches Mike mentioned; so you should avoid it.
 
priyanshi bhardwaj
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:The proper question is not "can you use reflection", the proper question is "does it make sense". You can use all kinds of approaches to all kinds of problems, but that may not be a good idea. In this case, I don't see what benefit using reflection would have over the approaches Mike mentioned; so you should avoid it.

using when or if statements will increase number of hard coded lines.I want to avoid it.
 
Tim Moores
Saloon Keeper
Posts: 5765
146
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code Mike posted is quite short. For starters, it's unlikely that using reflection will reduce it significantly, if at all. But more importantly, having fewer lines of code is not a meaningful measure of code quality. Having readable and maintainable code is, and code using reflection is almost inevitably harder to understand and maintain, and thus should be avoided.

Where did you get the idea that shorter code is better code?
 
priyanshi bhardwaj
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:
Where did you get the idea that shorter code is better code?

No where.Actually I want to handles errors and log's in my code.These are just dummy number of errors which I have stated.The errors can go upto from 1....250 also.What if I want to add more errors after few months also.So I want to write a code which is scalable as well as less hard coded so that it's easier for me to handle errors in future also.That is the main reason I am trying to avoid if and when statements writing it for 250 errors is quite almost impossible.
I simply want a class where I will define error code with string which needs to be printed for my user. And a function which will map my error code.
 
Master Rancher
Posts: 4208
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are these errors that are part of exceptions?
Just trying to get an idea of where these things fit in.

A lot of places I've worked at use enums for this type of issue.
Then you can associate the readable text with the enum in the same spot, and when you use it you don't need any if/switch/whatever statements.
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!