❔ HeadFirst Design Pattern
So I started with this Book today. First problem starts with the Duck Project:
and when he tests he makes an instance of an abstract class ???? Is this a Java thing that works but not in C#?
41 Replies
he did not make an instance of an abstract class
Duck mallard = new MallardDuckk
Duck is an abstract class
MallardDuckk isnt
but how can you say Duck mallard = ?
he's making an instance of
MallardDuckk
, not Duck
its upcastedso you can make a variable of Duck but not with the new operator
yes
variable can be
Duck
, the instance can't.
abstract just means "you cant create instances of this type"oh okey
so my problem is different
this is in my program.cs
what does the error message say
That should be fine, assuming your namespaces and stuff are correct.
is it because my class has the same name as my project?
dont name ur namespace and class the same name
Your type shares a name with the namespace.
yes
so I have to find a way to refactor all namespaces
in my project
thats fairly easy, just use the built-in renaming tool. Ctrl+R, R by default.
set your cursor (the writing marker) on the namespace and do the shortcut
oh thats coolk
thank you
so does this replace everyything in my solution or project?
and is it casesensitiv and matches the exact word?
namespaces are case sensitive
because I was using CTRL + H all this time
yes I know but the CTRL+R,R I mean
so, thats your normal text-based search and replace
the rename tool follows code references
for example I wanna replace foot but have other variable called football would CTRL + R, R also change the foot in football?
no
its not doing it by text
its doing it by the code identifier
by code identifier u mean if I change namespace it only looks for other namespaces
and not variables or anything else
yeah, its context aware
if you are changing a namespace, it only changes namespaces.
but it changes them everywhere they are used, including in fully qualified type names etc
so for variables I shouldnt use CTRL+R,R
yes you should
its context aware.
it knows what it is changing
its NOT a string based search and replace.
hmm ok so when would u use CTRL + H ?
never, for changing code
search and replace is dangerous, as "foot" and "football" shows
ye I didnt know about CTRL+R,R so I would always go with CTRL+H and go each change one step at a time to make sure everything gets renamed correctly
thankk you all I just had to put using DuckProgram; and now it works
you can set a "default namespace" in your project properties too
so a new file wont get
Duck
by defaultwell its my fault I need a Duck class but I can call my project whatever
you could just rename the
Duck
class 🙂
DuckBase
or somethingit wouldnt make sense to change the Duck class it makes more sense to rename the Project
sure, but more work 🙂
how? CTRL R, R did it perfectly
¯\_(ツ)_/¯
🙂
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.