VB6 Module in C# 2003

  • Thread starter Thread starter Mark Jerde
  • Start date Start date
M

Mark Jerde

This is probably a simple FAQ I missed in my reading & googling. I've been
using C# for over a year but I don't know the best way to handle a simple
class that will be called many times from many objects. The class doesn't
have to save state, it is really a simple function. IIRC VB6's "module"
worked well for this.

For example I need a class that takes a byte or an array of bytes and
returns a nybble-oriented, leading-zero-filled string representing the bits.
E.g.
In: 0F
Out: "0000-1111"
I know how to write the class but I'm not sure the best way to set up the
rest of my program to minimize creation & disposing of its instances.

This class will be called a bazillion times from the 40 instances of a
usercontrol on my Windows form. Other than creating a BinaryNybble object
in the main form and passing it to the 40 UCs as they are created, is there
another way to have the 40 UCs share one BinaryNybble instance?

Thanks.

-- Mark
 
Mark,

You don't have to create instances of the class at all. If you are not
saving state in the instance of the class, then just make the method static,
and you can just call the method on the class itself, like so:

public static string DoSomething()
{
return "hello";
}

// Call in code.
string doSomething = MyClass.DoSomething();

Hope this helps.
 
Nicholas -- Thanks, that's what I was looking for. Don't know how I missed
it... ;-)

-- Mark

Nicholas Paldino said:
Mark,

You don't have to create instances of the class at all. If you are not
saving state in the instance of the class, then just make the method
static, and you can just call the method on the class itself, like so:

public static string DoSomething()
{
return "hello";
}

// Call in code.
string doSomething = MyClass.DoSomething();

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mark Jerde said:
This is probably a simple FAQ I missed in my reading & googling. I've
been using C# for over a year but I don't know the best way to handle a
simple class that will be called many times from many objects. The class
doesn't have to save state, it is really a simple function. IIRC VB6's
"module" worked well for this.

For example I need a class that takes a byte or an array of bytes and
returns a nybble-oriented, leading-zero-filled string representing the
bits. E.g.
In: 0F
Out: "0000-1111"
I know how to write the class but I'm not sure the best way to set up the
rest of my program to minimize creation & disposing of its instances.

This class will be called a bazillion times from the 40 instances of a
usercontrol on my Windows form. Other than creating a BinaryNybble
object in the main form and passing it to the 40 UCs as they are created,
is there another way to have the 40 UCs share one BinaryNybble instance?

Thanks.

-- Mark
 
Right - and that's what the VB module is - it's just a shorthand for a static
sealed class. (plus you don't have to specify the class name when calling the
methods).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter



Nicholas Paldino said:
Mark,

You don't have to create instances of the class at all. If you are not
saving state in the instance of the class, then just make the method static,
and you can just call the method on the class itself, like so:

public static string DoSomething()
{
return "hello";
}

// Call in code.
string doSomething = MyClass.DoSomething();

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mark Jerde said:
This is probably a simple FAQ I missed in my reading & googling. I've
been using C# for over a year but I don't know the best way to handle a
simple class that will be called many times from many objects. The class
doesn't have to save state, it is really a simple function. IIRC VB6's
"module" worked well for this.

For example I need a class that takes a byte or an array of bytes and
returns a nybble-oriented, leading-zero-filled string representing the
bits. E.g.
In: 0F
Out: "0000-1111"
I know how to write the class but I'm not sure the best way to set up the
rest of my program to minimize creation & disposing of its instances.

This class will be called a bazillion times from the 40 instances of a
usercontrol on my Windows form. Other than creating a BinaryNybble object
in the main form and passing it to the 40 UCs as they are created, is
there another way to have the 40 UCs share one BinaryNybble instance?

Thanks.

-- Mark
 

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

Similar Threads


Back
Top