Re: accessing a field of another class within same namespace

Tech-Archive recommends: Fix windows errors by optimizing your registry



Thanks Jon,

I instantiated it, but the value which has been assigned does not appear.
What I did was to make it static public, but ideally I would just
like to access it with the value that it has (not globally).

Thanks,

Carlos.

"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message
news:MPG.1dae314496ec018c98c874@xxxxxxxxxxxxxxxxxxxxxxx
> Carlos <ch_sanin@xxxxxxxxx> wrote:
>> I am trying to access a public field of another form class within
>> the same namespace. The field is public
>
> Oh dear - you should consider making it private, backing a public
> property.
>
>> , what is the best way
>> to access it from a different class? I defined as
>> private MyNameSpace.Form1 cForm1;
>>
>> and I am trying to use it later as
>>
>> TextBox.Text = cForm1.TextBox.Text;
>>
>> I see the properties, but I have a warning telling me that cForm1 is
>> never
>> assigned, and that its value will always be null. How can I avoid that?
>
> Simple - give it a value. I think you're missing that you need an
> instance of the form whose field you want to change. You need to ask
> yourself *which* form you want to use the field of. There could be
> several forms of the same type being shown at once (at least
> conceptually).
>
> --
> Jon Skeet - <skeet@xxxxxxxxx>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too


.



Relevant Pages

  • Re: Mulitihreading problem with BackgroundWorker
    ... I found some info on the Net that advised to create a new event in the Form class and subscribe to it from other classes, but you suggest creating the event in the class that actually raises the event and subscribe to it from the Form class, which makes more sense and sounds less cumbersome. ... just edit the Designer code and change "private" to "public." ... It works if I've changed textbox1 from private to public, but I guess it's not good practice. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: accessing a field of another class within same namespace
    ... > the same namespace. ... Oh dear - you should consider making it private, backing a public ... but I have a warning telling me that cForm1 is never ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Save + Restore form variables
    ... Some local variables are set up as public and published properties, ... > others are private to the form class. ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: access a control in another form
    ... There's a property for each control determining its visibility outside the ... It's 'private' by default, you can change it to 'internal' or ... 'public' for the control field to become visible outside the form class. ... Dmitriy Lapshin [C# / .NET MVP] ...
    (microsoft.public.dotnet.languages.csharp)