473,473 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

[newbe] instantiating class variable in constructor

Hi, this is my first day using c# and I must say it's cool but data
types.. I'm not use to them (php...) they're driving me crazy.

I need to declare a class variable, but I don't know it's type since
it's passed to the constructor when the object is created. I'd like
something like this:

class MyClass
{

public MyClass(int attribute)
{
if (attributes == 1)
{
string myDeck = "something";
}
else if (attributes 1)
{
int myDeck = "123";
}
}

public string tellMe() // by the way I'm saying string, but
it might be integer... what the..
{
return myDeck;
}
}
how can I solve this mess? Can anyone reassure me that this compulsory
typing isn't actually just a headache?

bye!
Luca(IT)
Jan 9 '08 #1
8 1841
Snaggy wrote:
Hi, this is my first day using c# and I must say it's cool but data
types.. I'm not use to them (php...) they're driving me crazy.

I need to declare a class variable, but I don't know it's type since
it's passed to the constructor when the object is created. I'd like
something like this:
[...]
how can I solve this mess? Can anyone reassure me that this compulsory
typing isn't actually just a headache?
Well, generally you would want to strongly type your data where possible. Is
there a practical reason you could not simply provide two separate properties to
provide access to your different data types?

Alternately, you could define your variable as "object", ie:
private object myDeck;

Chris.
Jan 9 '08 #2
what do you mean 2 diff properties? I'd still have do declare my
variable after having constructed the object. Let me ask this, is it
possible to declare variable object wide (accessible through
this.varName) after having created the object? Or do I have to declare
them all and as object the typeToBeDefined ones?

(hope I explained myself..)

bye
Jan 9 '08 #3

if you don't know the type.. you can either use "object", (which is kinda
crappy)..or

Use a Generic. Here is a sample. (see below).

As far as "compulsary typing"...strong typing makes more maintainable code.
The time spent on code is maintenance, not development.


class MyClass<T>

{

private T _model = default (T);

public MyClass(T attribute)

{

_model = attribute;

}

public T FindIt()

{

return _model;

}

}

class Program

{

static void Main(string[] args)

{

MyClass<intmc1 = new MyClass<int>(123);

int x = mc1.FindIt();

Console.WriteLine(Convert.ToString (x));

MyClass<Exceptionmc2 = new MyClass<Exception>(new Exception("You can use
any object here with Generics!"));

Exception foundEx = mc2.FindIt();

Console.WriteLine( (foundEx.Message ));

MyClass<Guidmc3 = new MyClass<Guid>(Guid.NewGuid());

Guid g = mc3.FindIt();

Console.WriteLine(g.ToString ("N"));

}

}

"Snaggy" <l.******@gmail.comwrote in message
news:69**********************************@c23g2000 hsa.googlegroups.com...
Hi, this is my first day using c# and I must say it's cool but data
types.. I'm not use to them (php...) they're driving me crazy.

I need to declare a class variable, but I don't know it's type since
it's passed to the constructor when the object is created. I'd like
something like this:

class MyClass
{

public MyClass(int attribute)
{
if (attributes == 1)
{
string myDeck = "something";
}
else if (attributes 1)
{
int myDeck = "123";
}
}

public string tellMe() // by the way I'm saying string, but
it might be integer... what the..
{
return myDeck;
}
}
how can I solve this mess? Can anyone reassure me that this compulsory
typing isn't actually just a headache?

bye!
Luca(IT)

Jan 9 '08 #4
at the "HERE" I got this problem when tried to debug:
C# is case sensitive. Try Add(...)

Marc
Jan 9 '08 #5
On Jan 10, 12:26*am, Marc Gravell <marc.grav...@gmail.comwrote:
at the "HERE" I got this problem when tried to debug:

C# is case sensitive. Try Add(...)

Marc
nope.. did it already
Jan 10 '08 #6
On Jan 10, 1:14 pm, Snaggy <l.cio...@gmail.comwrote:

<snip>
nope.. did it already
If you've done it in both places already, it should compile. I took
your code, changed "add" to "Add" in both places, and added the
appropriate using directives, and it works fine.

Jon
Jan 10 '08 #7
On Jan 10, 2:21*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jan 10, 1:14 pm,Snaggy<l.cio...@gmail.comwrote:

<snip>
nope.. did it already

If you've done it in both places already, it should compile. I took
your code, changed "add" to "Add" in both places, and added the
appropriate using directives, and it works fine.

Jon
yeah, it seems so... it must have been some other problem too, thanks!

anyway.
Returning to the main topic.. is it possible to define a variable
that'll be class accessible in a method?
Jan 11 '08 #8
On Jan 11, 4:30 pm, Snaggy <l.cio...@gmail.comwrote:

<snip>
Returning to the main topic.. is it possible to define a variable
that'll be class accessible in a method?
No. You can only declare *local* variables in methods.

Jon
Jan 11 '08 #9

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

Similar topics

2
by: Vijay Singh | last post by:
How do you instantiat a class with a Class.forName( ) method, if the class's default constructor requires a parameter?
1
by: thechaosengine | last post by:
Hi everyone, I posted a question earlier about a collection class that I'd made seemingly getting instantiated magically after a class that contained a variable of that type had its constructor...
6
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate...
2
by: david | last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway, although Ollie's answer makes perfectly sense when dealing with classes, it doesn't seem to me to apply as well if you have to...
0
by: MichaelSL | last post by:
I set up a global variable: string DesiredString; I set up an array of a struct: struct structProcessClass { public string Name; public Type Class; }
3
by: Mike | last post by:
Hello In the following code,why are there 2 different Class1's.Is one a class definition and the other a function? namespace Music { public class Class1 { public Class1() {
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
18
by: RB | last post by:
Hi guys (and gals!), I've got 2 classes, "TypesafeConstant" and "Color". "Color" inherits from "TypesafeConstant", and adds no new functionality. All "Color" does is to instantiate some class...
1
by: madshov | last post by:
Hi, I have made a matrix class, that I wish to make an instiation of in another class. Matrix class: #include <vector> #include <iostream> using namespace std; // So the program can see cout...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.