Instantiating a Sub Class

P

pbd22

Hi.

I have a class with a subclass for metadata.

When I need to use the object, I instantiate the
parent class as such:

FileStream file = new FileStream();

And then assign values:

file.Method = value;
file.MetaData.FileName = "name";

But, when I get to assigning values to the subclass, I get a null
reference exception (I didn't instantiate the subclass).

I am not quite sure how i should be doing this?

Thanks.
 
P

Peter Duniho

pbd22 said:
Hi.

I have a class with a subclass for metadata.

What does that mean? Why are you inheriting the class just for the
purpose of maintaining metadata? Would something like the Decorator
Pattern be more appropriate?
When I need to use the object, I instantiate the
parent class as such:

FileStream file = new FileStream();

And then assign values:

file.Method = value;
file.MetaData.FileName = "name";

But, when I get to assigning values to the subclass, I get a null
reference exception (I didn't instantiate the subclass).

I am not quite sure how i should be doing this?

Doing what? What is it really that you are trying to do here?

The only code you showed was the code that works. I don't see how any
of us are supposed to figure out what's wrong with the code that doesn't
work, since you haven't posted any of it, never mind a
concise-but-complete code example that reliably demonstrates what your
issue is.

As far as the null reference exception, if that happens you must have a
null reference. And of course, you cannot access class members from a
null reference. The short answer to that part of your question is
"don't do that".

Since you say yourself that you "didn't instantiate the subclass", I
have no idea how you expect for the subclass's members to be accessed at
all. Where would that data exist, given that you haven't created an
instance of the class in which the data can exist?

As stated, your question is far too vague to be answerable in a reliable
way. What I can tell you is that if you want to use instance members of
a class, you have to have an instance _of that class_. The instance can
either inherit the class, or it can be the class, but either way the
instance has to be an instance of the class containing the members you
want to access.

If that's not a sufficiently detailed answer for you, please spend some
time to write a post that contains a clear, specific question and which
contains enough code for us to understand what you're trying to do and
why it doesn't work.

Pete
 
P

Peter Duniho

Peter said:
[...]
file.Method = value;
file.MetaData.FileName = "name";

But, when I get to assigning values to the subclass, I get a null
reference exception (I didn't instantiate the subclass).

I am not quite sure how i should be doing this?

Doing what? What is it really that you are trying to do here?

The only code you showed was the code that works.

Sorry...I didn't notice in your post you're trying to access members of
FileStream that don't exist. Obviously the above statement of mine is
incorrect.

Still, it's not clear from your post why you would think doing that
would work. If the variable is declared as "FileStream", you can't
access anything in the instance except members of the FileStream class
(either those declared by the FileStream class, or inherited by the
FileStream class from its base classes).

As I wrote before, please post a concise-but-complete code example, and
please rewrite your question so that it's specific and clearly states
what it is exactly you're trying to accomplish.

Pete
 
P

pbd22

Peter said:
[...]
file.Method = value;
file.MetaData.FileName = "name";
But, when I get to assigning values to the subclass, I get a null
reference exception (I didn't instantiate the subclass).
I am not quite sure how i should be doing this?
Doing what?  What is it really that you are trying to do here?
The only code you showed was the code that works.

Sorry...I didn't notice in your post you're trying to access members of
FileStream that don't exist.  Obviously the above statement of mine is
incorrect.

Still, it's not clear from your post why you would think doing that
would work.  If the variable is declared as "FileStream", you can't
access anything in the instance except members of the FileStream class
(either those declared by the FileStream class, or inherited by the
FileStream class from its base classes).

As I wrote before, please post a concise-but-complete code example, and
please rewrite your question so that it's specific and clearly states
what it is exactly you're trying to accomplish.

Pete

OK, thanks for the response.

I am designing an WCF service for file uploads (in this case, images).
As you have observed, I have created a class that allows me to both do
the stream and include metadata information in my parameter argument.

[DataContract(Namespace = "FileTransfer")]
public class FileTransfer
{
[DataMember]
public string GetUploadStatus;
[DataMember]
public int UploadFile;
[DataMember]
public FileTransferInfo FileInfo;
[DataMember]
public Stream FileByteStream;
}

[DataContract(Namespace = "FileTransfer")]
public class FileTransferInfo
{
private string _guid;
private int _flag;
private long _fileSize;
private string _fileName;
private DateTime _lastUpdate;
private FileTypeEnum _fileType;

private object _sync = new object(); // single lock for both
fields

public FileTransferInfo() { }

public FileTransferInfo(string _guid, int _flag, long
_fileSize, string _fileName, DateTime _lastUpdate, FileTypeEnum
_fileType)
{
this.Guid = _guid;
this.Flag = _flag;
this.FileSize = _fileSize;
this.FileName = _fileName;
this.LastUpdate = _lastUpdate;
this.FileType = _fileType;
}

[DataMember(Name = "Guid", Order = 3, IsRequired = true)]
public string Guid
{
get { lock (_sync) { return _guid; } }
set { lock (_sync) { _guid = value; } }
}

//ETC. Other getters and setters below...

}

Now, In my CodeBehind, I want to use the WCF service to
upload a file such that:

FileTransferServiceClient upload = new FileTransferServiceClient();
HttpPostedFile m_objFile = default(HttpPostedFile);
FileTransfer transmit = null;

// Loop Through HttpPostedFile And Assign Values

transmit.FileByteStream = m_objFile.InputStream;
transmit.FileInfo.Guid = Guid.NewGuid().ToString();
transmit.FileInfo.Flag = default(int);
transmit.FileInfo.FileSize = m_objFile.ContentLength;
transmit.FileInfo.FileName = m_objFile.FileName;
transmit.FileInfo.LastUpdate = DateTime.Now;

int retParam = upload.UploadFile(transmit);

// End loop

//NOTE: The loop fails when it gets to FileInfo.Guid and throws
//a null reference exception. When I step through in watch view,
//FileInfo is null.

Thanks again.
 
F

Family Tree Mike

pbd22 said:
Peter said:
[...]
file.Method = value;
file.MetaData.FileName = "name";
But, when I get to assigning values to the subclass, I get a null
reference exception (I didn't instantiate the subclass).
I am not quite sure how i should be doing this?
Doing what? What is it really that you are trying to do here?
The only code you showed was the code that works.
Sorry...I didn't notice in your post you're trying to access members of
FileStream that don't exist. Obviously the above statement of mine is
incorrect.

Still, it's not clear from your post why you would think doing that
would work. If the variable is declared as "FileStream", you can't
access anything in the instance except members of the FileStream class
(either those declared by the FileStream class, or inherited by the
FileStream class from its base classes).

As I wrote before, please post a concise-but-complete code example, and
please rewrite your question so that it's specific and clearly states
what it is exactly you're trying to accomplish.

Pete

OK, thanks for the response.

I am designing an WCF service for file uploads (in this case, images).
As you have observed, I have created a class that allows me to both do
the stream and include metadata information in my parameter argument.

[DataContract(Namespace = "FileTransfer")]
public class FileTransfer
{
[DataMember]
public string GetUploadStatus;
[DataMember]
public int UploadFile;
[DataMember]
public FileTransferInfo FileInfo;
[DataMember]
public Stream FileByteStream;
}

[DataContract(Namespace = "FileTransfer")]
public class FileTransferInfo
{
private string _guid;
private int _flag;
private long _fileSize;
private string _fileName;
private DateTime _lastUpdate;
private FileTypeEnum _fileType;

private object _sync = new object(); // single lock for both
fields

public FileTransferInfo() { }

public FileTransferInfo(string _guid, int _flag, long
_fileSize, string _fileName, DateTime _lastUpdate, FileTypeEnum
_fileType)
{
this.Guid = _guid;
this.Flag = _flag;
this.FileSize = _fileSize;
this.FileName = _fileName;
this.LastUpdate = _lastUpdate;
this.FileType = _fileType;
}

[DataMember(Name = "Guid", Order = 3, IsRequired = true)]
public string Guid
{
get { lock (_sync) { return _guid; } }
set { lock (_sync) { _guid = value; } }
}

//ETC. Other getters and setters below...

}

Now, In my CodeBehind, I want to use the WCF service to
upload a file such that:

FileTransferServiceClient upload = new FileTransferServiceClient();
HttpPostedFile m_objFile = default(HttpPostedFile);
FileTransfer transmit = null;

// Loop Through HttpPostedFile And Assign Values

transmit.FileByteStream = m_objFile.InputStream;
transmit.FileInfo.Guid = Guid.NewGuid().ToString();
transmit.FileInfo.Flag = default(int);
transmit.FileInfo.FileSize = m_objFile.ContentLength;
transmit.FileInfo.FileName = m_objFile.FileName;
transmit.FileInfo.LastUpdate = DateTime.Now;

int retParam = upload.UploadFile(transmit);

// End loop

//NOTE: The loop fails when it gets to FileInfo.Guid and throws
//a null reference exception. When I step through in watch view,
//FileInfo is null.

Thanks again.


Depending on your design you have a couple of choices. You could create
a new FileInfo object in your "CodeBehind", and set transmit.FileInfo to
that object. Alternatively in your constructor for the FileTransfer
object, initialize the FileInfo object there.
 
P

Peter Duniho

Family said:
pbd22 said:
[...]
//NOTE: The loop fails when it gets to FileInfo.Guid and throws
//a null reference exception. When I step through in watch view,
//FileInfo is null.

Thanks again.


Depending on your design you have a couple of choices. You could create
a new FileInfo object in your "CodeBehind", and set transmit.FileInfo to
that object. Alternatively in your constructor for the FileTransfer
object, initialize the FileInfo object there.

In addition to what Mike wrote (which would be the canonical reply for a
plain C# application), you should consider that a) in your first post
you never mentioned at all that you're using ASP.NET (which, based on
your comment about "code behind", I assume you are), and b) you can do
things in ASP.NET that wouldn't make any sense at all in plain C#.

In other words, there's a _possibility_, depending on the context in
which you're trying to use this "FileInfo" member, that ASP.NET should
have initialized it for you and that there's a specific ASP.NET
technique for doing so. If you're unable to constrain your question by
describing it solely in terms of plain C#, that's a strong indicator
that you've posted your question to the wrong newsgroup and would find
more useful help in an ASP.NET-specific newsgroup.

Pete
 

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