Re: String Reference Type
- From: "Lars" <jon.doe@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 11 Feb 2008 03:03:39 GMT
"bruce barker" <nospam@xxxxxxxxxx> skrev i meddelandet
news:%231aoD5EbIHA.1212@xxxxxxxxxxxxxxxxxxxxxxx
no heap is used.
the variable x is on the stack and is the value, rather than reference to
the value. the variable MyValue being a class static value, is allocated
in the static class definition structure, and again is the value, not a
pointer to the value.
the answer to the whole issue is very easy
C# Code
// Below is code that profs if nothing else is given in the parameter list
C# uses Call By Value (CBV) "call by value"
//
// Do you really know what CBV and Call By Referense (CBR) means.
// It has nothing to do with how it is stored on stack or heap.
// It has to with how the programmer use the parameters.
//
// CBV makes sure that the function doesn't change the value for the
caller
// weather it's changed within the function or not!
//
// If a function change a value on a CBR parameter the value of the
variable sent as parameter
// is changed for the caller!
//
// This is trivial. This is the same in ALL languages I use except
Smalltalk that only uses CBR.
//
// This code also demonstrates how to use and write default and copy
contructors.
//
// Clicking Buttin1 switches lblY1 and lblY2 and then it set's
lblClassName name to "My String".
// Let's se if any one else knows why, I do but I'll you guess.
//
// Clicking the Button Button2 should increase I, why doesnt it do so?
// Is it because the page is reloaded and the whole object is created
again. .NET programming I guess?
//
// Let's see if I get it right. Change I to a WebControl at it will
increase since WebControls are send back to the server, or?
//
// How ever the default and copy constructors work.
//
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace Lesson05
{
public partial class LRBand : System.Web.UI.Page
{
String X;
static int I = 0;
public LRBand()
{
setX("My Sting");
}
public LRBand(LRBand anObject)
{
X = anObject.getX();
}
public String getX() { return X; }
public void setX(String anX) { X=anX; }
void swap(String x1, String x2, ref String y1, ref String y2)
{
String tmp;
tmp = x1;
x1 = x2;
x2 = tmp;
tmp = y1;
y1 = y2;
y2 = tmp;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SqlDataSource1_Selected(object sender,
SqlDataSourceStatusEventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
String S1 = lblY1.Text.ToString();
String S2 = lblY2.Text.ToString();
LRBand myClass = new LRBand(this);
swap(lblX1.Text, lblX2.Text, ref S1, ref S2 );
lblY1.Text = S1;
lblY2.Text = S2;
lblClassName.Text = myClass.getX() ;
I += 1;
}
protected void Button2_Click1(object sender, EventArgs e)
{
I = I + I;
lblCount.Text = I.ToString();
}
}
}
//
// lblY1.Text and lblY2.Text switches place. I runthe test on my localhoast
server.
//
// Now if you used your own class that didn't define copy constructors
Lars
in .net numerics, bools, dates, structures and enums are value types,
everything else is a true object and thus a reference type.
-- bruce (sqlwork.com)
RN1 wrote:
On Feb 10, 4:38 am, "Jonathan Wood" <jw...@xxxxxxxxxxxxxxxx> wrote:
Lars,
No, Strings in Pascal (and ADA as I reacll) are tru types.This appears to be to me but I don't know what "no" refers to, or what
"tru
types" are.
I could be wrong but it seems to me you are confusing terms. I'm not
that
familiar with Delphi but a reference in C++ is different from a .NET
reference type. I don't know the exact criteria used to determine value
or
reference type, but to me a reference type is a pointer, and so any type
that requires a pointer seems it would be a good candidate. And as far
as
I'm concerned, all strings require pointers, whether they are made
available
to the language that uses them or not. (Certainly, it wouldn't make
sense to
ever store a string in a register or even on the stack.)
Don't know about C# but I hope the compiler takes care of this. ByAccording tohttp://www.knowdotnet.com/articles/referencetypes2.html,
references or not is not of interest. What is of interest is if you
have
the option to use call by value for strings or if all parameters are
treated as references. Can you use call by value in C# how do you use
call
by value?
"One
of the most common mistakes developers make when learning .NET is
confusing
Reference and Value Types with Passing values by Reference or Value."
Again,
I think you are confusing terms.
--
Jonathan Wood
SoftCircuits Programminghttp://www.softcircuits.com
OK...now suppose I have the following code:
<script runat="server">
Class MyInt
Public MyValue As Integer
End Class
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim x As New MyInt
...................
...................
...................
End Sub
</script>
If I am not wrong, the Dim line in the Page_Load sub "creates space in
the heap & returns a pointer (say, 0x000001)". To whom does the Dim
line return the pointer?
Please correct me if I am wrong.
.
- Follow-Ups:
- Re: String Reference Type
- From: Lars
- Re: String Reference Type
- References:
- String Reference Type
- From: RN1
- Re: String Reference Type
- From: Jonathan Wood
- Re: String Reference Type
- From: Lars
- Re: String Reference Type
- From: Jonathan Wood
- Re: String Reference Type
- From: RN1
- Re: String Reference Type
- From: bruce barker
- String Reference Type
- Prev by Date: Re: Dumb JavaScript question - how to use \\ not as a comment?
- Next by Date: Re: Dumb JavaScript question - how to use \\ not as a comment?
- Previous by thread: Re: String Reference Type
- Next by thread: Re: String Reference Type
- Index(es):