C# OO - 1.1 - going from vb.net to c#

G

Guest

I need a little assistance in developing objects in C#.

Why does my C# obj barf?????



In vb.net I'll create a class obj in vb called ValCriteria. Make it
serializable so make my call as small as possible. Declare my members and
get and set my properties. My constructor is usually left blank unless I'm
initializing a hashtable or something.

Public sub new()

End sub

Then I create a function that returns that obj like so...

Public Shared Function NewValCriteria() As ValCriteria
Return New ValCriteria
End Function


C# is different.
I create my members and properties. Set my constructor(ok by default its
created). Then I try to create my function to create the instance of my obj
and c# continues to barf. Any idea?

Namespace val.business
{
[Serializable ()]
Public class ValCriteria : myBaseClass
{
Public ValCriteria
{
//blank constructor
}

private string _criteriaOne;

public string criteriaOne
{
get
{
return _criteriaOne;
}
set
{
_criteriaOne = value;
}
}

public ValCriteria Load ()
{
return ValCriteria;
}

}
}


Thanks
 
M

Marc Gravell

My interpretation of your code is below (I had to get rid of the base type
because it wasn't in the sample):
1: case is important in C#
2: even default ctors have parenthesese
3: shared maps to static; need a "new" operator; also not sure this serves
any purpose when the caller can call new ValCriteria() just as easily

Does that help?

Also:
Make it serializable so make my call as small as possible.

I'm not sure what you mean here, but I don't think it does what you think...

Marc

namespace val.business {
[Serializable()]
public class ValCriteria {
public ValCriteria() {
//blank constructor
}

private string _criteriaOne;

public string criteriaOne {
get {
return _criteriaOne;
}
set {
_criteriaOne = value;
}
}

public static ValCriteria Load() {
return new ValCriteria();
}

}
}
 
N

Nicholas Paldino [.NET/C# MVP]

mgonzales3,

Marking a class as serializable doesn't make your calls to methods on
the class "small", it just allows you to take the instance of the class and
then store it in a stream.

Your method should look like this:

public ValCriteria Load ()
{
return new ValCriteria();
}

This way, a new instance of the class is returned.

Hope this helps.
 
G

Guest

again. the team from redmond and elsewhere are found helping us smaller folks.

it works.

thanks
 
N

Nicholas Paldino [.NET/C# MVP]

I'm not from Redmond =)

I would fall in the "elsewhere" category. I don't work for Microsoft...
 
M

Michael S

mgonzales3 said:
I need a little assistance in developing objects in C#.

Why does my C# obj barf?????

While others have tried to give you some rational replies for you problem, I
would like to present a more esoteric explaination.

As Visual Studio has an inbuilt AI that 'feels' you are a VBer, all your
classes will implement IBarfable and most of your code gets wrapped with a
secret keyword:

barf(MyClass o = new MyClass()) { //code; }

... which is suggar, like the using statement, that translates into

MyClass o = new MyClass();
((IBarfable)o).Barf();

While I provide no proof for this; I'm pretty sure that this is what's
actually going on..
Perhaps Nicholas or Jon knows more... =)

Happy Coding
- Michael S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top