2 questions

  • Thread starter Thread starter Jay
  • Start date Start date
J

Jay

Hi,
Just making the transition over to VB.NET and have 2 questions.

1. VBC
Is there a reason why references (/r:xxxxx) needs to be included?
Seeing as they are referenced in the code with the imports keyword it seems
to be like a double-up?

2. Object.Methods
abc = new Object
abc.ToString()
returns system.object

abc = new Object
abc.GetType()
returns system.object

What's the difference?

Cheers
Jay
 
1) They don't unless there's possible naming collision. For instance,
DataTable's are memeber of System.Data. If I don't import System.Data, then
I need to use this:

Dim dt as New System.Data.DataTable

however, if I use an Imports Statement at the top, then I can just use this:

Dim dt as New DataTable

2) They are both essentially the same in that the second line of each has
no bearing on the return value. Calling ToString doesn't turn the object
from its current type into a string, it just provides a string
representation
Try
'Throw some Exception
Catch ex as System.Exception
'At this point, ex is of Type System.Exception
Debug.Assert(false, ex.ToString) 'will display the exception info but
ex.GetType will return System.Exception not String
End Try

So, your question is essentially this:

2. Object.Methods
abc = new Object
returns system.object

abc = new Object
returns system.object

And both are really the same. Calling the middle code line in each of those
(The GetType or ToString) has no bearing on the return value.

HTH,

Bill
 
William Ryan eMVP said:
1) They don't unless there's possible naming collision. For instance,
DataTable's are memeber of System.Data. If I don't import System.Data, then
I need to use this:

1. Sorry lost me here (fairly new to .net)
I meant, if I import a namespace into my code
e.g. imports system.windows.forms, then why do I need to add this switch to
the vbc compiler at the command prompt "/r:system.windows.forms.dll"
I thought that maybe the compiler would read the list of imports and add the
references itself.

2) They are both essentially the same in that the second line of each has
no bearing on the return value. Calling ToString doesn't turn the object
from its current type into a string, it just provides a string
representation

When I used the word returns I was thinking more of printing to the console
or something.
So if I have an object myObj and I use either
console.writeLine(myObj.GetType()) or console.writeLine(myObj.ToString())
methods then won't both print "system.object"?
If so, then how do they differ?

The chances are that I have completely misread what they do but, to me, it
seems like GetType determines what datatype a var is and returns the
datatype whereas ToString will determine what the datatype is and errr
return a string displaying the vars datatype??

Jay
 
Ooops, I thought you were using Visual Studio. I see you wrote VBC, just
missed that.

The Console.WriteLine() with the gettype needs to be cast to string with
GetType.ToString....
 
William Ryan eMVP said:
Ooops, I thought you were using Visual Studio. I see you wrote VBC, just
missed that.

Nope, I have vs.net through my MSDN sub but I'm using notepad so I can fully
understand the language before I let an IDE type it for me.

The Console.WriteLine() with the gettype needs to be cast to string with
GetType.ToString....

Does it? ok.
The book I'm reading doesn't show that in the example. I haven't actually
tried it so not sure If I'd get an error.

Jay
 
Jay said:
The chances are that I have completely misread what they do but, to me, it
seems like GetType determines what datatype a var is and returns the
datatype whereas ToString will determine what the datatype is and errr
return a string displaying the vars datatype??

ToString returns a "string representation" it can be overridden in your
objects to return whatever is appropriate. If you have a customer object
Cust1.ToString can return that customer's ID number for instance. Consider
that an integer variable using the .ToString method doesn't return "integer"
it returns a string representation of the integer.

Tom
 
Tom Leylan said:
ToString returns a "string representation" it can be overridden in your
objects to return whatever is appropriate. If you have a customer object
Cust1.ToString can return that customer's ID number for instance. Consider
that an integer variable using the .ToString method doesn't return "integer"
it returns a string representation of the integer.

Tom

Ahh thanks.
So if I'm reading correctly....
Unless you intend to do something with the returned value (other than just
note the datatype) then both methods do the same thing?

Jay
 
Jay said:
So if I'm reading correctly....
Unless you intend to do something with the returned value (other than just
note the datatype) then both methods do the same thing?

I wouldn't generalize it quite that way... the following link
http://msdn.microsoft.com/library/d.../html/frlrfsystemobjectclasstostringtopic.asp

mentions that the default implementation "returns the fully qualifed name of
the type of the Object." So it may that you get the same results in some
cases but ToString is overrided in lots of cases. Basically I wouldn't rely
on ToString if I was attempting to obtain an object Type.

Tom
 
Tom Leylan said:
I wouldn't generalize it quite that way... the following link
http://msdn.microsoft.com/library/d.../html/frlrfsystemobjectclasstostringtopic.asp

mentions that the default implementation "returns the fully qualifed name of
the type of the Object." So it may that you get the same results in some
cases but ToString is overrided in lots of cases. Basically I wouldn't rely
on ToString if I was attempting to obtain an object Type.

Thanks.

Any idea on my 1st question? (snipped from here so pasted)
"If I import a namespace into my code
e.g. imports system.windows.forms, then why do I need to add this switch to
the vbc compiler at the command prompt "/r:system.windows.forms.dll"
I thought that maybe the compiler would read the list of imports and add the
references itself."

Jay
 
Jay said:
Any idea on my 1st question? (snipped from here so pasted)
"If I import a namespace into my code
e.g. imports system.windows.forms, then why do I need to add this switch to
the vbc compiler at the command prompt "/r:system.windows.forms.dll"
I thought that maybe the compiler would read the list of imports and add the
references itself."

I wouldn't expect the compiler to read the "imports" statement and then to
automatically add the references. What if you didn't import anything?

The references are "assemblies" which can contain multiple namespaces which
are referenced by the imports statement. In fact multiple assemblies can
contain identical namespaces... the compiler would then have to search the
hard drive looking for files that looked right and then ask you "this new
one or the old version I found?"

I think you are trying to connect imports and references. Imports are a
convenience for you during editing, the compiler doesn't care. The
references tell the compiler where to find the definitions of the classes
that it might encounter.

Tom
 
Jay,
In addition to the other comments.
1. VBC
Is there a reason why references (/r:xxxxx) needs to be included?
/r:xxxx allows you to use a given type

While Imports allows you use the a given type without qualifying the full
name. (William's example of DataTable verses System.Data.DataTable).
2. Object.Methods

ToString is a function that returns a System.String, it is overridable so
you can implement it as appropriate in your class, the System.Object itself
implements it as GetType().ToString().

GetType is a function that returns a System.Type object.

System.Type.ToString returns the name of the qualified name of the type.

Hope this helps
Jay
 
Tom Leylan said:
I wouldn't expect the compiler to read the "imports" statement and then to
automatically add the references. What if you didn't import anything?

The references are "assemblies" which can contain multiple namespaces which
are referenced by the imports statement. In fact multiple assemblies can
contain identical namespaces... the compiler would then have to search the
hard drive looking for files that looked right and then ask you "this new
one or the old version I found?"

ok thanks.
I was just curious.... if I use vs.net I just choose build and it does it's
thing.
If (which I currently do) I use notepad then I have to spoonfeed the
compiler.
No biggie was just curious as to why the double typing.
The (referenced) dlls must be in my "path" as I seem to only need type the
name of them rather than the full path (thankfully!)
I think you are trying to connect imports and references. Imports are a
convenience for you during editing, the compiler doesn't care.

I (wrongly/rightly) assumed that the import meant... in order to run this
exe you are going to need to give me access to these objects.
I (wrongly/rightly) assumed that providing these namespaces to the compiler
was restating the fact.
As I said, I've been doing my learning within Notepad so the fruits of my
labour are never known until post-compile (as opposed to pressing a function
key in vs.net)
The
references tell the compiler where to find the definitions of the classes
that it might encounter.

So, when in vs.net, I choose "build" vs.net will include the references
based on what I've listed as imports?
Or it will see I've included something like New Point(0,100) and think ahh
you need system.drawing for this?

If so then this is what I meant by.. can the compiler not work this out?
Certainly not complaining about it, just trying to understand.

Jay
 
Jay,
So, when in vs.net, I choose "build" vs.net will include the references
based on what I've listed as imports?
VS.NET has 'Project - Add Reference' where you add new references, to see
the list of current references in the project you can use Solution Explorer,
and expand the References node of a Project.

When VS.NET invokes vbc it passes these references to the compiler, either
with /r: or a response file.
If so then this is what I meant by.. can the compiler not work this out?
Certainly not complaining about it, just trying to understand.
Neither VS.NET nor the compiler "works this out". The problem with "work
this out" is that the compiler may need to check 250+ assemblies located on
your hard disk, as you have 250+ assemblies in your "path". I hope you agree
explicitly listing 5 assemblies with 'Project - Add Reference' or /r: will
be significantly quicker to compiler then the check 250+ assemblies each
time you build a project.

250+ assemblies are based on the number of assemblies in the GAC on my
machine, the \Windows\Assembly folder. 250+ includes both 1.0 & 1.1
assemblies, as I have both versions on my machine. Also if you are building
class libraries (.dll) you have your own assemblies to add to the mix.
Further if you purchased any products, then you can add their assemblies to
the number...

Hope this helps
Jay
 
Jay said:
If (which I currently do) I use notepad then I have to spoonfeed the
compiler.

BTW you can DL much better free editors than Notepad. Most support multiple
files and some have syntax highlighting and such.
The (referenced) dlls must be in my "path" as I seem to only need type the
name of them rather than the full path (thankfully!)

I don't know that they need to be in the path but if they aren't then you
surely have to supply the path. That said, you aren't actually typing the
command line in every time are you? If nothing else a batch file should be
able to handle this task.
I (wrongly/rightly) assumed that the import meant... in order to run this
exe you are going to need to give me access to these objects.

That would be the "wrongly" side :-)
So, when in vs.net, I choose "build" vs.net will include the references
based on what I've listed as imports?
Or it will see I've included something like New Point(0,100) and think ahh
you need system.drawing for this?

As I note Jay Harlow has replied, "nope" :-) The compiler (or linker) can't
know you want system.drawing since you could have intended to use
jay.drawing. That's why you have to tell it where to look. You can see
that you couldn't have both an existing release version and a test version
of a library (under development) if the compiler decided it knew which one
you intended to use.

Tom
 
Back
Top