Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / February 2007

Tip: Looking for answers? Try searching our database.

Anyone knows the C# Equivalent

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
alee - 17 Feb 2007 16:52 GMT
Hello,

I am trying to convert a program from Microsoft VB.NET team:

http://blogs.msdn.com/vbteam/archive/2005/04/14/TableAdaptersAndObjects.aspx

I am having a hard time converting the following two sections.  Anyone knows
the C# equivalent?

- oringial VB.NET code from VB Team:
Public Sub AcceptChanges()

   For Each item As T In Me.Items

       item.ObjectState = ObjectState.Unchanged

   Next

   DeletedRows.Clear()

End Sub

- my approach:
     public void AcceptChanges()
       {
           foreach (T item in this.Items)
           {
               item.ObjectState = ObjectState.Unchanged;
           }
           DeletedRows.Clear();
       }
- Visusal Studio error: 'T' does not contain a definition for 'ObjectState'

2.  Original code from VB team:

Dim txScope As New System.Transactions.TransactionScope

   Using txScope
   ...
   end using

- my approach:

   using txScope
   {
       ...
   }
- Visual Studio:

Syntax error, '(' expected

Best regards,

Alan L.
sturnfie@gmail.com - 17 Feb 2007 17:48 GMT
> - my approach:
>       public void AcceptChanges()
[quoted text clipped - 6 lines]
>         }
> - Visusal Studio error: 'T' does not contain a definition for 'ObjectState'

What is your definition for "T"?  I am assuming this article is using
it as a generic object, meaning you need to create it and what it is
supposed to be; in this case, give it a property of "ObjectState".
Read the article to figure out what the expectations of the object are
(namely, what it is supposed to be based on).

> - my approach:
>
[quoted text clipped - 5 lines]
>
> Syntax error, '(' expected

This is an issue of scope.  The proper syntax is:

using (txScope)
{
  ....
}

--
lucas
alee - 18 Feb 2007 04:23 GMT
Lucas,

Thanks for your suggestions.  The using syntax works fine now.

However, I have tried several method to define the following VB.NET
equivalence without any luck.

VB.NET

Public MustInherit Class BaseList(Of T As BaseObject) Inherits BindingList(Of
T)

C# - tried

public abstract class BaseList<T as BaseObject> : BindingList<T>
public abstract class BaseList<T:BaseObject> : BindingList<T>

I just want to the generic T to inherit properties defined in BaseObject.

Cheers,

Alan L.

>> - my approach:
>>       public void AcceptChanges()
[quoted text clipped - 23 lines]
>--
>lucas
Barry Kelly - 19 Feb 2007 01:56 GMT
> Public MustInherit Class BaseList(Of T As BaseObject) Inherits BindingList(Of
> T)

public abstract class BaseList<T> : BindingList<T>
 where T : BaseObject

-- Barry

Signature

http://barrkel.blogspot.com/

alee - 19 Feb 2007 04:35 GMT
Hello Barry,

Thanks for your suggestion, and it does solve the problem.

Regards,

Alan L.

>> Public MustInherit Class BaseList(Of T As BaseObject) Inherits BindingList(Of
>> T)
[quoted text clipped - 3 lines]
>
>-- Barry
Göran Andersson - 17 Feb 2007 17:48 GMT
> - Visusal Studio error: 'T' does not contain a definition for 'ObjectState'

C# is case sensetive while VB is not. Have you checked the exact name,
so it isn't for example "objectState"?

> - my approach:
>
[quoted text clipped - 5 lines]
>
> Syntax error, '(' expected

using (txScope) {
   ...
}

Signature

Göran Andersson
_____
http://www.guffa.com

alee - 18 Feb 2007 04:27 GMT
Hi Goran,

Thanks.  The using syntax works, but I still have problem converting the
definition
for the generic T.  I

public abstract class BaseList<T of BaseObject> : BindingList<T>

-  I want the generic key argument inherits properties defined in BaseObject.

Originaal VB.NET syntax is:

Public MustInherit Class BaseList(Of T As BaseObject) Inherits BindingList(Of
T)

Cheers,

Alan L.

>> - Visusal Studio error: 'T' does not contain a definition for 'ObjectState'
>
[quoted text clipped - 10 lines]
>    ...
>}
Göran Andersson - 18 Feb 2007 13:48 GMT
If you have a base class for all objects that the class handles, then
you don't need to use generics at all in your class. Just use the base
class when you inherit the BindingList.

public abstract class BaseList : BindingList<BaseObject>

Now your BaseList is a BindingList that handles any object that inherits
from BaseObject.

In your method that loops the items, just reference them as BaseObject:

public void AcceptChanges() {
   foreach (BaseObject item in this.Items) {
      item.ObjectState = ObjectState.Unchanged;
   }
   DeletedRows.Clear();
}

> Hi Goran,
>
[quoted text clipped - 26 lines]
>>    ...
>> }

Signature

Göran Andersson
_____
http://www.guffa.com

alee - 19 Feb 2007 04:38 GMT
Hi Goran

The BaseObject will be inherited by other classes.  And I am defining an
extension to the
BindingList<T>.

I solved the problem by using the where keyworkd suggested by Barry and Mehdi.

Thanks for your feedback.

Regards,

Alan L.

>If you have a base class for all objects that the class handles, then
>you don't need to use generics at all in your class. Just use the base
[quoted text clipped - 19 lines]
>>>    ...
>>> }
mehdi - 18 Feb 2007 10:46 GMT
> Hello,
>
[quoted text clipped - 53 lines]
> --
> Message posted viahttp://www.dotnetmonster.com

Hi,

The first equivalent:
Well, you are trying to use a class (T) that is supposed to have an
ObjectState property. However, this should be explicitly declared
using the "where" keyword in C#. You need to change the class
definition to something as follows:

public class MyClass<T> where T: BizObjBase

this tells the compiler that T is of type BizObjBase...

The second equivalent:
using (TransactionScope txScope = new TransactionScope())
{
 //do whatever you need with txScope
}

HTH,
Mehdi
alee - 19 Feb 2007 04:34 GMT
Hello Mehdi,

Thanks for your suggestions, and they work well.

Regards,

Alan L.

>> Hello,
>>
[quoted text clipped - 22 lines]
>HTH,
>Mehdi
Alvin Bruney [MVP] - 19 Feb 2007 01:47 GMT
Google instant C#, this application will translate your VB code into C#. It
is very good.

Signature

Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon and
www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley

> Hello,
>
[quoted text clipped - 52 lines]
>
> Alan L.
alee - 19 Feb 2007 04:39 GMT
Hello Alvin,

I will look into using instant C# for my future projects.

Thanks and Regards,

Alan L.

>Google instant C#, this application will translate your VB code into C#. It
>is very good.
[quoted text clipped - 4 lines]
>>
>> Alan L.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.