Newbie confusion on assignment, references, pointers, arrays..

  • Thread starter Thread starter Russ
  • Start date Start date
R

Russ

If I do:

TextBox [] tb = new TextBox [10];

Does it actually create 10 textboxes? I would think so but that is
not what I want. I want to create an array of 10 pointers to
textboxes. So if:

TextBox [];

And then later:

TextBox box = new TextBox;

and then:

tb [0] = box;

What happens? Am I placing a pointer to box in the array, or am I
copying the box into the array and creating a new box in the process?

I think the latter, but again, that is not what I want. I want a
pointer or reference to the original box. I want at some point later
to be able to iterate through the array and get the value from each
TextBox that the array elements point to.

In C++:

TextBox *tb [10];
TextBox box = new TextBox;
tb [0] = &Box;

When I try something similar in C#, I get an error saying that
pointers can only be used in unsafe context. Somewhere I read that it
is possible to create an array of references. But then how can you
assign an item later, references are inviolate, right?

Can anyone tell me how to accomplish what I am trying to do.

Thanks, Russ
 
If I do:

TextBox [] tb = new TextBox [10];

Does it actually create 10 textboxes? I would think so but that
is not what I want.

Russ,

..Net data types come in two flavors: reference types and value types.
Syntactically their declarations look the same, but their definitions
are different, and they behave differently.

// Declare a value type and a reference type.

int myInt, copyOfMyInt; // Value types.
TextBox myTextBox, textBoxReference; // Reference types.


// Define the variables.

myInt = 42; // Value type.
myTextBox = new TextBox(); // Reference type.

// Note the use of the "new" operator. myTextBox
// holds a reference to an instance of the TextBox class.


// Behavior.

// Value types.
copyOfMyInt = myInt;

// Both copyOfMyInt and myInt now hold separate
// copies of the number 42.

copyOfMyInt = 99;

// Now copyOfMyInt holds 99, while myInt still contains 42.


// Reference types.
textBoxReference = myTextBox;

// textBoxReference holds a separate copy of the reference myTextBox
// has. Both variables now refer to the same instance of the
// TextBox class.


Here's some more info:

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/csspec/html/vclrfcsharpspec_1_2.asp

or

http://tinyurl.com/2zlc2
 
Russ... Nope it doesn't. Your code creates an array object with ten
slots. Each
slot can hold a reference of type TextBox and each slot is initialized
to null.

Top Ten C++ Gumption Traps


In no particular order.


1) Arrays in C# are Objects


In C#, arrays are objects derived from System.Array. The following code
snippet creates an array object that can contain ten elements of
MyClass:


MyClass[] arrayMC= new MyClass[10];
What may surprise you, is that this call does not create any instances
of
MyClass! In fact, if you iterate over the array, you will find that the
array
contains ten null elements. To fill the array, you must explicitly
create ten
instances of MyClass and fill the array:

MyClass[] arrayMC= new MyClass[10];


[STAThread]
static void Main(string[] args)
{
    //
    // TODO: Add code to start application here
    //
    Class1 c1= new Class1();
    for (int i=0; i<10; i++)
    {
        c1.arrayMC= new MyClass();
    }
    Console.ReadLine();
}


Regards,
Jeff
TextBox [] tb = new TextBox [10];
Does it actually create 10 textboxes.<

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Chris R. Timmons said:
.Net data types come in two flavors: reference types and value types.
Syntactically their declarations look the same, but their definitions
are different, and they behave differently.

// Declare a value type and a reference type.

int myInt, copyOfMyInt; // Value types.
TextBox myTextBox, textBoxReference; // Reference types.


// Define the variables.

myInt = 42; // Value type.
myTextBox = new TextBox(); // Reference type.

// Note the use of the "new" operator. myTextBox
// holds a reference to an instance of the TextBox class.

The "new" operator doesn't mean it's a reference type though. I think
it's a bit of a red-herring in this case - the difference is just that
there are literals for the "primitive" value types, just as there are
literals for string, which is a reference type.
 
Thanks to you Jeff, and also to Chris and Jon for your explanations.
At one time I actually had figured it out the way you explained it -
mostly. But something is not working and that has me confused. Maybe
if I explain exactly what my problem is one of you will be able to
shed some light.

In a Web Client, I have Microsoft Web Controls called TabStrip and
MultiPage. The MultiPage contains dynamically added Controls called
PageViews to which numerous TextBoxes are added as controls:

TextBox box = new TextBox ();
// Set up the box
pv.Controls.Add (box);

I do this for one or more instances of PageView depending upon data.
It works great for setting up the tabbed pages and displaying the
data. But my problem is in trying to retrieve user changes from the
dynamically created TextBoxes.

I have a submit button on the main part of the page (not in one of the
tabbed pages), and when the user clicks it, an event is fired and code
gets executed. At this point I try to iterate through all the
controls contained in the MultiPage, but the MultiPage is already
empty - it has no controls! So, how to get the data out?

Knowing a little about C# (mostly from beating my head against the
wall for a couple of months now), I figured "hmm, managed code will
not dispose an item that still has a reference to it, so I will just
make an array of references to the text boxes and then they will not
get disposed and I can use the array to read the values". So I wrote
code like:

TextBox [] tb = new TextBox [n]; // at startup

// then when creating the controls
TextBox box = new TextBox ();
tb [id] = box;

From your explanations, this should produce a reference to one of the
TextBoxes in each array element. BUT - when I get to my click routine
and look at the TextBoxes 'in' the array, every one has a text value
of "0", even though the user has entered some other value. "0" is the
initial value, so it looks like the array contains a copy of the
original TextBox, and not a reference to the one that is used in the
Tab control...

I just cannot understand this behavior. I thought I was somehow wrong
in my understanding about how the arrays worked and that is what
prompted the original question.

I'd sure appreciate any additional insight as to what might be causing
my problem.

Thanks, Russ



Russ... Nope it doesn't. Your code creates an array object with ten
slots. Each
slot can hold a reference of type TextBox and each slot is initialized
to null.

Top Ten C++ Gumption Traps


In no particular order.


1) Arrays in C# are Objects


In C#, arrays are objects derived from System.Array. The following code
snippet creates an array object that can contain ten elements of
MyClass:


MyClass[] arrayMC= new MyClass[10];
What may surprise you, is that this call does not create any instances
of
MyClass! In fact, if you iterate over the array, you will find that the
array
contains ten null elements. To fill the array, you must explicitly
create ten
instances of MyClass and fill the array:

MyClass[] arrayMC= new MyClass[10];


[STAThread]
static void Main(string[] args)
{
    //
    // TODO: Add code to start application here
    //
    Class1 c1= new Class1();
    for (int i=0; i<10; i++)
    {
        c1.arrayMC= new MyClass();
    }
    Console.ReadLine();
}


Regards,
Jeff
TextBox [] tb = new TextBox [10];
Does it actually create 10 textboxes.<

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Russ... I have no experience with multipage, but on post back the data
is
there. I suspect that the problem is that you are not extracting the
data
before the controls are cleared or refreshed and initialized back to
zero. As
an example, if you had a "manager" class to manage the text boxes, you
could store a key value and text box reference in a Hashtable. You
could
then extract the data using the key value on post back, but this only
works if
you don't refresh the controls. In the following example, this only
works if
you don't call databind on post back and only if you extract the data in
the
button_UpdateClick handler before you call databind.

private void Page_Load(object sender, System.EventArgs e)
{
// perist and restore page variables
if (ViewState["rowIndex"] != null)
{
try
{
index=
Int32.Parse(ViewState["rowIndex"].ToString());

}
catch {}
}
else
{
ViewState["rowIndex"]= index;
}

sqlDataAdapter1.Fill(dataSet11);
manager= new AdapterManager(dataSet11.authors);
manager.Index= index;
RegisterControls();

// Put user code to initialize the page here
if (!IsPostBack)
{
DataBindControls();
}
}

private void buttonUpdate_Click(object sender, System.EventArgs e)
{
UpdateControls(); // new data is here, send it to database
DataBindControls(); // data refreshed from database
// should
reflect changes to database
}

http://www.geocities.com/jeff_louie/databindingclasses.htm

Regards,
Jeff
I have a submit button on the main part of the page (not in one of the
tabbed pages), and when the user clicks it, an event is fired and code
gets executed. At this point I try to iterate through all the
controls contained in the MultiPage, but the MultiPage is already
empty - it has no controls! So, how to get the data out?<

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Russ... I have no experience with multipage, but on post back the
data is there. I suspect that the problem is that you are not
extracting the data before the controls are cleared or refreshed
and initialized back to zero.

Jeff, thanks for your answer. I am sure you are right, but all
attempts by me to find the data fail. Please note that I am not using
any SQL connection and not databinding the MultiPage to anything. I
have used databind to bind to arrays, but is it possible to databind a
control containing a set of dynamically created TextBoxes to something
and get the data out that way?

As an example, if you had a "manager" class to manage the text
boxes, you could store a key value and text box reference in a
Hashtable. You could then extract the data using the key value
on post back, but this only works if you don't refresh the controls.
In the following example, this only works if you don't call
databind on post back and only if you extract the data in the
button_UpdateClick handler before you call databind.

Ok, I'm not sure what you mean by "if you don't refresh the controls".
Do you mean if I don't databind? If you don't mean that, how can I
prevent refreshing? I am not databinding anything, and in my
Page_Load I am using if (!IsPostBack), but in page load I really don't
do anything with the MultiPage anyway (except set some view parameters
like height, width, color).

It really appears that all the data is gone at post back. In my click
event I use the debugger to see that MultiPage1.Controls has a count
of zero (even though it had a count of 2 immediately after setting it
up). So I can't iterate through the controls and get the data out
that way. The MultiPage has an Unload event, and I tried to use that
to capture the data before it unloaded, but strangely it unloads
before it is ever displayed in the browser! (Thus before it is
changed by the user.)

You talked about an AdapterManager, and I don't really know what that
is. But since I am not using a data adaptor, I think it does not
apply to what I am doing.

Since you already told me you don't know about MultiPages, I don't
expect you will be able to help much but if you do have any ideas I
would appreciate them.

Thanks again, Russ
 
Russ... As you guessed, I don't have the multipage control on my system
so I
cannot test it. You might try posting a new topic with multipage control
in the
subject line here or to microsoft.public.dotnet.framework.aspnet

Regards,
Jeff

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Back
Top