hi
I'm new to c# and although having read 2 tutorials I cannot find what the
parenthesis in these 2 example situations mean;
1) string strKeyValue = (string)sampleConfig["Title"];
2) class myParentPage = (default_aspx) this.Page;
(default_aspx is the class name for my parent Page) 11 1686
Davíð,
In this case, "(string)" and "(default_aspx)" are casts. They allow you
to perform conversions (in the case of primitive value types), or convert a
reference of one type to another (assuming that the conversion is valid).
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Davíð Þórisson" <db**@hi.is> wrote in message
news:OX**************@TK2MSFTNGP10.phx.gbl... hi I'm new to c# and although having read 2 tutorials I cannot find what the parenthesis in these 2 example situations mean;
1) string strKeyValue = (string)sampleConfig["Title"]; 2) class myParentPage = (default_aspx) this.Page;
(default_aspx is the class name for my parent Page)
Hi Davið,
(string) is a cast. It is used for type conversion (between class names,
structs, interfaces etc). It indicates that sampleConfig["Title"] returns
something other than a string, typically an object. To be able to use
this object as a string you first need to cast it back to a string, by
using (string).
Consider object o = 1;
int i = o;
Now, even though o contains a number, it cannot be directly stored as an
int. You need to tell the compiler you are aware of the dangers by
putting (int) in front of o.
int i = (int)o;
Casting changes the "appearance" of the object and you need to cast to the
correct class to be able to perform specific tasks on the objects.
Consider an ArrayList. It can hold any number of objects, and all
different kinds of objects at the same time.
However, internally, the ArrayList considers all these objects to be of
type Object, the basic type all other types in .Net Framework inherits
from. When you retrieve an object from an ArrayList it returns the Object
"signature" so no matter what type it was when you put it in, you cannot
do anything with it other than the stuff belonging to Object, like
ToString() and GetType(). You need to cast the object back to the
original type (class, struct, interface, etc).
ArrayList a = new ArrayList();
string s = "hello world";
a.Add(s);
string s = (string)a[0]; // a[0] returns an Object
Don't confuse the class type Object with the "physical" object (the thing,
which can be any class, struct, value, array, enum, interface ...).
I'm not sure if this helps you in any way, and some further code samples
might be in order, but perhaps others can fill in.
--
Happy Coding!
Morten Wennevik [C# MVP]
Hi,
It is a cast expression, to convert one expression to a given type. You have
to be careful though that the expression being cast can be converted to the
target type, this may not enforced by the compiler and you can get a runtime
exception. this is used a lot currently for example in collections,
ArrayList is a collection of objects instances, that you may have to cast to
the correct type to be able to use it.
now , regarding the examples that you use:
1) string strKeyValue = (string)sampleConfig["Title"];
I prefer to write:
string strKeyValue = sampleConfig["Title"].ToString();
please note that the above will never give you error !!!. and you may end
with the incorrect value ( usually the fully qualified name of the class ).
2) class myParentPage = (default_aspx) this.Page;
This should give you compilation error, the correct syntax is:
object myParentPage = (default_aspx) this.Page;
or
default_aspx myParentPage = (default_aspx) this.Page;
Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Davíð Þórisson" <db**@hi.is> wrote in message
news:OX**************@TK2MSFTNGP10.phx.gbl... hi I'm new to c# and although having read 2 tutorials I cannot find what the parenthesis in these 2 example situations mean;
1) string strKeyValue = (string)sampleConfig["Title"]; 2) class myParentPage = (default_aspx) this.Page;
(default_aspx is the class name for my parent Page)
Davíð Þórisson <db**@hi.is> wrote: I'm new to c# and although having read 2 tutorials I cannot find what the parenthesis in these 2 example situations mean; 1) string strKeyValue = (string)sampleConfig["Title"]; 2) class myParentPage = (default_aspx) this.Page; (default_aspx is the class name for my parent Page)
They're both casts. I strongly suggest that you ditch ASP.NET for a
while, start learning C# and .NET with simple console applications, and
then move onto ASP.NET only after you know the basics.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
thx everyone, I'm familiar to casts just didn't know this was the syntax
Jon I've already been reading for 2 weeks now I want to experiment
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Davíð Þórisson <db**@hi.is> wrote: I'm new to c# and although having read 2 tutorials I cannot find what the parenthesis in these 2 example situations mean;
1) string strKeyValue = (string)sampleConfig["Title"]; 2) class myParentPage = (default_aspx) this.Page;
(default_aspx is the class name for my parent Page)
They're both casts. I strongly suggest that you ditch ASP.NET for a
while, start learning C# and .NET with simple console applications, and
then move onto ASP.NET only after you know the basics.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
one last thought Morten, when casting say array to string, what or who
defines how to convert the data, is it a built in method of the array object
or is some other "standard" method used?
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opsghnvur2klbvpo@pbn_computer... Hi Davið,
(string) is a cast. It is used for type conversion (between class names, structs, interfaces etc). It indicates that sampleConfig["Title"] returns something other than a string, typically an object. To be able to use this object as a string you first need to cast it back to a string, by using (string).
Consider object o = 1; int i = o;
Now, even though o contains a number, it cannot be directly stored as an int. You need to tell the compiler you are aware of the dangers by putting (int) in front of o.
int i = (int)o;
Casting changes the "appearance" of the object and you need to cast to the correct class to be able to perform specific tasks on the objects.
Consider an ArrayList. It can hold any number of objects, and all different kinds of objects at the same time. However, internally, the ArrayList considers all these objects to be of type Object, the basic type all other types in .Net Framework inherits from. When you retrieve an object from an ArrayList it returns the Object "signature" so no matter what type it was when you put it in, you cannot do anything with it other than the stuff belonging to Object, like ToString() and GetType(). You need to cast the object back to the original type (class, struct, interface, etc).
ArrayList a = new ArrayList(); string s = "hello world"; a.Add(s);
string s = (string)a[0]; // a[0] returns an Object
Don't confuse the class type Object with the "physical" object (the thing, which can be any class, struct, value, array, enum, interface ...).
I'm not sure if this helps you in any way, and some further code samples might be in order, but perhaps others can fill in.
-- Happy Coding! Morten Wennevik [C# MVP]
Davíð Þórisson <db**@hi.is> wrote: one last thought Morten, when casting say array to string, what or who defines how to convert the data, is it a built in method of the array object or is some other "standard" method used?
You can't convert an array to a string using a cast.
Using a cast does one of three things:
1) Performs no actual conversion, just changes the type of reference to
something else which the actual object is compatible with. For
instance:
object o = "hello";
string s = (string)o;
2) Performs an implicit or explicit type-defined conversion. For
instance:
SqlInt32 x = new SqlInt32(10);
int i = (int)x;
3) Performs an unboxing operation - here the type must be *exactly*
that of the boxed value:
object o = 10;
int i = (int)o;
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Simple value types have conversion rules that are defined by the C# language
specification.
To successfully cast any reference type A to B, A and B must have an
inheritance relationship with one another.
Since everything inherits from "object", it can be used as a container of
sorts for any other type. Once you put any type t into an instance of type
object, o, you can always retreive it by casting o to t -- that is, "t
myInstance = (t)o". When this is done with value types it's called boxing
and unboxing -- a special behavior implemented by the abstract class,
ValueType, from which all value types (structures and enums) derive.
You mention casting an array to a string. Your original example was:
string strKeyValue = (string)sampleConfig["Title"];
You can't cast an array to a string because one is not an ancestor of the
other. sampleConfig["Title"] is a member of the sampleConfig collection,
which is a collection of objects. The (string) cast gets you the string
that was stored in the instance of type object called sampleConfig["Title"].
Technically, what you are casting from is sampleConfig.Item("Title").
sampleConfig["Title"] is an "indexer", a bit of C# syntax sugar that lets
you define and use an array-like syntax to access members of collections.
--Bob
"Davíð Þórisson" <db**@hi.is> wrote in message
news:Ox*************@TK2MSFTNGP09.phx.gbl... one last thought Morten, when casting say array to string, what or who defines how to convert the data, is it a built in method of the array object or is some other "standard" method used?
"Bob Grommes" <bo*@bobgrommes.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... Simple value types have conversion rules that are defined by the C#
language specification.
To successfully cast any reference type A to B, A and B must have an inheritance relationship with one another.
not exactly true - what about the implicit conversion operator.
Consider the following in which there is absolutely no inheritance
relationship between MyString and YourString - yet the cast works as defined
:
regards
roy fine
/* ****************** */
public void Test(){
YourString a = new YourString("goodbye");
MyString b = (MyString)a;
}
/* ****************** */
class MyString{
string val;
public MyString(string t){val=t;}
}
/* ****************** */
class YourString{
string val;
public YourString(string t){val=t;}
public static implicit operator MyString (YourString t){
return new MyString("Hello");
}
}
Roy,
Well then perhaps I should rephrase it this way:
"To successfully cast any reference type A to B, A and B must either have an
inheritance relationship with one another, or A must have an appropriate
user-defined type conversion operator defined for type B."
Thanks for the nice refinement.
--Bob
"Roy Fine" <rl****@twt.obfuscate.net> wrote in message
news:ea**************@TK2MSFTNGP11.phx.gbl... "Bob Grommes" <bo*@bobgrommes.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... Simple value types have conversion rules that are defined by the C# language specification.
To successfully cast any reference type A to B, A and B must have an inheritance relationship with one another.
not exactly true - what about the implicit conversion operator.
Consider the following in which there is absolutely no inheritance relationship between MyString and YourString - yet the cast works as defined :
regards roy fine
/* ****************** */ public void Test(){ YourString a = new YourString("goodbye"); MyString b = (MyString)a; }
/* ****************** */ class MyString{ string val; public MyString(string t){val=t;} }
/* ****************** */ class YourString{ string val; public YourString(string t){val=t;} public static implicit operator MyString (YourString t){ return new MyString("Hello"); } }
bob, well done.
rlf
"Bob Grommes" <bo*@bobgrommes.com> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl... Roy,
Well then perhaps I should rephrase it this way:
"To successfully cast any reference type A to B, A and B must either have
an inheritance relationship with one another, or A must have an appropriate user-defined type conversion operator defined for type B."
Thanks for the nice refinement.
--Bob
"Roy Fine" <rl****@twt.obfuscate.net> wrote in message news:ea**************@TK2MSFTNGP11.phx.gbl... "Bob Grommes" <bo*@bobgrommes.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... Simple value types have conversion rules that are defined by the C# language specification.
To successfully cast any reference type A to B, A and B must have an inheritance relationship with one another.
not exactly true - what about the implicit conversion operator.
Consider the following in which there is absolutely no inheritance relationship between MyString and YourString - yet the cast works as defined :
regards roy fine
/* ****************** */ public void Test(){ YourString a = new YourString("goodbye"); MyString b = (MyString)a; }
/* ****************** */ class MyString{ string val; public MyString(string t){val=t;} }
/* ****************** */ class YourString{ string val; public YourString(string t){val=t;} public static implicit operator MyString (YourString t){ return new MyString("Hello"); } }
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: David MacQuigg |
last post by:
I'm concerned that with all the focus on obj$func binding, &closures,
and other not-so-pretty details of Prothon, that we are missing what
is really good - the simplification of classes. There are...
|
by: Bengt Richter |
last post by:
ISTM that
@limited_expression_producing_function
@another
def func(): pass
is syntactic sugar for creating a hidden list of functions. (Using '|' in place of '@'
doesn't change the picture...
|
by: Roy Yao |
last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ?
According to my analysis, operator sizeof, (type) and * have the same
precedence, and they combine from right to left. Then this...
|
by: typingcat |
last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so
on. I've tried many PHP IDEs today, but almost non of them supported
Unicode (UTF-8) file.
I've found that the only Unicode...
|
by: Hermawih |
last post by:
Hello ,
I want your opinion about this .
In order to say it clearly , I think I have to describe it in long sentences .
I could consider myself as Intermediate/Advance Access Developer ;...
|
by: Daniel Rudy |
last post by:
What is wrong with this program? When I try to compile it, I get the
following error. Compiler is gcc on FreeBSD.
strata:/home/dcrudy/c 1055 $$$ ->cc -g -oe6-3 e6-3.c
e6-3.c: In function...
|
by: Protoman |
last post by:
Could you tell me what's wrong with this program, it doesn't compile:
#include <iostream>
#include <cstdlib>
using namespace std;
class Everything
{
public:
static Everything* Instance()
|
by: Manuel |
last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using
devc++.
So I've imported the glut32.dsp into devc++, included manually some
headers, and start to compile.
It return a very...
|
by: Cylix |
last post by:
this.menus = { root: new Array };
|
by: gimme_this_gimme_that |
last post by:
What is going on here with the dollar signs and parenthesis?
$(document).ready(function(){
$("li").behavior("click",function(){
$(this).load(menu.html");
});
});
And when do you use click...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |