I am 2 hours into my first Kotlin, and any coding, lesson and I am following along with a tutorial.
I have come across an "Unresolved reference" and am not sure how to resolve it so that I can continue following along with my lesson.
There are several options presented, such as: creating local variable, parameter, property and reference.
As stated, I have 2 hours of experience and no idea what these mean yet. Any insight would be appreciated, thanks you.
Screenshot
I'm gonna paste your image in so you can see what I'm talking about - in general, you should always paste code, not screenshots (adding a screenshot as well is fine), but in this case the screenshot is helpful:
See how the else is underlined in red? That's indicating some kind of error, and for a basic language keyword like else, that's a sign you've gone wrong with the syntax - what you've written doesn't make sense, for some reason. Hovering over the red squiggle should give you some info about what the problem could be (it can't always tell for sure, if the code doesn't make sense)
Also, see how everything after the else is greyed out? It's not showing up as code in the normal colours - so it's not that there's code that doesn't make sense, it's not even being recognised as code at all. Again, when a whole section "goes wrong" like that, it's a sign that something's messed up with the syntax, usually something earlier that's causing a problem. And since the else before it is highlighted as a problem, that's a good place to look!
That's just general advice - your issue is this:
if (enteredName == "") { // opening brace for the if block
// some stuff
} // closing brace for the if block
// we just closed the if block - so what's this closing?
} else {
You'll see braces a lot in Kotlin - in simple terms, they wrap a bunch of code into a single block, so when that block is run, it can do several steps. So your if condition, if it evaluates to true, will run everything in that block that follows it. If it evaluates to false, it won't. And in that case, if there's an else statement following it, it'll run whatever follows that instead.
All your if/else stuff is happening inside that click listener you're defining:
submitButton.setOnClickListener {
// some code to run when the button is clicked
}
So those braces hold the code that runs when the listener fires. But what you've written is this:
submitButton.setOnClickListener {
if (something) { // open if
// stuff
} // close if
} // close click listener
// this is now outside the click listener block
else {
That else has to follow an if - but it doesn't, it follows that setOnClickListener line, because that brace closed the lambda (the code block) and ended that line. Now the next line is else { which makes no sense to Kotlin, and that's why it's tripping up, you're getting a red error mark, and the code following it is greyed out because Kotlin doesn't know what to do with it. It's extremely confused.
So you need to make sure your braces are paired up, and each closing brace is in the correct place to close its counterpart. They close in reverse order, right? Basically backing out of each block. Making sure each block is indented an extra level helps, because then you can visually see where each block starts and ends (and it draws a vertical line from the start to the end of each one). If you just remove that extra closing brace, you'll be missing one, because it's just in the wrong place. So you'll have to work out where to put it! Where the listener block ends.
That's uh a really long post to say "one of your braces is in the wrong place" but I hope it helps you understand what's going on and how to spot this kind of thing in the future. You'll have to handle these blocks a lot, so it's good to get used to wrangling them. It's pretty simple once you get it!
Related
This is kind of silly, but the answer is eluding me and the issue is driving me a bit bonkers. Every now and then Excel decides to not let me use ' and " properly, forcing me to press another key before it appears. The thing is, the only solution I can ever find to this problem is changing the keyboard layout. For this very reason, I only have US English as a keyboard option and yet Excel still pulls this prank on me. It's also only Excel doing it, I can type totally normally in web browsers as well as Word.
Is it possible I accidentally hit some setting or something? Is there a way to prevent this from happening? Eventually it just sort of goes away, but it might be days before that happens, so I'm thinking there must be a button I'm bumping on accident that is toggling this setting.
To prepare an exam, I have to learn coding using google docs rather than using an IDE. It may seem idiot or impratical but the teacher really insists on that. It seems that is the same thing as Google interviews for example...
So far, I really got used with using two spaces indentation (I hate using tab indendation). And I want to learn how can I do that quickly in Google docs. Is there a shortcut or a "tips" (add-on?) for that?
For example, if you have an "if-condition" starting at the position number 4 and you want to move to the following line, it can be really convenient to have the cursor position at 4. Then, you'll add two spaces to start your block at position number 6. (ALL the if block will have at least position 6). But in Docs, when you move to the following line, the cursor always start at the beginning of the line except if you use the tab indentation. And it's really shitting if you want to indent your code properly...
Do you have a solution for that please ?
I was facing the same problem.
To write Python code in google docs, my solution is:
Switch off auto-capitalization, auto-correction and smart quotes. And other auto-substitutions so that you may write code without docs like formatting.
To do this: Tools> Preferences and uncheck the above items. Screen shots attached below.
Set tab to two spaces.
Right-click on ruler on top of page. Add left-tab stop, an arrow will appear, move this tab stop to 2 spaces from left of ruler. Try adding tab in the current line and if the tab size is OK, save this formatting.
To save:
Format > Paragraph Styles > Normal Text > Update 'Normal Text' to match.
Last, to add color(synatx highlight), you may use 'code blocks extension'.
Click install.
Now, write code in docs, select the code in google doc, click on:
Add-ons > Code Block. A pop up opens up.
Choose language : 'python' for me.
Choose theme: 'atom-one-dark'
click Format.
The code looks much like IDE, and writing more code is easier too.
While I understand that sometimes a point can be made by making students do things in a way that might not seem logical, this one doesn't make any sense to me.
Students don't learn anything useful by pressing space twice instead of letting an editor or IDE do it for them.
I wonder, is it because he/she wants it delivered in Google Docs or because they want you coding that way? If it's the latter, you won't have to hide your workaround.
I can only suggest using a good text editor, I always use the excellent and free Notepad++, and copy and paste it to and from Google Docs. Your instructor will never know. In fact, I'd be hunting around to see of there was a way to access your code files directly in Google Docs from Notepad++, or to auto-sync a folder with Google docs.
Notepad++ has syntax colouring - which will save your life - and can be set to indent with tabs or spaces to whatever indent width you specify. If not using an IDE, I only use Notepad++.
Your instructor sounds like some I had, people who cross a line from being quirky but with a point to make to just being a dick. There is absolutely no point in telling a student to code only in Google docs. Google docs is a great thing, I love it, but it is by no stretch of the imagination a coding tool.
(I see that this is an aged thread, but I'll respond in case someone else with a similar issue - like this year's class for that course - comes looking for an answer.)
You could try (ab)using bulleted lists:
Insert a bulleted list
Right click on the bullet, select "More bullets..."
In the "Symbol" selection list, choose "Format & whitespace" and select one of the whitespace options as bullet char
Repeat for as many levels (of bullet sub-lists) as you think you may need in your program
Fix indentation to match whatever feels best for you
Every time you need to write a nested block, you will have to press "tab" only once, then bullet list level is kept and the indentation with it. To go back to the outer block, just press shift-tab.
However, IMHO it doesn't worth the trouble. Generally, interview question solutions are not that large and hitting spacebar a couple of times is not much of an overhead.
It's somewhat cumbersome but you can set as many tab stops as you want, and docs will move nested tabs to the next tab stop as well. For example:
You can add tab stops by right clicking on that tab bar where you want the tab stop to be and clicking Add left tab stop. You'll want to have all text in the document selected if you want the tab stop to apply everywhere.
I can't imagine this issue happens for everyone because it renders Web Essentials completely unusable, so I'm really confused as to what's going on.
Here's what I would like to accomplish: a simple "a" tag block nested within another block (in this case, "body")
HOWEVER! The second I type "a", an intellisense dropdown pops up with suggestions.
I do not do anything to invoke this dropdown aside from typing "a". Once I then press space or open curly bracket, "align-content" is populated from the dropdown.
This is obviously worse than frustrating since it makes using the editor completely impractical. Similar things happen with other tags, too. "span" becomes "ruby-span" for instance.
I've checked the Web Essential settings and there's nothing there to deal with this. Can anyone replicate and help with this issue? Thanks.
What is the best way to prevent ReSharper from wrapping certain lines of code. For example, an if statement with one condition and a simple body should not be wrapped. ReSharper currently forces this line...
if (item == null) { return null; }
... to break like this:
if (item == null)
{
return null;
}
I don't want to blindly disable this feature necessarily... just make it a bit more intelligent.
Update for newer versions of ReSharper
(my version is 2021.1)
Since a picture is worth than a thousand words, you probably want to configure it like so. In this way it takes care of constructs such as using too:
Check the ones that start with Keep existing arrangement [...] if you want to instruct ReSharper to leave, say, single-line ifs alone if they are already written that way.
Took me longer to figure out than I wanted it to, hopefully it will help someone else.
You can change this in the R# Settings:
Open the menu Resharper -> Options
Go to Code Editing -> C# -> Formatting Style -> Line Breaks and Wrapping
Change to option Break line in single embedded statement to Put on a single line or Do not change
Edit:
As mentioned by #Alexander it would be more suitable to use the option Break Line in a block with a single statement which I think is available since R# 8
In latest Resharper it is under General >> "Wrap log lines"
A couple weeks ago my IDE started getting very forceful... Since it's hard to explain what I mean, here is a video:
http://www.youtube.com/watch?v=tNGMlWwmzpg
It lets me do what I want outside of the parenthesis, so maybe it thinks it's bad syntax two put to evaluations inside, but even if that's true, I don't want my IDE restricting what I can type, it should just use intellisense and put the squigglies.
Here are the extensions I have installed (I disabled the one I installed most recently but it didn't help). I've used almost all of these forever without problem...
update: Offered a bounty because this is making me rage. I have disabled ALL extensions and right now I have the following line:
output += "<div title='" + dr("ServiceGroupType_Desc") + "'>" + dr("ServiceGroupType_Desc") + "</div>"""
I cannot delete the two extra quotes at the end. It just won't let me.
I had the same issue as you and had to uninstall the Codealike plugin to get it working again.
Often when things start misbehaving like this, I find one of three things has happened:
1) Somehow a modifier key gets stuck on. Either my keyboard gets weird, or some software glitch happens. Make sure none of your keys are stuck on unexpectedly.
2) Make sure accessibility services are off (or if you use some of them, make sure only the ones you use are on). Some of these can really mess with you if you are not aware they are on.
3) Make sure your keyboard is in the expected region with the expected layout. Sometimes this switches unexpectedly, and starts messing things up, and I won't realize for a long time.
Sorry if that isn't much help, but it is the only things I can think of.
As you've mentioned, "I have disabled ALL extensions", so it should have nothing to do with any plugin.
Start by turn off IntelliSense
Option->Editor->C/C++->turn off IntelliSense with IntelliSense=FALSE
see if the whole auto complete feature is gone.
"I disabled the one I installed most recently but it didn't help"
You have to make sure the extension is not running in the background. If any, kill them as long as it doesn't affect the VS.