473,396 Members | 1,849 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,396 software developers and data experts.

length of data type

how to get the length of variable for differernt data type, such as data
type of structure, integer, date, etc.

Thanks in advance.
Jul 7 '06 #1
4 2392
donald,

You can't really get these from the system. Some people use the sizeof
operator (or Marshal.SizeOf method), but you can't do that, since it will
give you the size of the marshaled type (to unmanaged code).

For primitive types (things like int, bool, double, etc, etc), these
sizes are well known, but for classes and the like, the only way you can
determine that is through a hook into the CLR.

What exactly are you trying to do?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"donald" <do*********@gmail.comwrote in message
news:eA**************@TK2MSFTNGP05.phx.gbl...
how to get the length of variable for differernt data type, such as data
type of structure, integer, date, etc.

Thanks in advance.


Jul 7 '06 #2
For all value types you can use sizeof()

The one issue with this is if the value type happens to be a structure and
happens to contain a reference type only the reference to the reference type
will be counted (not the size of the reference type as well).

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

"donald" <do*********@gmail.comwrote in message
news:eA**************@TK2MSFTNGP05.phx.gbl...
how to get the length of variable for differernt data type, such as data
type of structure, integer, date, etc.

Thanks in advance.


Jul 7 '06 #3
Greg,
>For all value types you can use sizeof()

The one issue with this is if the value type happens to be a structure and
happens to contain a reference type only the reference to the reference type
will be counted (not the size of the reference type as well).

If the struct contains a reference type field, you won't be able to
use sizeof on it at all (try it and you'll get a CS0208), so that's
not an issue.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 8 '06 #4
You are right mattias .. I wasn't very clear in this case.

You can still use Marshall.SizeOf in such situations which is what I was
referring to ..

as an example ...

using System;
using System.Collections.Generic;
using System.Text;
namespace FreakShow {
struct foo {
string bar;
int foo1;
int foo2;
}
class Program {
static void Main(string[] args) {
foo f = new foo(
Console.WriteLine(System.Runtime.InteropServices.M arshal.SizeOf(f));
}
}
}

Will include the reference size to bar but not the actual size of bar when
determining the size.

Cheers,

Greg Young
MVP- C#
http://codebetter.com/blogs/gregyoung
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:Oi**************@TK2MSFTNGP03.phx.gbl...
Greg,
>>For all value types you can use sizeof()

The one issue with this is if the value type happens to be a structure and
happens to contain a reference type only the reference to the reference
type
will be counted (not the size of the reference type as well).


If the struct contains a reference type field, you won't be able to
use sizeof on it at all (try it and you'll get a CS0208), so that's
not an issue.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jul 8 '06 #5

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

Similar topics

3
by: srinivas reddy | last post by:
Hi, I have following questions. 1. Does va_arg allow one to read user defined types. My compiler allows but I am wondering whether it is true for all. 2. I wrote the following code. Pardon my...
14
by: Luiz Antonio Gomes Pican?o | last post by:
How i can store a variable length data in file ? I want to do it using pure C, without existing databases. I'm thinking to use pages to store data. Anyone has idea for the file format ? I...
13
by: Jason Manfield | last post by:
For some URLs (e.g.http://v3.espacenet.com/origdoc?DB=EPODOC&IDX=WO2005028634&F=0&QPN=WO2005028634), the content length for the HttpWebResponse I get with request.GetResponse in empty. The...
4
by: Scott Lemen | last post by:
Hi, Some Win APIs expect a structure with a fixed length string. How is it defined in VB .Net 2003? When I try to use the FixedLengthString class I get an "Array bounds cannot appear in type...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
1
by: emielvl | last post by:
Hello, I'm developing a client/server architecture based on the XML-RPC implementation in php4. All works pretty well, except that in the response from the server there is no "Content-Length" in...
3
by: Bill Pursell | last post by:
I have a program that does most of its work traversing a bunch of lists. The lists contain a void *, and I spent some time today replacing the void *'s with a copy of the data at the end of the...
1
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string arrays. Bascially the vb6 programmer stored all...
3
by: imtmub | last post by:
Hi All, I am facing some problem in the field data(Description field in Item table). In that table one field(Description field)data legth is 255 and type: nvarchar. In some operation this field data...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.