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

local classes

PGP
I notice that local classes are not supported in C#. Could you please
discuss why this is so? Was this a bad coding practice to begin with?
Aug 29 '07 #1
12 4770
On Aug 29, 4:15 pm, "PGP" <priyesh_do_not_replywrote:
I notice that local classes are not supported in C#. Could you please
discuss why this is so? Was this a bad coding practice to begin with?
What exactly do you mean by "local classes"? Are you talking about the
anonymous inner classes that Java has? They usually tackle a need
which is better addressed with delegates in C#.

Jon

Aug 29 '07 #2
PGP
>
What exactly do you mean by "local classes"? Are you talking about the
anonymous inner classes that Java has? They usually tackle a need
which is better addressed with delegates in C#.

Jon
Not sure about Java. In C++, they are classes that can be defined within
functions.
Aug 29 '07 #3
<"PGP" <priyesh_do_not_reply>wrote:
Not sure about Java. In C++, they are classes that can be defined within
functions.
And what use are they? If you could say what you normally try to
achieve with them, we can explain what you'd do in the same situation
in C#.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 29 '07 #4
PGP

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
<"PGP" <priyesh_do_not_reply>wrote:
>Not sure about Java. In C++, they are classes that can be defined within
functions.

And what use are they? If you could say what you normally try to
achieve with them, we can explain what you'd do in the same situation
in C#.
Sure. If you will please take a look at the following example which
calculates rect areas
inside a control. If we could discuss this based on this example, it would
be helpful
for me to understand the scenario and how exactly C# approaches this.

//IPaintableRectSubscriber is an interface with one method - void
OnPaintableRect(int nIndex, const CRect& rcItem);
void CThumbNailCtrl::IteratePaintableRects(IPaintableRe ctsSubscriber*
pSubscriber)
{
//Calculate rect areas that need painted, call back to subscriber
pSubscriber->OnPaintableRect(nIndex, rcItem) ;
}
//
BOOL CThumbNailCtrl::OnEraseBkgnd(CDC* pDC)
{
CRect rcClient ;
GetClientRect(rcClient);
//Local class here - note that the functionality
//implemented is not of interest to any other part of my control
class CEraseBkHelper : public IPaintableRectSubscriber
{
CThumbNailCtrl& m_outer; CDC* m_pDC; CRgn m_clipRgn; CRect
m_rcClient;
public:
void OnPaintableRect(const int i, const CRect& rc)
{
m_pDC->ExcludeClipRect(&rc);//exclude painted areas from
erase.
}
}erasebkhelper(*this, pDC, rcClient);
}
//
void CThumbNailCtrl::OnPaint()
{
CPaintDC dc(this);
//Local class here - Note the different params passed in constructor
//so i can have isolated logic and again, none of this
//makes sense to anything outside this function.
class CPaintHelper : public IPaintableRectsSubscriber
{
CThumbNailCtrl& m_outer; CPaintDC& m_dc;
public:
void OnPaintableRect(const int i, const CRect& rc)
{
//do the real painting here.
}
}painthelper(*this, dc);
}

As you see from the comments, the local classes are giving me a
nice way to encapsulate my logic within the respective functions,
yet allows for subscribing to a callback from another function
or class, hence maximizing the reuse.
Aug 29 '07 #5
<"PGP" <priyesh_do_not_reply>wrote:
And what use are they? If you could say what you normally try to
achieve with them, we can explain what you'd do in the same situation
in C#.
Sure. If you will please take a look at the following example which
calculates rect areas
inside a control. If we could discuss this based on this example, it would
be helpful
for me to understand the scenario and how exactly C# approaches this.
<snip>
As you see from the comments, the local classes are giving me a
nice way to encapsulate my logic within the respective functions,
yet allows for subscribing to a callback from another function
or class, hence maximizing the reuse.
Right. Delegates provide the best way of encapsulating a piece of logic
in C#, particularly with anonymous methods or lambda expressions from
C# 3. In the C++ code, there's a lot of "fluff" around the real point
of the local class, which is basically just to encapsulate the logic of
the OnPaintableRect method.

The part about the different parameters being passed in the constructor
is achieved in C# 2 and 3 using captured variables of anonymous
functions.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 29 '07 #6
PGP
Right. Delegates provide the best way of encapsulating a piece of logic
in C#, particularly with anonymous methods or lambda expressions from
C# 3. In the C++ code, there's a lot of "fluff" around the real point
of the local class, which is basically just to encapsulate the logic of
the OnPaintableRect method.
One of the seemingly "fluffy" point is that the encapsulated logic in local
classes
as opposed to distributed delegates poses a maintenance issue as one has to
track
back to the caller to really find what's affected with changes in the
delegate.
Another point is that it protects the localized logic from being reused
(read misused)
which might sound like an unforgivable sin but you might be able to relate
if you ever had to maintain code that you wrote after it has passed a couple
of hands
and back to you again.
Now there could be IDE advances in VS2008 which i am not familiar with at
this point
which might solve these issues.

Aug 29 '07 #7
PGP wrote:
>Right. Delegates provide the best way of encapsulating a piece of logic
in C#, particularly with anonymous methods or lambda expressions from
C# 3. In the C++ code, there's a lot of "fluff" around the real point
of the local class, which is basically just to encapsulate the logic of
the OnPaintableRect method.

One of the seemingly "fluffy" point is that the encapsulated logic in local
classes
as opposed to distributed delegates poses a maintenance issue as one has to
track
back to the caller to really find what's affected with changes in the
delegate.
Another point is that it protects the localized logic from being reused
(read misused)
My first thought when I saw the code example you provided was that a
delegate would suit your needs just fine.

Using an anonymous method, declared in the method in which you need the
delegate, addresses that need and all of the above points you make. Can
you provide an example of a C# implementation of what you're trying to
do that uses anonymous methods and yet still has the problems you
describe? It would be easier to help understand the issues you're
concerned about if you would.

Pete
Aug 29 '07 #8
<"PGP" <priyesh_do_not_reply>wrote:
Right. Delegates provide the best way of encapsulating a piece of logic
in C#, particularly with anonymous methods or lambda expressions from
C# 3. In the C++ code, there's a lot of "fluff" around the real point
of the local class, which is basically just to encapsulate the logic of
the OnPaintableRect method.

One of the seemingly "fluffy" point is that the encapsulated logic in local
classes as opposed to distributed delegates poses a maintenance issue as one has to
track back to the caller to really find what's affected with changes in the
delegate.
What do you mean by "distributed delegates"? Do you mean the logic
being expressed in a separate place from the code using it? If so,
anonymous methods do exactly what you want.
Another point is that it protects the localized logic from being reused
(read misused)
Likewise anonymous methods.
which might sound like an unforgivable sin but you might be able to relate
if you ever had to maintain code that you wrote after it has passed a couple
of hands and back to you again.
Now there could be IDE advances in VS2008 which i am not familiar with at
this point which might solve these issues.
Anonymous methods were introduced in C# 2 (VS2005). They are largely
superceded by lambda expressions in C# 3 (VS2008) but they're still
very useful.

See http://pobox.com/~skeet/csharp/csharp2/delegates.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 29 '07 #9
PGP
>
What do you mean by "distributed delegates"? Do you mean the logic
being expressed in a separate place from the code using it? If so,
anonymous methods do exactly what you want.
Yes, Anonymous methods seems to be addressing all the concerns i
have raised. Thank you!
Aug 29 '07 #10
PGP
My first thought when I saw the code example you provided was that a
delegate would suit your needs just fine.

Using an anonymous method, declared in the method in which you need the
delegate, addresses that need and all of the above points you make. Can
you provide an example of a C# implementation of what you're trying to do
that uses anonymous methods and yet still has the problems you describe?
It would be easier to help understand the issues you're concerned about if
you would.

Pete
Pete,
You are right. I was still thinking about delegates when i raised the
above concerns of potential distributed code. Anonymous methods
seem to solve these issues. Thanks!

Priyesh
Aug 29 '07 #11
You are right. I was still thinking about delegates when i raised the
above concerns of potential distributed code. Anonymous methods
seem to solve these issues. Thanks!
Actually, (small) local classes can be useful in C++ for the reasons you
cited earlier (improved encapsulation). Few programmers ever use them
however. Unfortunately, their main (would-be) benefit isn't supported by the
language at all. You can't use them for template arguments and this is my
biggest pet peeve with the language. I find myself constantly irritated
every time I need to call a C++ algorthim (or any other template) and can't
create a small local class to work with it (a class that has specific
functionality to the function where needed and would therefore be better
defined there). It's still not clear to me why this feature was never made
available even after asking Stroustrup himself about it during a conference
a couple of years ago. His response was very terse but had something to do
with "binding" issues IIRC (whatever the details may be). The current rules
(as implemented) don't allow it since templates are effectively specialized
at the nearest global or namespace scope wherever used. IOW, the rule
precludes.their use for local classes since any use of a template
effectively specializes the template just before the function itself (so any
local class remains unknown). At least C# allows anonymous methods which can
effectively mimic some of the same functionality (thankfully, I use them
often) but a local class would be even better.
Aug 30 '07 #12
PGP
Actually, (small) local classes can be useful in C++ for the reasons you
cited earlier (improved encapsulation). Few programmers ever use them
however. Unfortunately, their main (would-be) benefit isn't supported by
the language at all. You can't use them for template arguments and this is
my biggest pet peeve with the language. I find myself constantly irritated
every time I need to call a C++ algorthim (or any other template) and
can't create a small local class to work with it (a class that has
specific functionality to the function where needed and would therefore be
better defined there). It's still not clear to me why this feature was
never made available even after asking Stroustrup himself about it during
a conference a couple of years ago. His response was very terse but had
something to do with "binding" issues IIRC (whatever the details may be).
The current rules (as implemented) don't allow it since templates are
effectively specialized at the nearest global or namespace scope wherever
used. IOW, the rule precludes.their use for local classes since any use of
a template effectively specializes the template just before the function
itself (so any local class remains unknown). At least C# allows anonymous
methods which can effectively mimic some of the same functionality
(thankfully, I use them often) but a local class would be even better.
Larry,
Well said about why local classes have no linkage - seriously limits
their use with templates. My feelings exactly about not being able
to write a std predicate inside the *only* function which uses it.
I havent used anonymous methods but am looking forward to it.
Still stuck with .NET 1.1 due to product issues.
Thanks for the reply. Atleast i am not the only one
trying to use local classes. Apart from the template usage
frustration, i really appreciate the support for local
classes in c++.

Priyesh
Aug 30 '07 #13

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

Similar topics

3
by: Tim Hill | last post by:
I'm just getting up to speed on Java, and have a question about local class behavior. Here's a mini-app to illustrate: interface Foo { void print(); } class LocalClassTest { public static...
3
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore...
2
by: Marcin Kalicinski | last post by:
Hi, Why local structs/classes cannot be used as template parameters? void f(std::list &l) { struct Cmp { /*...*/ }; l.sort(Cmp()); // Error here }
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
2
by: Alex | last post by:
Yes you can: <html><head><script language="javascript"> SaveToFile('This is a text to save in a file', 'C:\\temp\\test.txt'); alert(read('C:\\temp\\test.txt')); function SaveToFile (text,...
17
by: Steven T. Hatton | last post by:
I didn't think this was possible in C++. I don't believe Stroustrup mentions this anywhere in TC++PL(SE). I've never seen it used in code that I can recall. Is this feature widely supported? Is...
2
by: A.J. Bonnema | last post by:
Hi all, I just started using Python. I used to do some Java programming, so I am not completely blank. I have a small question about how classes get instantiated within other classes. I have...
5
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I have a app that uses several membership/role providers. I can list these Providers with the code: Dim rootWebConfig1 As Configuration rootWebConfig1 =...
28
by: cpluslearn | last post by:
Hi, I have a local class inside a function template. I am able to wrap any type in my local class. Is it legal C++? What type of class is Local? Is it a class template or regular class? Thanks...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.