473,395 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

.NET and C#

Hi everybody.
Is in .NET any class, which converts .NET data types to c# types? (i.e. gets
a variable of one of .NET data types and returns native c# type?).
Rgds,
Komes

Aug 4 '06 #1
10 1133
komes wrote:
Is in .NET any class, which converts .NET data
types to c# types? (i.e. gets a variable of one of
.NET data types and returns native c# type?).
They are the same thing. When you declare a variable as "int" or
"float", it's the same as writing "Int32" or "Single".

Eq.
Aug 4 '06 #2
Maybe I'm having a "slow brain" day, but can you clarify what you mean?

C# doesn't really have any data types of its own (it merely has compile-time
aliases to the CLR types); the types are the same - i.e. typeof(int) ==
typeof(Int32).

Do you mean the names? i.e. give it "System.Int32" and it returns "int"? in
which case I am not aware of anything inbuilt, but the list of aliases isn't
long. You're throwing me a bit by talking about variables - is this for a
code generator?

Marc
Aug 4 '06 #3
OK, that's clear, but I have another problem in the same code. I want to
write some data with binarywriter from system.io. That method has 18
versions and takes object of different data types: bool, int, and so on. I
have a regular object, which has data of all types - read from database. I
want to make something like this:
Object o =...
binarywriterobject.write(o as o.getType())
But of course it's not possible, because it's like convertion to Type type.
Have you any idea how to solve this?
Rgds,
Komes

Aug 4 '06 #4
komes,

No, I don't think so. C# is just one of the languages and If there were such
a class or method or whatever for c# there must be for VB.NET, J# and
several other languages available. It is just not possible.

However as far as it goes for C# or VB.NET I believe you can use CodeDOM to
do the trick for you.

Try the following

[STAThread]
static void Main(string[] args)
{

CodeVariableDeclarationStatement var = new
CodeVariableDeclarationStatement(new CodeTypeReference(typeof(Int32)),
"foo");
// Obtains an ICodeGenerator from a CodeDomProvider class.
CSharpCodeProvider provider = new CSharpCodeProvider();
ICodeGenerator gen = provider.CreateGenerator();
// Generates source code using the code generator.
gen.GenerateCodeFromStatement(var, Console.Out, new
CodeGeneratorOptions());
}

this will generate source code for declaring variable *foo* of type int or
whatever Type obect provided. The good thing is that it will use C# keywords
for the primitive types. Thus the generated code will be:

int foo;

Having that string I believe you can extract the C# keyword using simple
string operations.
--
HTH
Stoitcho Goutsev (100)

"komes" <ki*********@wp.plwrote in message
news:ea**********@atlantis.news.tpi.pl...
Hi everybody.
Is in .NET any class, which converts .NET data types to c# types? (i.e.
gets a variable of one of .NET data types and returns native c# type?).
Rgds,
Komes

Aug 4 '06 #5
Binary writer is intended for writing /simple/ data (of known type) to
streams; it isn't clear what you are intending to write; if o is actually
just holding an int, string, etc (one of the ones that can already be
handled, but we don't know which at compile-time), then there is no easy way
to do this... perhaps the only way is via reflection with InvokeMember in
ExactBinding mode... not trivial, but possibly workable.

If you actually mean compund data (bespoke classes / structs), you have 2
choices:
1: write each field to the stream yourself (and read in the same order) -
essentially a compact form of manual serialization
or
2: look at BinaryFormatter().Serialize()

The latter will do a lot for you automatically, but is more verbose than the
former.

If that doesn't help, then post back...

Marc
Aug 4 '06 #6
Just write the object to the stream. While it is defined in your code as
"object," it is what it is. That is, while your variable is typed as
"object," the value of it will be a type. Example:

Object o = 25;

binarywriterobject.Write(o);

Because the value contained in the variable is an integer, the overload of
BinaryWriter.Write that takes an integer will be used.

Remember that all classes inherit object. Therefore, every instance of a
class *is* an object. However, it is still what it is. If you think of a
variable as a "box" it will help. The variable is not the value; it
*contains* the value. Yes, you refer to the value by referring to the
variable. But the variable itself is only a pointer to the actual instance.
It provides a "handle" that you can use to refer to the instance.

In mathematics (upon which all programming is based), algebra does the same
thing:

x + 5 = 10
x = 5

Note that the second equation says that "x" is equal to 5. Of course, "x" is
nothing but a placeholder, and provides a "handle" for working with an
as-yet-unknown number. So, when you refer to "x" you are not referring to
the variable itself, but to the number it represents.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Who is Mighty Abbott? A twin-turret scalawag.

"komes" <ki*********@wp.plwrote in message
news:ea**********@atlantis.news.tpi.pl...
OK, that's clear, but I have another problem in the same code. I want to
write some data with binarywriter from system.io. That method has 18
versions and takes object of different data types: bool, int, and so on. I
have a regular object, which has data of all types - read from database. I
want to make something like this:
Object o =...
binarywriterobject.write(o as o.getType())
But of course it's not possible, because it's like convertion to Type
type. Have you any idea how to solve this?
Rgds,
Komes

Aug 4 '06 #7
BinaryWriter does not have a Write(object) overload, so this will not
compile. Sorry.

To force it to inspect the object to determine an appropriate overload to
innvoke, you need to use reflection:

//(where "writer" is the specific writer in question, and "obj" is the value
typed as object):
writer.GetType().InvokeMember("Write", BindingFlags.InvokeMethod |
BindingFlags.Public | BindingFlags.Instance | BindingFlags.ExactBinding,
null, writer, new object[] { obj });

Marc
Aug 4 '06 #8

"Kevin Spencer" <uc*@ftc.govwrote in message
news:OF**************@TK2MSFTNGP05.phx.gbl...
Just write the object to the stream. While it is defined in your code as
"object," it is what it is. That is, while your variable is typed as
"object," the value of it will be a type. Example:

Object o = 25;

binarywriterobject.Write(o);

Because the value contained in the variable is an integer, the overload of
BinaryWriter.Write that takes an integer will be used.
I wrote a quick test just to see this in action (I'm curious even though
I've been developing VB.Net and C# for years now) ...

object a = null;
object b = 123;
object c = "123";

private void Show(object Value) { Console.WriteLine("object"); }
private void Show(int Value) { Console.WriteLine("int"); }
private void Show(string Value) { Console.WriteLine("string"); }

after running the above...I get the following output in the console window:

object
object
object
I don't mean to prove ya wrong Kev...just checking it out and found that you
were...the overloads that take an int and a string are never called...and
here is why...

When you create a variable of type object, and set it to a value type, the
variable does not hold the value. The variable is holding the "box"'ed
value. Since it's holding a box'ed value, it is seen as an object and not
the specified type (int or string). So, the first method (passing object
value) is called.

Correct me if I'm wrong, but that's how I see it happening here ;)

Mythran
Aug 4 '06 #9
BinaryWriter does not have a Write(object) overload, so this will not
compile. Sorry.
Hi Marc,

You're correct, of course. My mistake.

--

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Who is Mighty Abbott? A twin-turret scalawag.

"Marc Gravell" <ma**********@gmail.comwrote in message
news:eY**************@TK2MSFTNGP05.phx.gbl...
BinaryWriter does not have a Write(object) overload, so this will not
compile. Sorry.

To force it to inspect the object to determine an appropriate overload to
innvoke, you need to use reflection:

//(where "writer" is the specific writer in question, and "obj" is the
value typed as object):
writer.GetType().InvokeMember("Write", BindingFlags.InvokeMethod |
BindingFlags.Public | BindingFlags.Instance | BindingFlags.ExactBinding,
null, writer, new object[] { obj });

Marc

Aug 4 '06 #10
Yes, sorry, Mythran. I had a brain fart, perhaps an early "senior moment."
;-)

Marc's advice is spot on.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Who is Mighty Abbott? A twin-turret scalawag.

"Mythran" <ki********@hotmail.comwrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl...
>
"Kevin Spencer" <uc*@ftc.govwrote in message
news:OF**************@TK2MSFTNGP05.phx.gbl...
>Just write the object to the stream. While it is defined in your code as
"object," it is what it is. That is, while your variable is typed as
"object," the value of it will be a type. Example:

Object o = 25;

binarywriterobject.Write(o);

Because the value contained in the variable is an integer, the overload
of BinaryWriter.Write that takes an integer will be used.

I wrote a quick test just to see this in action (I'm curious even though
I've been developing VB.Net and C# for years now) ...

object a = null;
object b = 123;
object c = "123";

private void Show(object Value) { Console.WriteLine("object"); }
private void Show(int Value) { Console.WriteLine("int"); }
private void Show(string Value) { Console.WriteLine("string"); }

after running the above...I get the following output in the console
window:

object
object
object
I don't mean to prove ya wrong Kev...just checking it out and found that
you were...the overloads that take an int and a string are never
called...and here is why...

When you create a variable of type object, and set it to a value type, the
variable does not hold the value. The variable is holding the "box"'ed
value. Since it's holding a box'ed value, it is seen as an object and not
the specified type (int or string). So, the first method (passing object
value) is called.

Correct me if I'm wrong, but that's how I see it happening here ;)

Mythran


Aug 4 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.