|
I want to calculate the size of the variable and not size of the data type. For example int anyValue=10;
what is the size of anyValue ?
(*Not calculate size of int)
NOTE:-->
So here anyValue size must be less from int datatype size.
|
|
|
|
|
You could do it by a loop that repeatedly right shifts the value until it reaches zero. The number of shifts gives the size.
|
|
|
|
|
Have a read of this Marshal.SizeOf[^]
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
The size of a variable is the size of the type it declared as: so for your example:
int anyValue = 10; the size of anyValue is always 32bits / 4 bytes because an int is a fixed size, regardless of what it contains.
This is even true for reference types: the variable which holds the reference will always be the same size:
Control c = button1; c is a reverence to a Control instance, so it will always be 32 bits or 64 bits depending on your your app is built and running.
The value it refers to can be different sizes in some cases: string, arrays, collections and so on can be variable sized1, but in general even a specific instance of a class will always be the same size. Value type instances are always the same size, they can't vary.
1: Even then, the collection instance is not a variable size, but the data it collects may be - the actual collection of objects is stored as an array of objects which is itself a reference.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hi OriginalGriff,
int sizeOfVariable = Marshal.SizeOf('A');
int size = sizeof(Char);
Console.WriteLine("The size using marshal is {0} and SizeOf is {1} ",sizeOfVariable,size);
Console.Read();
Why the two result is different ??? :P
|
|
|
|
|
sizeof vs Marshal.SizeOf[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
This is where is starts to get complicated...
Because a Char is not the same everywhere!
'A' is a char - so it will normally return 2 because a Unicode character takes two bytes (an ASCII character takes one, but nearly all the time in .NET you are dealing with Unicode values)
So the sizeof(char) returns 2 because it needs two bytes of managed memory to store each char.
When you use Marshalling however, it isn't dealing with Managed memory, so it doesn't use Unicode - it uses ASCII instead. Because Marshalling is all about getting ready to talk to external non-NET applications, it can't just pass managed memory directly - because it might have to move it while the other application is using it - so it will effectively prepare a copy of the data in ASCII, because that is the older character standard that most apps used pre-NET.
So sizeof will give you the Managed size - 2
and Marshall.SizeOf gives you the unmanaged size - 1
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hi OriginalGriff
Yess This is right Boss
|
|
|
|
|
What definition of "size" are you using here? Because normally it would be said that the size of anyValue is by definition the size of its type, it's a fixed-size type after all.
|
|
|
|
|
public interface IWTF
{
string SomeString { set; get; }
}
public static class WTF<T> where T : IWTF
{
public static string WTFMethod(IWTF i1, IWTF i2)
{
return i1.SomeString + i2.SomeString;
}
} Given the above: in some Method, in some instance-of-something-or-other, you call the 'WTFMethod in 'WTF:
WTF.WTFMethod(null, null); Of course, an error would be thrown as the 'WTFMethod was executed.
Which means (?): if I really want to make sure that I'm getting valid parameter inputs to 'WTFMethod:
public static string WTFMethod(IWTF i1, IWTF i2)
{
if (!(i1 is IWTF) || !(i2 is IWTF)) throw new ArgumentException("You Suck !");
return i1.SomeString + i2.SomeString;
} This lack-of-strong-typing (I don't know what else to call it) became apparent to me today while testing an Interface and its implementations.
Why ?
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
I assume in your second code block the generic type got lost while pasting it?
I can't see anything abnormal here - the compiler allows you to provide null for any reference type argument. The argument being an interface type doesn't matter.
But null is the only other possibility aside from a valid IWTF-reference, so you can replace the (i1 is IWTF) -checks with checking for null/non-null.
cheers, Sascha
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Hi, I'm new in C#, first of all sorry for my bad english.
I have a form, there is a login and a password input in my form.
I want to make a update query in a virtual machine, just a sample (I know the code isnt right):
UPDATE 'launcher' = 0 WHERE 'account' = textbox.Text and 'login' = textbox2.Text
--
So, whow can I make this? I know that to have privilegies I have to execute this from a php inside the machine where is the sql table.
Like my title says, I want to send the values account and password to a PHP in online machine, and that executa the query.
|
|
|
|
|
You have to send a HTTP Request to the machine where the web application is running. Construct the URL correctly, then send it, and collect the response. E.g.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
|
|
|
|
|
Thanks, if your type I found the solution in Internet!
|
|
|
|
|
Initially , I did row grouping in excel document using c#. Again I perform row grouping in between the (already grouped rows). same excel document the outline level will be increased.
I use below code
Excel.Range rng2 = sheet.get_Range((rg_row1 + 1) + ":" + (rg_row2 - 1), Missing.Value) as Excel.Range;
rng2.EntireRow.Group();
Please tell me why that outline level is increased?
How to maintain 2 levels?
|
|
|
|
|
I want clear understanding.
|
|
|
|
|
|
Just to add, the second example there is rather ambiguous.
Rather than having object , if you modify the code slightly to use string (string is still a reference type) as:
string name = "sandeep";
char[] values = {'s','a','n','d','e','e','p'};
string myName = new string(values);
Console.WriteLine("== operator result is {0}", name == myName);
Console.WriteLine("Equals method result is {0}", myName.Equals(name));
Console.ReadLine();
This is due to the CLR String pool -> the strings with same content reference to the same memory.
Just saying, if that interests the OP.
You have just been Sharapova'd.
|
|
|
|
|
It might be wise to call ReferenceEquals on your two strings to prove the point, or not, as the case may be.
Alan.
|
|
|
|
|
Yeah, sorry. I really should have mentioned about interned strings.
You have just been Sharapova'd.
|
|
|
|
|
Always(== and .equals) do value comparison in case of String datatype.
in case of other reference type == produce reference comparison and .Equals compares value or content.
In value types data both act same and compares value only
After Some RND I found this.
If its not 100% right please explain why
|
|
|
|
|
“==” compares if the object references are same while “.Equals()” compares if the contents are same.
So if you run the below code both “==” and “.Equals()” returns true because content as well as references are same.
Take 2 variables & test it ,you came to clear conclusion....
|
|
|
|
|
There are many differences, I'll list only some of them..
Equals is a virtual function that is present in Object, whereas operator== is a static function. Many of the differences arise from this. For example (not an exhaustive list),
- there is by default no operator== for a value type, where there is an Equals inherited from Object.
- you can use Equals on a value that has any generic type, you can use operator== only if the generic type is constrained to be a reference type (and then it will mean reference equality, not any operator== defined by whatever the concrete type was).
- an overridden Equals must deal with a parameter of type Object, an implemented operator== can decide for itself what types its parameters must have (at least one of them must be the same as the declaring type though).
Together this usually (but not always) means that implementing operator== is confusing for a reference type, but expected and wanted for a value type.
They also have slightly difference meanings in general, operator== can be "whatever makes sense", but Equals should be an equivalence relation, because it (or its cousin, IEquatable<sometype>.Equals) is used all over the place in the standard library and third party libraries with the assumption that it is an equivalence relation. Violating that will cause trouble if, for example, you use it as a key in a Dictionary<Tkey, Tvalue>, or if you try to put it in as HashSet<T> (which various parts LINQ like to do behind the scenes, breaking unexpectedly and invisibly if Equals is not an equivalence relation).
Usually operator== should (for sanity) also be an equivalence relation, but it isn't always, for example take double.NaN, which must be equal to itself according to Equals (equivalence relations must be reflexive), but compares unequal to everything (including itself) according to operator==. If it didn't compare equal to itself according to Equals, putting it in a HashSet<double> would result in it "never really being present" (it would be added, but testing whether it is in there would return false), so thankfully that is not how it works. Meanwhile it should also compare unequal to itself, otherwise it would be in blatant violation of the IEEE 754 standard for floating point arithmetic.
|
|
|
|
|
Hi,
I have to built one console application in C# and the problem is --
Sample Dataset:
CategoryId ParentCategoryId Name Keywords
100 -1 Business Money
200 -1 Tutoring Teaching
101 100 Accounting Taxes
102 100 Taxation
201 200 Computer
103 101 Corporate Tax
202 201 Operating
109 101 Small business
System
Tax
Write a working program which should do the following:
a. Given a category id return category name, parentCategoryId and key-word. Ensure that if
key-word is not present for the category, then the data from its parent should be
returned.
i. Input: 201; Output: ParentCategoryID=200, Name=Computer,
ii. Input: 202; Output: ParentCategoryID=201, Name=Operating System,
Sample Input/Output:
Keywords=Teaching
Keywords=Teaching
b. Given category level as parameter (say N) return the names of the categories which are
of N’th level in the hierarchy (categories with parentId -1 are at 1st level).
Sample Input/Output:
i. Input: 2; Output: 101, 102, 201
ii. Input: 3; Output: 103, 109, 202
.........................................................
Now my piece of code is ----
static void Main(string[] args)
{
DataSet ds = new DataSet("Category");
// Table for parents
DataTable parentTable = new DataTable("Parents");
//parentTable.Columns.Add("CategoryId", typeof(int));
parentTable.Columns.Add("ParentCategoryId", typeof(int));
parentTable.Columns.Add("Name", typeof(string));
parentTable.Columns.Add("Keywords", typeof(string));
//Create some childs.
parentTable.Rows.Add(new object[] { 100, "Business", "Money" });
parentTable.Rows.Add(new object[] { 200, "Tutoring", "Teaching" });
parentTable.Rows.Add(new object[] { 101,"Accounting", "Taxes" });
//parentTable.Rows.Add(new object[] { 102, 100, "Taxation", "" });
parentTable.Rows.Add(new object[] { 201, "Computer", "" });
//parentTable.Rows.Add(new object[] { 103, 101, "Corporate Tax", "" });
//parentTable.Rows.Add(new object[] { 202, 201, "Operating System", "" });
//parentTable.Rows.Add(new object[] { 109, 101, "Small Business Tax", "" });
ds.Tables.Add(parentTable);
// Table for childrend
DataTable childTable = new DataTable("Child");
childTable.Columns.Add("CategoryId", typeof(int));
childTable.Columns.Add("ParentCategoryId", typeof(int));
childTable.Columns.Add("Name", typeof(string));
childTable.Columns.Add("Keywords", typeof(string));
//Create some childs.
childTable.Rows.Add(new object[] { 100,-1, "Business", "Money" });
childTable.Rows.Add(new object[] { 200,-1, "Tutoring", "Teaching" });
childTable.Rows.Add(new object[] { 101,100, "Accounting", "Taxes" });
childTable.Rows.Add(new object[] { 102,100, "Taxation", "" });
childTable.Rows.Add(new object[] { 201,200, "Computer", "" });
childTable.Rows.Add(new object[] { 103,101, "Corporate Tax","" });
childTable.Rows.Add(new object[] { 202,201, "Operating System", "" });
childTable.Rows.Add(new object[] { 109,101, "Small Business Tax", "" });
ds.Tables.Add(childTable);
// Create their relation.
DataRelation parentChildRelation = new DataRelation("ParentChild",parentTable.Columns["CategoryId"], childTable.Columns["ParentCategoryId"]);
ds.Relations.Add(parentChildRelation);
// Display each parent and their children based on the relation.
string category = null;
category = Console.ReadLine();
for (int i = 0; i < ds.Tables["Child"].Rows.Count; i++)
{
if (category == ds.Tables["Child"].Columns["CategoryId"].ToString())
{
foreach (DataRow parent in parentTable.Rows)
{
DataRow[] children = parent.GetChildRows(parentChildRelation);
Console.WriteLine("\n{0}, has {1} children", parent["ParentName"].ToString(), children.Count<datarow>());
foreach (DataRow child in children)
{
Console.WriteLine("\t{0}", child["ChildName"].ToString());
}
}
}
else
{
Console.WriteLine("\n Enter a valid Category ID :\n");
}
}
}
.............................................................
Questions -
1. I am getting a ArgumentNullException on DataRelation line. The error is "'column' argument cannot be null.
Parameter name: column" - How to solve this.
2. Where I can make a check that Keywords are null.
3. Should I define another DataTable for Level?
Thanks & Regards
Supratik De
|
|
|
|
|
Supratik De wrote:
DataTable parentTable = new DataTable("Parents");
parentTable.Columns.Add("ParentCategoryId", typeof(int));
...
DataRelation parentChildRelation = new DataRelation("ParentChild", parentTable.Columns["CategoryId"], childTable.Columns["ParentCategoryId"]);
...
I am getting a ArgumentNullException on DataRelation line. The error is "'column' argument cannot be null.
parentTable doesn't contain a column called CategoryId , because the line which adds that column has been commented out.
From the description, it sounds like you only want a sinlge DataTable , with a self-referencing relationship.
You'll need to tell the DataRelation not to try to create constraints, since there is no row with a CategoryId of "-1". Alternatively, replace the "-1" in the ParentCategoryId column with null .
You can use the GetParentRow method[^] to get the parent row, and the GetChildRows method[^] to get the child rows.
If you set the table's PrimaryKey , you can use the Rows.Find method[^] to quickly find a row by its CategoryId.
This should get you started:
DataTable theTable = new DataTable("Data");
theTable.Columns.Add("CategoryId", typeof(int));
theTable.Columns.Add("ParentCategoryId", typeof(int));
theTable.Columns.Add("Name", typeof(string));
theTable.Columns.Add("Keywords", typeof(string));
theTable.PrimaryKey = new DataColumn[] { theTable.Columns[0] };
theTable.Rows.Add(new object[] { 100, -1, "Business", "Money" });
theTable.Rows.Add(new object[] { 200, -1, "Tutoring", "Teaching" });
theTable.Rows.Add(new object[] { 101, 100, "Accounting", "Taxes" });
theTable.Rows.Add(new object[] { 102, 100, "Taxation", null });
theTable.Rows.Add(new object[] { 201, 200, "Computer", null });
theTable.Rows.Add(new object[] { 103, 101, "Corporate Tax", null });
theTable.Rows.Add(new object[] { 202, 201, "Operating System", null });
theTable.Rows.Add(new object[] { 109, 101, "Small Business Tax", null });
ds.Tables.Add(theTable);
DataRelation parentCategoryRelation = new DataRelation("ParentCategory",
theTable.Columns["CategoryId"],
theTable.Columns["ParentCategoryId"],
false);
ds.Relations.Add(parentCategoryRelation);
DataRow theRow = theTable.Rows.Find(201);
DataRow parentRow = theRow.GetParentRow(parentCategoryRelation);
DataRow[] childRows = theRow.GetChildRows(parentCategoryRelation);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|